There are three ways to create a bean, creating a static factory, an instance factory, and a constructor. What are the differences between these three methods?

1.Spring creates beans

The main process is to create an xml file, then set the initial value of the bean component through this xml file, and obtain the xml configuration file through reflection in the code. The advantage is that you only need to modify the xml file instead of modifying the code. The disadvantage is that when your project is large, there are too many xml configuration files, which are bloated and not conducive to object management, because you directly obtain its instance by configuring this bean. To put it bluntly, we are creating the object now. The process is handed over to the spring container, and its instance can be obtained directly without worrying about how it was created.

2. Create using static factory, instance factory, and constructor

        First of all, the results of the creation are the same. The time to load the configuration file has already created examples, so the main difference is that the creation method is different;

        The default no-argument construction is to hand over the specified class to Spring's own factory BeanFactory for management;

        In a static factory, the factory itself does not need to create a factory object, but is called through a static method; to create a bean through a static factory is to hand over the static factory to Spring management, and then let Spring call the static method of the object created in the static factory. The jar package suitable for third parties does not have a constructor method. It may be private, so you cannot use the constructor to create it. At this time, you can only create it using the static method it provides. At the same time, you can hand over the class in the third-party jar to spring. To manage, because Spring creates objects through reflection. Reflection can set accessible to true, so that objects can be created bypassing the permission mechanism. For example, private permissions cannot create objects, but after setting this, objects can be created. , you can use the static factory provided by Spring to create the configuration of the instance.

        Instance factory, the factory itself needs to create a factory object, and a new factory instance needs to be created first, so the bean is created through the instance factory, which means that the static factory is handed over to Spring management, and the process of creating the instance is also handed over to Spring management, which is using the getBean method. , Spring helps you create instance factory objects. Integrate factory classes that have not been integrated with SPring. Using this method, we use code management ourselves. For example, Spring's integration of Hibernate is implemented in this way.

Summary: To put it simply, it is mainly created using structures.

Annotation yyds below

Use @Component (mark component), @Service (mark service layer),@Repository (Mark entity class), @Controller (Mark Web component)

Controller is one of the five major components of sping MVC, which are - - - >

(1) DispatcherServlet This is the entrance to the entire spring mvc. After the front-end view layer sends the request to the background, it will let DispatcherServlet distribute the request, which is like a command, directing spring to which Controller should send the front-end request for processing.

(2) HandlerMapping, this is specifically responsible for request dispatch, that is, DispatcherServlet will find a bean and ask it which controller it should hand the request to. This bean is HandlerMapping.

(3) Controller is a real processor that handles front-end requests. It should be very familiar.

(4) ModelAndView encapsulates model information and view information. Model is the data you want to bring to the front end, and View is the view you want to return, such as an index page. If you want to return data in Json format, you should use the @RestController annotation to add it to the class, or add @ResponseBody to the method to automatically convert the returned data into Json. This is used more often, because current projects are generally front-end and back-end. They are separated. The front-end and back-end interactions generally use data in Json format.

(5) ViewResolver view processor ultimately locates the page, which is to find where the page specified by the Controller is and return a page to the front end)

Summary: These annotations all define beans, which means that a bean will be automatically generated for you in the place where the annotation is placed. By default, they are all in singleton mode, so be careful not to have duplicate names, otherwise it will prompt you that there are already duplicates. Name, the common bug is that the dependencies you introduce are in different packages and implement different functions, but if they all have the same bean, they will conflict and report errors. This is more often encountered.

        Use the @Bean annotation to mark this as a Bean. It is simple and fast. It is a commonly used method. Use it with @Configuration. Put @Configuration on the class and mark this as a Spring boot configuration class. It will automatically scan the class for @ Bean annotation method, add it to the Spring container, and automatically generate objects.

Guess you like

Origin blog.csdn.net/LegendaryChen/article/details/127054479