Injection injection of Spring annotation container

Source: Internet
Author: User
Tags getmessage

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

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.