Premise: Spring has been deployed (refer to the previous article: Web project Deployment Spring Bean Factory)
One: New dependent jar Package
1:spring-aop-4.1.6.release.jar (required when modifying beans attribute in Applicationcontext.xml)
Two: Web. xml unchanged, modify Applicationcontext.xml
1: Remove DTD (<! DOCTYPE beans Public "-//spring//dtd BEAN 2.0//en" "Http://www.springframework.org/dtd/spring-beans-2.0.dtd" >)
2: New beans Attribute (xmlns,xmlns:xsi,xmlns:context,xsi:schemalocation)
3: Remove <bean/> container injection, change to annotation scan injection
4: Remove the association injection between <bean/>, modify it as annotation auto-injection
Three: Spring container class with annotation injection
1: Inject spring container (@Component, @Controller, @Service, @Repository)
The 2:spring container is injected into the bean (@Resource (name= "xxx"), @Autowired)
Ps:
1:applicationcontext.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-2.5 . xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/s Pring-context-2.5.xsd "> <Context:annotation-config/> <!--Insert a container according to the annotation scan class - <Context:component-scanBase-package= "Dao,service" /> <!--Upgrade: Annotations, auto-injection - <Beanclass= "Org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /></Beans>
2:servlet unchanged, service (STUDENTSERVICEIMPL)
PackageService;ImportJavax.annotation.Resource;ImportOrg.springframework.stereotype.Service;ImportDAO. Studentdao;Importentity. Student; @Service ("Studentservice") Public classStudentserviceimplImplementsStudentservice {@Resource (name= "a") PrivateStudentdao Studentdaot; Public voidinsertstudent (Student Student) {Studentdaot.insert (Student); }}
3:dao (Studentdaoimpl)
PackageDAO;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.SQLException;Importjava.sql.Statement;ImportJava.text.SimpleDateFormat;Importorg.springframework.stereotype.Repository;Importentity. Student; @Repository (A) Public classStudentdaoimplImplementsStudentdao { Public voidInsert (Student Student) {Try{class.forname (driver); Connection Con=drivermanager.getconnection (Url,username,password); Statement SQL=con.createstatement (); SimpleDateFormat SDF=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); Sql.execute ("Insert student (Name,math,create_time) VALUES ('" +student.getname () + "', '" +student.getmath () + "', '" +sdf.format ( Student.getcreatetime ()) + "')"); Sql.close (); Con.close (); }Catch(java.lang.ClassNotFoundException e) {System.err.println ("ClassNotFoundException:" +e.getmessage ()); }Catch(SQLException ex) {System.err.println ("SQLException:" +ex.getmessage ()); } }}
Injection injection of Spring annotation container