Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC : how does Spring Bean work?

Tags:

spring-mvc

For 2 months now, I decided to develop a web application according to the MVC pattern.

I come from a program for the embedded (C/C++) so I'll get step by step to the Java.

Until now I was doing all my app development by hand i-e I had all of my Servlets in a package, my JavaBean in a package, my (JPA) Entities in another package and the same for the DAO layer. So far so good, I understand what I am doing and everything works well.

To "facilitate" me the development and doing things faster, I decided to use Spring MVC (I heard good of this framework) but here I meet some problems of understanding on the concept of the Spring Beans.

Here are my questions about this:

What is a Spring Bean? (I have already read this but it's not tell me how using it?) (For me a Bean was exclusively a simple Java class which respects some rules but when I look at a Spring Bean it is totaly different, I saw some Spring codes where their beans returned an object, why did they call it "Bean", I am confused), so.

Where are Spring Beans involved in the workflow of SpringMVC?

Where in the application should I place Spring Beans?

How and when to use it?

I am using the JavaBeans on the business part of my application, example to retrieve the fields from a form, is it possible to do the same thing with the Spring Beans?

Since the configuration of Spring Bean seems to met difficult (because I have trouble understanding the mechanism behind) can we make a web application with Spring MVC without using a single Spring bean (except for the DispatcherServlet, of course)?

Why use a SpringBean instead of a Java Bean?

What more does a Spring Bean?

I realize that Spring MVC is not as simple to handle, there are many things to master and especially a lot of configurations.

Where to start?

Should I give up using Spring MVC?

I have searched the web to understand but I can't find anything that explains things in detail.

Thank you.

PS : Sorry for my english, I live in France.

like image 884
akuma8 Avatar asked Dec 11 '22 14:12

akuma8


2 Answers

The java bean and spring-managed beans are different things. Java bean is indeed some object with particular naming convention where spring bean is different.

What spring refers to as (spring managed) bean is simply instance of a class which lifecycle is managed by Spring framework. It doesn't have to follow the Java bean naming conventions etc. The most straight-forward usage of a bean is dependency injection. You can inject such spring-manged object (bean) into other beans - like controllers for example.

You have several ways how to create a bean. It's nicely described in the doc.

Note that from spring standpoint even a the controller is just another bean which happen to have some extra features.

To get started I would go with some spring-boot sample application. It saves you a lot of configuration and it's generally good starting point.

like image 193
Jan Zyka Avatar answered Dec 30 '22 13:12

Jan Zyka


Lemme try explaining the point of Spring Framework first.

Spring is based on the concept of inversion of control (IoC). This concept basically means functional beans express their dependencies in interfaces, and that something (in this case Spring) is responsible for injecting the correct implementations.

For example assume I have a UserRepository (interface). I have two implementations, a MySqlUserRepository and a MongoDbUserRepository, since my customers can opt for one of those two storage technologies. Assume now I have multiple classes that consume UserRepository. To avoid having them all have to know which impl we're using, the consumers express only that they need a UserRepository (usually via an @Inject or @Autowired annotation), and I leverage this framework to inject the correct one at initialization time.

This is the point of Spring Framework. These objects (UserRepository impl and consumers) are Spring-managed-beans, or, more succinctly, Spring Beans. Spring manages their lifecycle and wires them together.

Spring MVC is an MVC framework built on Spring Framework. That is to say your MVC artefacts (controllers, etc...) are Spring Beans.

Spring should typically manage all of your non-stateful objects(there are cases where there may be a good reason not to do this, but that's beyond the scope of this answer). Your stateful objects are what your non-stateful objects manage. Spring manages the objects that manage your state.

Spring is a pretty powerful framework with lots of add-ons, and most packages in the java ecosystem will have, at a minimum a tutorial on how to get them working with Spring, if not out of the box integration.

like image 41
Taylor Avatar answered Dec 30 '22 13:12

Taylor