r/learnjava • • Aug 10 '20

Whats is spring/spring boot?

Sorry just a newbie question.. but I was wondering if someone can explain to me in simple terms what spring/spring boot is. I mainly use Java to program desktop apps using swing and mysql as the database. Is this in any way related to what I'm doing? I've been reading on google that it's a framework but I still don't understand what it does. Is it something that helps in developing web apps? Any help would be appreciated thanks.

65 Upvotes

25 comments sorted by

69

u/FrenchFigaro Aug 10 '20

Spring is an application framework whose core concept is dependency injection, which means that, as long as you use the proper declarations in your code, Spring takes care of the initialization of the various components of your app.

A framework is basically a skeleton app that can be used to concentrate on the actual problems your application is supposed to solve, rather than the meta-problem of building a functional application before putting your business code in it.

Spring can be used to develop desktop programs, but it's not its main area of usage. As someone else pointed out, it's mostly used for back end applications (web services, mostly). Spring MVC can be used to create stand-alone web applications using JSF or ThymeLeaf.

Spring is configuration heavy, which is where Spring Boot comes into play. Boot is an overlay over Spring, and it's (mostly) a collection of default configurations for Spring components, that helps you start a project more rapidly by allowing you to configure components only if you need a configuration that's different from the default. There's actually a bit more to it, but that's the main idea.

Boot is used a lot in webservices environment, because it allows to embed a server container inside the application and have it run as a stand alone, rather than having to deploy it on an external server (like tomcat, or glassfish, or whatever). Which is great for dockerisation (among other stuff)

3

u/Dokiace Aug 10 '20

How long has you been working on spring? I really like your explanation

8

u/FrenchFigaro Aug 10 '20

I never worked ON Spring, but I've been working with it since '15 I think, so 5 years ?

2

u/Dokiace Aug 10 '20

my bad, I meant working with Spring. would you mind explaining spring bean to me? somehow Bean still confuses me

7

u/FrenchFigaro Aug 10 '20

Okay, I'll try.

In java, a "bean" is another name for a POJO (Plain Old Java Object), and not to be confused with an EJB (Enterprise Java Bean), which is a whole other kind of hornet's nest. That kind of bean is not a Spring Bean (yet)

So basically, a POJO is an object with attributes, getters, setters (if and when appropriate) and that's about it. Maybe a couple methods to compute some values from the attributes, but no real business logic (something like "getAverage" in an class that holds a list of grades, or "getModulus" in a class that represents a complex number). A POJO is pure data.

In a traditional approach, if you needed an instance of a POJO, you'd create one with new, set the attribute (through the setters, through a constructor, or even through a builder) as appropriate, and that'd be it.

But in some cases, you could find yourself needing a POJO that always has the same attributes. Of course, you could hard code a global constant, but sometimes, that's not enough (if the value is configuration dependant, for example).

That's where the @Bean annotation comes into play. It marks an object as a Spring Bean, which is a unique instance, whose values and instantiation are left up to Spring, according to the configuration. The values can be hard coded in a configuration class, like in this example, or, more interestingly, they can be configured in the beans.xml configuration file, like in that example. The values can also be profile dependant, so that you use one value on the integration environment, and another in production for example.

Now, @Bean can be applied to more complex objects, but for classes holding business logic, you'd be more likely to use @Component, @Service, or some other related annotation.

4

u/jtcuber435 Aug 10 '20

A spring bean is just an object that is managed by the spring container. You can configure spring to instantiate and put together a bean for you, which then can be accessed by the rest of your code.

1

u/Dokiace Aug 10 '20

what's the use case for spring bean? how is it different than @Autowired?

4

u/jtcuber435 Aug 10 '20

Spring beans are kind of the fundamental idea behind the spring framework. In large projects it is easier to get beans from the application context instead of using new every time you want an instance of a certain class. @Autowired is an annotation used to configure dependency injection.

4

u/Yithar Aug 10 '20 edited Aug 10 '20

I mean it's the central idea behind the Spring framework. Rather than manually creating objects Spring does it for you once (generally beans are singletons but you can change it).

I'm not sure what you mean by how it's different. @Autowired looks for a Spring bean. @Autowired won't work without Spring beans.

