Annotation management method of Spring framework context using annotations to inject object properties

Source: Internet
Author: User

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

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.