In spring MVC, the use of a service in controller is just a matter of using the annotation @resource/@Autowired, but a generic class (a class that does not use @controller annotations) needs to use the service. The service in spring is not as new as you would like to be new, because an object instantiated by new is detached from the spring container's management, gets no attribute value to the annotation, and therefore is null. Even if a @component annotation in the class that invokes the service is added to the spring container management, it is still null.
Instance:
@Component public
class Test {
@Resource (name = "Testserviceimpl")
private sessionservice testservice;
The public Test () {} is public
void Run () {
System.out.println ("* * *");
System.out.println (Testservice);
}
The new object in controller.
Test test = new test ();
Test.run ();
With Debug you can get test to be null
The following methods are available:
Common and tool-like solutions:
The generic class here refers to classes that are not managed by the spring container, which are instantiated by new.
1, Springcontextutil
Package com.test.framework.utils;
Import org.springframework.beans.BeansException;
Import org.springframework.beans.factory.NoSuchBeanDefinitionException;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.ApplicationContextAware; public class Springcontextutil implements Applicationcontextaware {private static ApplicationContext Applicationcon Text Spring applies the context//The following method adds a @override annotation because inheriting the Applicationcontextaware interface is a method that must be implemented @Override public void Setap Plicationcontext (ApplicationContext applicationcontext) throws Beansexception {SPRINGCONTEXTUTIL.APPL
Icationcontext = ApplicationContext;
public static ApplicationContext Getapplicationcontext () {return applicationcontext;
public static Object Getbean (String name) throws Beansexception {return Applicationcontext.getbean (name);
public static Object Getbean (String name, Class requiredtype) Throws Beansexception {return Applicationcontext.getbean (name, requiredtype);
public static Boolean Containsbean (String name) {return Applicationcontext.containsbean (name); public static Boolean Issingleton (String name) throws Nosuchbeandefinitionexception {return Applicationco
Ntext.issingleton (name); public static Class GetType (String name) throws Nosuchbeandefinitionexception {return Applicationconte
Xt.gettype (name); public static string[] Getaliases (String name) throws Nosuchbeandefinitionexception {return Applicationco
Ntext.getaliases (name); }
}
2, the spring configuration file Application.xml in the following configuration
<bean id= "Springcontextutil" class= "Com.test.framework.utils.SpringContextUtil" scope= "Singleton" ></bean >
3. Use
Dictservice Dictservice = (dictservice) springcontextutil.getbean ("Dictservice");
List<dict> dict = (list<dict>) dictservice.findbyhql (HQL);
Two, the tool class individual solution
Direct injection is not possible if we want to inject a service or mapper interface in our own encapsulated Utils tool class or in a controller generic class using @autowired annotations, because Utils uses a static method, We can not use the Non-static interface directly, when we encounter such a problem, we must find a way to solve.
The Spring Bean initializes and destroys the bean in the latter three ways, but the third one does not conform to this because Utils does not inherit or declare interfaces.
1. Annotation mode:
@Component public
class Testutils {
@Autowired
private itemservice itemservice;
@Autowired
private Itemmapper itemmapper;
public static testutils testutils;
@PostConstruct public
void init () {
testutils = this;
}
Example of a method using the service and Mapper interfaces in the Utils tool class, use the "testutils.xxx. Method" To have public
static void Test (Item record) {
TestUtils.itemMapper.insert (record);
TestUtils.itemService.queryAll ();
}
We can use the following annotations in the Init method, and the Init () method of time can be defined by itself, note: the Inti () method only needs a line of code, with me is absolutely OK, do not look at the other people on the Internet.
2. XML configuration mode:
We can remove the @postconstruct annotation on the init () method, and configure the following beans in the spring-comtext.xml so that there is nothing to write about, it is very simple.
<bean id= "testutils" class= "write the package full path name Utils class" init-method= "Init" ></bean>
Appendix: Another way to initialize and destroy a spring bean
Implements Initializingbean, Disposablebean these two interfaces, and Afterpropertiesset () and Destroy () methods
The sample code is as follows:[Java] View Plain copy public class initializingdisposableinit implements initializingbean, DisposableBean { @Override public void destroy () throws Exception { SYSTEM.OUT.PRINTLN ("Executive Initializingdisposableinit: destroy"); } @Override public void Afterpropertiesset () throws Exception { &NBSP;SYSTEM.OUT.PRINTLN ("Executive Initializingdisposableinit: afterpropertiesset"); } public static Void main (String[] arGS) { ClassPathXmlApplicationContext Context = new classpathxmlapplicationcontext ( "Classpath:beans-impl.xml"); context.close (); } }