Spring beans are managed by Spring. Which means Spring is aware of them and can do things. For example, Aspect-Oriented Programming. Let's say you want metrics for how long each microservice takes. You would use AOP for that, because you don't want to add the same code to every single application. Spring AOP can only aspect methods in a Spring bean. The way I think about AOP is extra code is executed before, during or after a method is executed.

Without the notion of Spring beans, you have to use bytecode manipulation to do AOP.

1

u/[deleted] Aug 10 '20

How is it great for dockerizarion? The last layer contains the whole thing even though your app is the only part likely to change often... Having a layer with the server and at the end a thin layer with your logic is what's best for dockerizarion. Unless I'm mistaken and that's how it works.

5

u/FrenchFigaro Aug 10 '20

It's great because you can very, very, very easily load balance multiple instances of your application, and start/stop them automatically as well.

Most common server in our world is Tomcat, and it doesn't handle multiple instances of the same application on the same server instance very well, so in this case, you need to duplicate the server as well. More straightforward if everything is on the same docker image.

Works especially well in a micro-services architecture, where you might load balance only some of the services and not all of them (although I am still not conviced that Spring, even with Spring Boot, is the best framework for microservices. Something like Spark works better in this case imho, but that's far beyond the scope of the OP).

2

u/[deleted] Aug 10 '20

You can load balance multiple instances easily with any java server. I don't think SpringBoot is suited particularly well for Docker, hence my question. I thought something had changed.

2

u/feral_claire Aug 10 '20

That's the recommended way to do it, making proper use of layers. The reality is many folks just throw everything on the final layer because that's how you would deploy to a vm, but that doesn't mean you have to do that. In the latest version the spring boot team actually added a tool to build a docker container with properly optimized layers. But even before that it was best practice to do that and other Java tools existed (jib) before the boot specific one was created.

1

u/[deleted] Aug 10 '20

Ok, I'll try to check it out! I just don't wanna throw another tool into my build process.

1

u/feral_claire Aug 10 '20

You probably already have a step to do a docker build. This would replace that (actually it would remove a step if you are integrating the docker build into the maven build and it was a separate step before)

1

u/[deleted] Aug 10 '20

I have it integrated in Azure pipelines, which also pushes my image to a private container registry, and deploys the new version to a Kubernetes cluster. I'm using Payara micro but I'm always checking to see what others are doing and maybe get a little advatange here or there. Thanks!

3

u/snot3353 Aug 10 '20

Spring is a set of a projects and libraries that provide many different capabilities. The most well known are the Spring projects that provide Dependency Injection (AKA Inversion of Control), Web Frameworks and Aspect-Oriented Programming (AOP). There are many beyond this though.

Spring Boot is a project that sits around some Spring Projects and provides Autoconfiguration, Starters and Actuator. It is used to make getting started with Spring projects easier by letting you indicate what you want to do and then automatically using default configurations (until you replace them). Since many Spring projects are complex and have many features, it can sometimes be difficult to get started with them if you have to know how to configure everything up front from scratch. Spring Boot is intended to ease this.

0

u/[deleted] Aug 10 '20

[deleted]

1

u/hoy83 Aug 10 '20

So its for programs that are online and use the web mostly?

1

u/[deleted] Aug 10 '20

Yeah. Like websites etc.

1

u/hoy83 Aug 10 '20

Do you use a front end language in combination with it like html/css and javascript?

0

u/[deleted] Aug 10 '20

Yeah. In works in combination with angular or any other front end languages/frameworks.

1

u/hoy83 Aug 10 '20

Thanks.. so technically it is a back end language then..

1

u/Yithar Aug 10 '20

Spring isn't a language. It's a framework. Java is a language. Also, with Thymeleaf, you don't need to use Angular or React. But most projects use React or Angular rather than Thymeleaf.

0

u/[deleted] Aug 10 '20

It's a java based framework and java is a backend language for sure.

2

u/Yithar Aug 10 '20

No, it doesn't need to be a web app to use Spring. ABasit106 is wrong. That's why he has so many downvotes.

https://www.baeldung.com/inversion-control-and-dependency-injection-in-spring

Spring's core functionality is Inversion of Control and Dependency Injection.