How non-Spring managed objects get to a spring managed object

Source: Internet
Author: User

Some thread classes or Servlets cannot call the classes inside the spring container by using spring annotations
Try to change the thread or servlet plus @component or @controller annotations to be managed by the spring container, and then call the other classes inside the Spring container and fail!
Finally, find the following two solutions:


One, initialize with spring config file applicationcontext.xml

[Java] View plaincopy
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.FileSystemXmlApplicationContext;

public class springutil{
private static ApplicationContext ApplicationContext = null;

public static ApplicationContext Getapplicationcontext () {
if (ApplicationContext = = null) {
ApplicationContext = new Filesystemxmlapplicationcontext ("Applicationcontext.xml");
}
return applicationcontext;
}
}
Disadvantages:
The Applicationcontext.xml is a full file path, which is one of the reasons that this is not very flexible,
Calling Springutil creates a ApplicationContext object once. If you change static to non-static, then each time you call the method, the object will be new, and the efficiency should be worse.



Second, by implementing the Applicationcontextaware interface

At spring initialization, the ApplicationContext object is injected into the class through the Setapplicationcomtext method implemented by the interface, as shown below
My solution is to choose this type of method:

1. Define Springutil

Import org.springframework.beans.BeansException;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.ApplicationContextAware;

public class Springutil implements Applicationcontextaware {

private static ApplicationContext ApplicationContext;

public static ApplicationContext Getapplicationcontext () {
return applicationcontext;
}

public void Setapplicationcontext (ApplicationContext applicationcontext) throws Beansexception {
Springutil.applicationcontext = ApplicationContext;
}

}

2, spring configuration file to join

<bean id= "ApplicationContext" class= "Com.lefu.pushCore.other.SpringUtil"/>

3. All the objects managed by spring can be obtained by the following methods:

Pushcontroller Pushcontroller = (pushcontroller) springutil.getapplicationcontext (). Getbean ("PushController");




Reference article: http://blog.csdn.net/a19881029/article/details/7842070

How non-Spring managed objects get to a spring managed object

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.