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