First or the configuration file for XML
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd " > <!--Turn on annotation scanning - <Context:component-scanBase-package= "Com.swift"></Context:component-scan></Beans>
Next , assume that the DAO class
Package Com.swift; Import org.springframework.stereotype.Component; @Component (Value= "DAO") Public class DAO { public String Fun () { return "This is Dao's fun () ..." ; }}
It is convenient to generate an object, even the value= in @component (value= "DAO") can not be written and become
@Component ("DAO")
then the class that assumes the service
PackageCom.swift;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.stereotype.Component; @Component (Value= "Service") Public classService {@AutowiredPrivateDAO DAO; PublicString Fun () {return"This is Service's fun () ..." + "\ r \ n" + This. Dao.fun (); } //Note Using the annotation method, you do not need to generate the setter method yourself. Public voidsetdao (dao dao) { This. DAO =DAO; } }
Use <bean id= "service" class= "Com.swift.Service" ><property name= "DAO" ref= "DAO" in the configuration file ></property> </bean>
Note that after generating two objects, the annotation property
@Autowired
It's done, automatic assembly, automatic connection.
finally , use a servlet to test
PackageCom.swift;Importjava.io.IOException;Importjavax.servlet.ServletException;ImportJavax.servlet.annotation.WebServlet;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; @WebServlet ("/test") Public classServlettestextendsHttpServlet {Private Static Final LongSerialversionuid = 1L; Publicservlettest () {Super(); } protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {response.getwriter (). Append ("Served at:"). Append (Request.getcontextpath ()); ApplicationContext Context=NewClasspathxmlapplicationcontext ("Zhujie.xml"); Service Service= (Service) Context.getbean ("service")); String Test=Service.fun (); Response.getwriter (). append (test); } protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {doget (request, response); }}
The browser results are as follows
This method of automatic loading @Autowired the principle is to find the object defined by the class name, which is not used much, because if more than one object exists, which is injected?
So, using
Another note that you can specify exactly which object to inject
@Resource (name= "DAO")
Private DAO DAO;
This method uses more
Annotation management method of Spring framework context using annotations to inject object properties