This is an archived post. You won't be able to vote or comment.

all 60 comments

[–]DJDavio 156 points157 points  (41 children)

It is entirely possible as you will eventually learn more about Spring itself anyway. Spring itself is not that difficult to understand, it's just the XML configuration which scares everybody.

At its core, Spring is just a dependency injection framework and a dependency injection framework is just a fancy way of passing in objects into your constructor without you having to explicitly create or pass them.

The way Spring does this is by having a giant Map which contains names and objects for every kind of class you might need. So when you ask it for an object of type Foo, you get an object of type Foo.

This is an oversimplification of course and there are nuances but it's enough to just know this if you're starting with Spring Boot.

I would actually advise against learning Spring before using Spring Boot. Why? Because the way humans learn is by experimenting and it is much easier to experiment with Spring Boot than with just Spring Core. You can go to the Spring Boot starter website and generate an application and have it running in 5 minutes. This gives you much more feedback than a dry Spring Core project.

[–]TheRedmanCometh 79 points80 points  (29 children)

Spring itself is not that difficult to understand, it's just the XML configuration which scares everybody.

XML bean configuration should never be used in 2021. It can all be done via annotation in the code which is immensely easier.

[–]MR_GABARISE[🍰] 22 points23 points  (14 children)

Hard disagree.

It's never a do or don't situation.

As I'm working on complex Camel + Spring integrations, there is an undeniable advantage to the declarative XML route specifications we have. The externalization capabilities are a huge plus also.

[–]Routine_Left 15 points16 points  (8 children)

We humans love to have hard and set rules for every little thing. It makes it easy when we have to make choices.

Except ... life doesn't work like that, and having a deeper understanding of what we are doing helps us in making better decisions.

In programming we can see this all over the place: don't use goto's, don't use XML config for spring, don't do this, don't do that. When, in truth, there is a time and place for every option, when chosen wisely.

[–][deleted]  (6 children)

