Summary of spring bean method in Java

Spring is a lightweight inversion of control (IOC) and aspect oriented (AOP) container framework. How to obtain spring configured beans in the program?

Bean factory (COM. Springframework. Beans. Factory. Beanfactory) is the core interface of spring framework. It provides high-level IOC configuration mechanism. Beanfactory makes it possible to manage different types of Java objects and application context (COM. Springframework. Context. ApplicationContext) is based on beanfactory and provides more application-oriented functions. It provides internationalization support and framework event system, making it easier to create practical applications. We generally call beanfactory IOC container and ApplicationContext application context. However, sometimes we also use applicationcon for convenience of writing Text is called the spring container.

For the purposes of the two, we can simply divide them: beanfactory is the infrastructure of the spring framework, which is oriented to spring itself; ApplicationContext is for developers who use the spring framework. In almost all applications, we directly use ApplicationContext instead of the underlying beanfactory.

There is a significant difference between the initialization of ApplicationContext and beanfactory: beanfactory does not instantiate a bean when initializing the container, and does not instantiate the target bean until a bean is accessed for the first time; ApplicationContext instantiates all single instance beans when initializing the application context. Therefore, the initialization time of ApplicationContext will be slightly longer than that of beanfactory

This article does not involve automatic injection through @ resource and @ Autowired, but only obtains the beans in the spring configuration file through ApplicationContext.

To get the bean configured in XML, the key is to get org springframework. context. ApplicationContext

The first method to obtain ApplicationContext:

perhaps

Instantiating ApplicationContext in this way is very time-consuming. This method is suitable for independent applications using the spring framework. It is only recommended when the program needs to manually initialize spring through the configuration file. The main implementation classes of ApplicationContext are classpathxmlapplicationcontext and filesystemxmlapplicationcontext. The former loads the configuration file from the classpath by default, and the latter loads the configuration file from the file system by default

example:

On the web XML writes a servlet, automatically starts, and calls BeanManager in the init method.

Using beanmanager. XML in Java code getBean(String beanId); To get the bean instance.

The second method to obtain ApplicationContext: obtain the ApplicationContext object through the tool class provided by spring. It is a method specially customized for web projects. It is recommended to be used in web projects. For example:

Through javax servlet. ServletContext obtains the ApplicationContext instance object, which means that request, session, etc. must be used.

In this way, the ApplicationContext object cannot be set as a member variable. In each specific method, you need to obtain the ServletContext through request, session, etc., and then obtain the ApplicationContext instance.

Therefore, this method is only recommended for web projects that can obtain ServletContext objects and do not need to define ApplicationContext objects as member variables.

Note: when using webapplicationcontextutils to obtain the ApplicationContext instance, you need to Add org. XML to the configuration file springframework. web. context. Contextloaderlistener listener, otherwise the ApplicationContext object cannot be obtained and null is returned.

Configuration file: Web xml

3. Inherit from the abstract class applicationobjectsupport

Abstract class applicationobjectsupport provides getapplicationcontext () method, which can easily get ApplicationContext. During spring initialization, the ApplicationContext object will be injected through the setapplicationcontext (ApplicationContext) method of the abstract class.

4. Inherit from the abstract class webapplicationobjectsupport

By inheriting org springframework. web. context. support. Webapplicationobjectsupport uses getwebapplicationcontext() to get org springframework. web. context. Webapplicationcontext because web applications have more features than ordinary applications, webapplicationcontext extends ApplicationContext. Webapplicationcontext defines a constant root_ WEB_ APPLICATION_ CONTEXT_ Attribute: when the context is started, the webapplicationcontext instance is placed in the attribute list of ServletContext with this key. Therefore, we can directly obtain the webapplicationcontext from the web container through the following statement:

5. Implement the interface applicationcontextaware

Implement the setapplicationcontext (ApplicationContext context) method of the interface and save the ApplicationContext object. When spring initializes, the ApplicationContext object will be injected through this method.

The third, fourth and fifth methods all need to configure classes in the spring configuration file:

Otherwise, the ApplicationContext will not be obtained and null will be returned.

The above content introduces you to the summary of spring bean method in Java. I hope you like it.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>