Java common classes get spring bean at any position

Source: Internet
Author: User

Method 1: Save the ApplicationContext object during initialization
Code:
ApplicationContext ac = new FileSystemXmlApplicationContex ("applicationContext. xml ");
Ac. getBean ("beanId ");
Note:
This method is applicable to standalone applications that adopt the Spring framework and require the program to manually initialize Spring through the configuration file.
 
Method 2: Obtain the ApplicationContext object through the tool class provided by Spring.
Code:
Import org. springframework. web. context. support. WebApplicationContextUtils;
ApplicationContext ac1 = WebApplicationContextUtils. getRequiredWebApplicationContext (ServletContext SC)
ApplicationContext ac2 = WebApplicationContextUtils. getWebApplicationContext (ServletContext SC)
Ac1.getBean ("beanId ");
Ac2.getBean ("beanId ");
Note:
This method is suitable for B/S systems using the Spring framework. The ApplicationContext object is obtained through the ServletContext object, and then the desired class instance is obtained through it. The difference between the two tools is that the former throws an exception when the acquisition fails, and the latter returns null.

In this example, the servletContext SC can be changed to servlet. getServletContext (), this. getServletContext (), or request. getSession (). getServletContext ();
In addition, because spring is the injected object in ServletContext, you can directly retrieve the WebApplicationContext object in ServletContext:
WebApplicationContext webApplicationContext = (WebApplicationContext) servletContext. getAttribute (WebApplicationContext. ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE );

 
Method 3: inherit from the abstract class ApplicationObjectSupport
Note:
The abstract class ApplicationObjectSupport provides the getApplicationContext () method to conveniently obtain ApplicationContext. During Spring initialization, The ApplicationContext object is injected through the setApplicationContext (ApplicationContext context) method of this abstract class.
 
Method 4: inherit from the abstract class WebApplicationObjectSupport
Note:
Call getWebApplicationContext () to obtain WebApplicationContext.
 
Method 5: implement the ApplicationContextAware Interface
Note:
Implement the setApplicationContext (ApplicationContext context) method of this interface and save the ApplicationContext object. During Spring initialization, The ApplicationContext object is injected using this method.
The preceding method is applicable to different situations. Select the appropriate method based on the actual situation.
It is worth mentioning that the classes used in the above method in the system are actually tightly coupled with the Spring framework, because these classes know that they are running on the Spring framework, therefore, such applications should be minimized in the system so that the system is as independent as possible from the current operating environment and the desired service provider should be obtained through DI.
Method 6:
Source code discovery
[Java]
Public class ContextLoaderListener extends ContextLoader implements ServletContextListener

In ContextLoader
[Java]
Public static WebApplicationContext getCurrentWebApplicationContext (){
ClassLoader ccl = Thread. currentThread (). getContextClassLoader ();
If (ccl! = Null ){
WebApplicationContext ccpt = currentContextPerThread. get (ccl );
If (ccpt! = Null ){
Return ccpt;
}
}
Return currentContext;
}

WebApplicationContext webctx = ContextLoader. getCurrentWebApplicationContext ();
String path = webctx. getServletContext (). getRealPath ("xls ");
Tcyclervice tp = webctx. getBean ("assumervice", tcyclervice. class );


Author: huang798807481

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.