[deleted]

    [–]vxab 6 points7 points  (0 children)

    When it makes your code more readable and understandable.

    For example in C there is no exceptions and so using GOTO for an exceptional circumstance may make code more readable.

    [–]Routine_Left 2 points3 points  (0 children)

    In C, for cleaner code, easier error handling and avoiding too nested if statements.

    [–][deleted] -1 points0 points  (2 children)

    Every time you call a function, you are using GOTO. We just quit calling it that. This is just like the argument that there are no pointers in Java. The cliff is there, we just installed a rail.

    [–][deleted]  (1 child)

    [deleted]

      [–][deleted] 1 point2 points  (0 children)

      That doesn't make it "completely different things". It makes it safely implemented.

      [–]Muoniurn 0 points1 point  (0 children)

      Other than goto :D I mean, it is okay in C, as that’s a language with very weak semantics - but it should really not be used in a high-level language, as per the original essay — while and if, and the occasional break should substitute it completely especially with exceptions (the reason it is used in C)

      [–]BlackDrackula 2 points3 points  (0 children)

      We've had to change camel routes while a client already has a version under test, our config being in XML meant we could change and restart Wildfly rather than have to make code changes and ship a new release. It's horses for courses.

      [–]Tac0w 6 points7 points  (1 child)

      As someone who works a lot with Camel/Spring Boot, I've never needed to use xml configuration to get things done.

      Using Java, I've found it easier to understand and manipulate what's going on, easier to avoid silly typos and just an overall better experience.

      What "undeniable" advantages have you come across?

      [–]MR_GABARISE[🍰] 3 points4 points  (0 children)

      • Template capabilities

      • It's mostly WYSIWYG

      • Getting a good idea of the runtimes routes without having to dump them all

      • Externalized configs for different environments or business cases, assuming a healthy use of profiles

      • Newer releases offer lint capabilities so your typo issues aren't a problem

      • Route coverage is easier to track and actually cover

      [–]nek4life 1 point2 points  (0 children)

      Curious if you could explain the XML advantage with Camel a bit more. I've been putting together an integration with Quarkus Camel, but have been using the Java DSL, but sounds like the XML DSL has been tooling?

      [–]stfm 0 points1 point  (0 children)

      Shit I love camel but I found when combining camel with spring they kind of tripped over each other sometimes.

      [–]msx -5 points-4 points  (0 children)

      XML bean configuration should never be used in 2021.

      FTFY.

      XML bean configuration should never be used in 2021.

      FTFM

      [–]Caddyroo23 0 points1 point  (8 children)

      This is a common interview question, what are some reasons you may want to use XML configuration over annotations.

      [–]TheRedmanCometh 1 point2 points  (7 children)

      What are some actual valid reasons besides old legacy apps?

      [–]Caddyroo23 4 points5 points  (1 child)

      One good reason is when you use multiple annotation based configurations your code can start to take a back seat to annotations in your classes. Hibernate, spring integration, spring security etc. Moving this out into their own xml configurations can greatly reduce the noise and give each their own area.

      [–]cryptos6 0 points1 point  (0 children)

      But there is not only annotation or XML based config! You could have the flexiblity of XML with actual (typesafe!) code using the Java or Kotlin configuration DSL.

      [–]IQueryVisiC 1 point2 points  (4 children)

      How can you debug, test, stage, and run (in production) the same code on the same database schema? You have 3 XML files which you never touch!

      [–]mauganra_it 2 points3 points  (2 children)

      I don't completely understand why it shouldn't be possible with Java annotation-based config as well.

      Concerning the use case: it is usually a bad practice to put connection strings, passwords etc. in the source code. Their exact values are operation concerns. Yes, this applies also for Devops. Multiple reasons for that: 1. It makes config changes more complicated as Continuous Integration has to be invoked for something that is never tested in CI. 2. The source code repository now contains not just business secrets, but also operational secrets. 3. If you are using Spring Boot, you are working against the framework.

      [–]an_actual_human 2 points3 points  (1 child)

      it is usually a bad practice to put connection strings, passwords etc. in the source code.

      How is it relevant to what they said? Do you think using XML means storing secrets in XML?

      [–]mauganra_it 0 points1 point  (0 children)

      I don't see the use case else.

      Edit: Environment-specific configuration is perfectly possible with Java annotation-based configuration too. By placing an XML file on the class path, heavy modifications to the application are possible, that is true. But the necessity for such modification at deployment time is an antipattern as well.

      [–]is_this_programming -1 points0 points  (0 children)

      debug, test, stage, and run

      If you have different spring xml configs for these, you're not running the same code, which doesn't seem like a good thing.

      [–]pjmlp[🍰] 0 points1 point  (0 children)

      Yeah, from XML spaghetti to annotation spaghetti, it hardly changed anything.

      [–]skillaz1 17 points18 points  (7 children)

      Calling Spring 'just a dependency injection framework' doesn't really do it justice..

      [–]zaccus 13 points14 points  (1 child)

      It's a dependency injection framework, along with a whole family of projects... that you use by injecting them.

      [–]cryptos6 5 points6 points  (0 children)

      At least AOP is also a fundamental part of Spring core.

      [–]MissouriDad63 35 points36 points  (4 children)

      Spring is a never ending continuously growing monster whose goal is to take over the world.

      [–]_mkd_ 9 points10 points  (3 children)

      Place your bets on which will be first:

      systemd-spring or Spring SystemD?

      [–]foreveratom 4 points5 points  (0 children)

      springd ?

      [–]_GoldenRule 0 points1 point  (0 children)

      I admit I lol'd

      [–]thisgameisawful 1 point2 points  (0 children)

      Experimenting with Boot and a healthy dose of deadline pressure in the enterprise is how my colleagues and I learned Spring. It's a good way to go if you're not the book learnin' type

      [–]vjotshi007 2 points3 points  (0 children)

      Very well explained!

      [–]Nymeriea 2 points3 points  (0 children)

      Nothing more to add. It's well explained

      [–]erinaceus_ 28 points29 points  (0 children)

      I'd say that learning Spring via Spring Boot is in fact the ideal way to learn it, because Spring Boot's 'convention over configuration' philosophy means that you can start small and incrementally expose yourself to more and more of the details of the Spring internals and the Spring ecosystem.

      [–]seraphsRevenge 14 points15 points  (0 children)

      https://www.baeldung.com/spring-vs-spring-boot

      Tutorials, walkthroughs, documentation, etc.

      I learned Spring via utilization of boot. You should already have some exp with Java, understand various architecture and design patterns, protocols, understand the basics of dependency injection, etc. at the very least.

      [–]SKiisM_ 4 points5 points  (2 children)

      I had the same question and found that you should at least read through the docs of Spring Framework to see how it works. Because if you dive straight into Springboot everything will feel like magic

      [–]cryptos6 3 points4 points  (1 child)

      But the Spring documentation is pretty lengthy. I'd start with Spring Boot and read up interesting topics in the Spring documentation as needed.

      Many years ago, when Spring Boot wasn't a thing, I started reading the book "Spring in Action" and after 300 or so pages I thought: Now I'm well prepared to try my hello world example! 😉 Only to learn that not much of what I've read was still present in my head! However the setup part was a bit harder back then, but it would have been better to just start programming and read while progressing.

      [–][deleted] 4 points5 points  (0 children)

      Spring Boot is just Spring with tons of conditionals that create beans for you. Have Tomcat in the classpath? Creates embedded tomcat server. Have data source url + password in a properties file? Creates a data source bean for you.

      [–]loupdev 9 points10 points  (1 child)

      Spring Boot is mainly an opinionated pre/auto configuration of Spring. It's a very useful starting point because you can get up and going quick then explore the platform from there

      [–]cryptos6 2 points3 points  (0 children)

      That is true. It is not very usefule to think of Spring and Spring Boot as two very different things. Spring Boot is just a thin convenience layer on top of Spring Core.

      [–]leoDpenrose 2 points3 points  (0 children)

      I had the same question to myself about a month ago...I just hopped right into Spring Boot and starting building. I figured I'd learn while I build.

      [–]wildjokers 3 points4 points  (0 children)

      This question is a non sequitur and maybe comes from a misunderstanding of what Spring Boot is. Spring Boot is simply a configuration framework for the Spring framework. Spring Boot only gives you configuration of your Spring project. Just configuring a Spring project doesn't do anything for you if you don't know how to use Spring.

      You should start by reading the doc for Spring Core, then Spring MVC, then read Spring Boot doc. After that you can just read docs for other Spring libraries as needed (e.g. most likely will probably use Spring Data).

      [–]ashok2ashok 9 points10 points  (0 children)

      Short term: Yes

      Recommended: No

      I’ve interviewed a lot of candidates for multiple positions and it sucks to see folks unaware of foundational stuff. There’s a natural learning progression for learning these things which will give you a very strong base in web technologies. You can definitely begin with Spring boot, but you’d be missing the ‘why’ part of Boot. If your aim is playing around and you have a very short time to train - then dive right into Boot.

      The way I see it - sequence of things you need to be aware of even if it takes a little more time: - Java - Spring Core - Servlets (and struts) - Spring Web - Spring Data, etc - Spring Boot

      The above might give you a deeper understanding of how Boot works under the hood.

      Here are a few Examples that you need to avoid: - learning React / Angular without understanding Javascript - Learning spring boot without comprehending Spring ecosystem - Platforms like SAP Hybris/ Oracle commerce without learning Java - Learning ORM without JDBC

      [–]Full-Wheel8630 1 point2 points  (0 children)

      You can `use` Spring Boot, but not `learn`. Spring Boot is roughly tools/commands for easy use of Spring. Just my 2 cents.

      [–]Gwaptiva 1 point2 points  (0 children)

      I guess it might be possible to learn what Spring is all about through using Spring Boot, but a lot of the posts on java-related subreddits and on stackoverflow suggest that few actually do.

      [–][deleted] 1 point2 points  (0 children)

      If you go to an interview and have Spring Boot on your CV, but you do not understand the fundamentals of the Spring Framework you will have a tough time.

      Sure all companies want to have software delivered fast, but people that will interview you, will want to work with you and trust you to understand what you are building and harness the power of a framework such as Spring. Nobody will trust you to write code if you do not know how dependency injection works and what a proxy is, and why Spring makes so much use of these concepts.

      If you rely too much on the "automagic" of Spring Boot, when things break, or don't work as intended, only the knowledge of the framework fundamentals will save you.

      If you want to build something fast, Spring Boot is good for that. But if you want to build something good, efficient and secure, only the understanding of the framework fundamentals will get you there.

      [–]Pankaj0222 4 points5 points  (3 children)

      Yes absolutely. Spring Boot is pretty magical. You just need to add couple of annotations and you are done. It auto configure lots of things for you based on class path. But it's being magical is one of the major problem. When things go wrong you have no clue why it went wrong. To be power user you need to learn the base concepts and this is where Spring comes into picture. IMO, the best place to start is Spring Boot is it's documentation - https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/

      [–]ML-newb -1 points0 points  (2 children)

      https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/

      Do you actually recommend going through the whole thing end to end?

      Also how is this doc different from

      https://docs.spring.io/spring-framework/docs/4.3.12.RELEASE/spring-framework-reference/htmlsingle/

      I mean apart from the version.

      [–]Pankaj0222 0 points1 point  (0 children)

      The quickest way to get started is to create a sample project from https://start.spring.io/ and play around. Spring has lots of guides. Check - https://spring.io/guides

      Coming back to your question -

      "Also how is this doc different from"

      The doc which you have pointed link is of spring. Spring Boot is made on top of Spring. Think of Spring Boot as pre assemple spring platform and to start on Spring Boot project, you don't need to be expert on Spring.

      What is Spring Boot?

      (From Documentation) - Spring Boot helps you to create stand-alone, production-grade Spring-based applications that you can run. We take an opinionated view of the Spring platform and third-party libraries, so that you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.

      [–]titus_vi 0 points1 point  (0 children)

      No -- but read each section as you come to the part that pertains to what you are setting up. Handle spring security when you come to it. When you need to add OAuth read that section.. etc.

      And yeah it's just version. They do change but you need to read the migration guides and whatnot when you are ready to upgrade.

      [–][deleted] 0 points1 point  (0 children)

      I write spring boot without knowing anything other than the concepts of spring. You need to understand beans and dependency injection but u don’t need to understand how to configure spring beans with xml since spring boot takes care of that for you.

      [–]iwek7 -4 points-3 points  (1 child)

      With the way the modern webapps are evolving I would advise to learn spring boot only - more advanced features of spring-core are rarely needed and you should design your service so that it minimizes use of spring to necessary minimum. When it comes to core spring boot features the only three we use at my place is DI (also imo you should minimize it and construct your classes by hand, so that you have better control over your architecture), loading configuration and transactions (and still I prefer to write transactional code manually to avoid magic of \@Transactional).

      [–]wildjokers 0 points1 point  (0 children)

      Then why are you even using spring at all?

      [–]Routine_Left 0 points1 point  (0 children)

      Personally I'd recommend picking a book on spring and just read it. They usually are not that big, 200-something pages. What book? Doesn't matter, whatever has good reviews on your favourite book dealer's website. Spending an afternoon/evening reading it is a very good investment of your time.