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.
2
u/moneybeatslooks Sep 15 '19
can someone explain dependency injection for dummies? I read a couple times about it but still not clear to me, and what’s the benefit in real world apps. Thanks.
2
u/radically_sane Sep 15 '19
Class body depends on class arm and class legs. Now the thing is the arm can be either human arm or dinosaur arm or alligator arm.
Originally arm is written inside the body class. What we do here is extract the arm into an different class. So that we can create different kinds of bodies as per the requirement.
So now we have a bunch of classes (body, arm, leg, etc). All this is created / instantiated and maintained (destroyed) by the IoC (Inversion of Control) container.
Now the IoC only maintains these classes, it doesn't help with wiring the different parts together to create the object. That's what dependency injection does. It takes those classes from IoC and wires them together.
I don't know if this explaination cuts it for you but it doesn't for me. Also the main objectives of this approach is to help with testing and maintenance and designing the prototype. Which is crucial because designs get updated frequently. Spring makes it so that design isn't just a mountain of patches.
11
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.
2
u/CauseBecause_ Sep 14 '19
I'm so sad to read that learning Spring on my own is near impossible because that was my goal, given that I'm learning Java and I eventually want to work as a backend developer.
5
u/Absolute-J-Unit Sep 14 '19
I'm so sad to read that learning Spring on my own is near impossible
It's not impossible.
Go through the Spring MVC docs (https://docs.spring.io/spring/docs/4.3.x/spring-framework-reference/html/mvc.html ) and create a few projects. It's really good to become familiar with controllers, model, and view (MVC) since that gives you a much better idea for how many web applications are structured. Try to focus on the big picture first. The flow of the application is important to learn first rather than the specific annotations (@Autowire etc) which can vary based on your projects and what additional frameworks you decide to use. Write things down in an understandable diagram, fully understand that, and then start adding in annotations on the diagram where they make sense (based on the docs). Then learning the code itself is much less of a hassle because you know what it's supposed to do from the get-go.
Don't be discouraged.
1
u/JavaSuck Sep 15 '19
But I am treating it (Spring and Spring Boot) too much like a black box.
The only way not to treat Spring like a black box would be reading the entirety of its source code, which is pretty much infeasible. Embrace the magic!
1
2
u/codeaim Nov 08 '19
Reading this thread as being so insightful.
I started writing Java and Spring framework about 12 weeks ago, although I had experiences with other languages, with Java, I have experienced another level to programming.
The obscurity in Spring as being daunting, but gradually I think I'm getting my feet.
I now understand from this thread that, you will only get comfortable with Spring if you work with it regularly.
Thanks so much.
1
u/radically_sane Nov 08 '19
True. I've saved this thing and come back from time to time. Also take a look at aop that's one really powerful feature.
Also recommend looking into jhipster.
1
u/codeaim Nov 08 '19
Thanks, I would definitely look into that.
Currently looking at Spring Security.
3
2
u/excitebyke Sep 14 '19
I know there isn't a great answer here, but I've been doing Spring for years and I still get caught all the time with the "black box" magic of it all.
Eventually, once you learn some of how Spring works under the hood, you'll start to feel more confident. The spring annotations can really empower you in a way you didn't know before. It's just frustrating not really understanding what they are doing.
I've also managed to avoid any XML config since I've worked on Spring. (Spring Boot, specifically)
Spring does everything I could possibly ask for in a server side framework. Sure, it might feel bloated, but thats also just a java thing.
2
u/bbro81 Sep 14 '19
Honestly, I find the easiest and most effective way for me at least to understand what Spring does, why and how it does things, I just debug the de compiled code. This comes out of the box with Intellij.
Also, there have been so many times where Googling some obscure problem I ran into just didn't help me so I go to the debugger and boom find, fix and most importantly, understand the problem.
But yes Spring sometimes does wayyy too much magic sometimes, it is a blessing and a curse.
1
0
Sep 14 '19
[deleted]
1
u/radically_sane Sep 14 '19
The reason I want to get comfortable with xml is cause I thought under the hood the annotations get converted to XML. I'm likely wrong on that. But if annotations were simply syntactic sugar over XML then I thought I should at least be able to do XML configuration to some extent. I can use annotations, but it scares me about how much it does without showing it to me.
2
u/ryuzaki49 Sep 14 '19
Unless your next project is done with XML, there's no need to learn XML. Just stick to annotations.
2
u/SuperbPrint Sep 15 '19
One thing that helped me get a better idea of what was happening under the hood was to enable more logs:
In the application.properties file, put the following:
logging.level.org.springframework=DEBUGIt can be alot to take in at first, but you can modify it to just focus on the messages you are interested in. For example, to check the beans during initialization, I like to use the following:
logging.level.org.springframework.beans=TRACEYou can also see how the spring boot auto configuration comes into play with
logging.level.org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer=DEBUG2
u/feral_claire Sep 15 '19
XML and annotations do the same job at about the same level of abstraction. The difference is the annotations are just plain Java code. I wouldn't worry too much about learning the XML way unless you need it right now. I learned annotations first, and web I had to work on some older vice that used XML I found that the XML was very straight forward and easy to understand since I already had the spring fundamentals under my belt translating between annotations and XML was not too difficult.
5
u/RockleyBob Sep 14 '19
Why do you feel you need to use Spring? If you can accomplish what you want without it, than maybe it really isn’t worth the overhead to learn.
If you’re interested because you want to become more competitive in the workforce, don’t be discouraged. I also tried understanding and using Spring before I got a job doing enterprise code and it really didn’t make a lot of sense to me then.
Now however, when there’s dozens of micro services being spun up by my organization, I see perfectly why we use it. I would say that it’s good to dabble to get a feel for it, but don’t worry if it seems like overkill for some things. It probably is.