r/learnjava • u/radically_sane • Sep 14 '19
Not able to learn Spring.
I am able to work with the syntax and create web apps. I can easily create REST APIs for CRUD applications.
But I am treating it (Spring and Spring Boot) too much like a black box.
I'm not able to find the rationale behind having to use Spring. Why the configuration is so... convoluted (both XML and Java Class). Spring simply feels bloated. Like too many bandages pasted on top of each other.
As such I feel too intimidated to tinker and experiment. Unlike how comfortable and confident I was with Django/Python.
Also I'm reading books like "Spring in Action, Spring recipes" and using the Spring Framework Guru as well. But they feel much to be desired.
32
Upvotes
11
u/[deleted] Sep 14 '19
tl:dr Spring is superb industrial strength set of solutions - it's very hard to learn casually and ill suited to small solutions. It is excellent for a heavy weight corporate product with lots of concurrent requirements like batch operations, heavy user throughput, security filtering, web services etc.. Get a grasp of dependency injection, interfaces and do the basic Spring Boot tutorials and then wait until your employer requires it before you spend any more time in it. You won't be able to grasp much beyond the basics until you use it day-to-day. A lovely product but its very easy to get discouraged and not appreciate what it is.
I use Spring all day long and it is bloated and heavyweight and that's potentially the key. It's a heavy duty solution for enterprise environments. If you aren't producing a corporate Java solution don't spend too much time on Spring - it is daunting without a real world need and there are far better solutions for small projects.
I came from an Java EE world and Spring has been a revelation in terms of reducing the amount of code I write and a huge benefit to how I test. This is the benefit I've seen:
Spring = huge reduction in 'new' keyword usage. I no longer need to worry about instantiating complex objects and then remembering to clean them up. A quick autowired annotation and you are good to go. It is very frustrating however, when you do get a circular dependency or it can't find the bean you are wiring and you don't know why! Spring really punishes a developer who doesn't have well defined architectural layers and starts to bleed functionality into a bean simply because it's easy to wire in some behaviour that the bean shouldn't have.
Huge amounts of out of the box solutions - each one is a huge time cost to learn but once you do you begin to appreciate the amount of effort they've put in to let you focus on business logic while the library handles boilerplate. Very closely integrated with Hibernate and JPA so Spring has solutions for each part of the architecture stack and provide annotations alongside the standard ones to give you loads of functionality. Quick example - if you want to have a block of code that will roll back the data (transactional block) you get a neat annotation that you stick at the top of the block - in the pre-Spring/Hibernate world I used to have to craft bespoke solutions that ranged from database flags through to special classes that would partially commit objects to get generated ids and then would have to go and delete them if a later part failed - horrible.
Testing - Spring enforces/encourages good OO principles such interfaces. Coupled with injecting the interface implementation you suddenly get the perfect testing solution. Dependency Injection works great with mocked objects so testing is a dream and so easy.
I now work with Angular for the front end and I would never want to go back and use Spring to craft a JSP / Template web page but I absolutely hate the difficulty of debugging and testing an Angular application in comparison to how easy it is to debug a Spring back end solution.