Spring annotation Method Injects bean

Source: Internet
Author: User

adding annotations to the Bean,spring configuration file also adds some constraints and the package in which the annotation is imported

Applicationcontext.xml

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "Http://www.springframework.org/schema/beans"3 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4 Xmlns:context= "Http://www.springframework.org/schema/context"5 xsi:schemalocation= "Http://www.springframework.org/schema/beans6 http://www.springframework.org/schema/beans/spring-beans.xsd7 Http://www.springframework.org/schema/context8 http://www.springframework.org/schema/context/spring-context.xsd ">9 Ten     <Context:component-scanBase-package= "Com.xiaostudy.service"></Context:component-scan> One     <Context:component-scanBase-package= "Com.xiaostudy.dao"></Context:component-scan> A </Beans>

Bean class Personimple.java injected with annotations

1  PackageCom.xiaostudy.service;2 3 Importjavax.annotation.PostConstruct;4 ImportJavax.annotation.PreDestroy;5 ImportJavax.annotation.Resource;6 7 ImportOrg.springframework.beans.factory.annotation.Value;8 ImportOrg.springframework.context.annotation.Scope;9 Importorg.springframework.stereotype.Component;Ten /* One * Bean Injection A * 1, @Component ("id") - * 2, WEB: - * @Controller ("id") Web the * @Service ("id") Service - * @Repository ("id") DAO -  */ -@Component ("Person") + /* - * Bean Scope + * @Scope ("singleton") single case (also default) A * @Scope ("prototype") is a new object once each new at  */ -@Scope ("Prototype") -  Public classPersonimpleImplementsPerson { -     /* - * Bean parameter injection - * 1, common parameters (i.e. those basic data types) in * @Value ("value") above the parameter or in the corresponding set method - * 2, reference parameters to * 2.1 Injected by type: @Autowired + * 2.2 Injected by name: @Autowired @Qualifier ("id") - * 2.3 Injected by name: @Resource the      *  *      */ $     //@ResourcePanax Notoginseng     Privatedao_demoimple Dao_demo; -@Value ("Xiaostudy") the     PrivateString name; +      A  the      PublicString GetName () { +         returnname; -     } $  $      Public voidsetName (String name) { -          This. Name =name; -     } the  -      Publicdao_demoimple Getdao_demo () {Wuyi         returnDao_demo; the     } -  Wu @Resource -      Public voidSetdao_demo (dao_demoimple dao_demo) { About          This. Dao_demo =Dao_demo; $     } -     /* - * Bean initialization method Annotations - * @PostConstruct A      */ + @PostConstruct the      Public voidinit () { -System.out.println ("Init () >>>>>>"); $     } the      the     /* the * Bean Destruction Annotations the * @PreDestroy -      */ in @PreDestroy the      Public voiddestroy () { theSystem.out.println ("Destroy () >>>>>"); About     } the      the}

Person interface

1  Package Com.xiaostudy.service; 2 3  Public Interface Person {45 }

Dao_demo interface

1  Package Com.xiaostudy.dao; 2 3  Public Interface Dao_demo {45 }

Dao_demoimple.java

1  PackageCom.xiaostudy.service;2 3 ImportOrg.springframework.beans.factory.annotation.Value;4 Importorg.springframework.stereotype.Component;5 6 ImportCom.xiaostudy.dao.Dao_demo;7@Component ("Dao_demo")8  Public classDao_demoimpleImplementsDao_demo {9     Ten     PrivateString name; One      A      PublicString GetName () { -         returnname; -     } the  -@Value ("Demo") -      Public voidsetName (String name) { -          This. Name =name; +     } -  +      Public voidAdd () { ASystem.out.println ("add>>>>>>"); at     } -      -}

Test class Test.java

1  PackageCom.xiaostudy.service;2 3 ImportOrg.springframework.context.ApplicationContext;4 ImportOrg.springframework.context.support.AbstractApplicationContext;5 ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;6 7 ImportCom.xiaostudy.dao.Dao_demo;8 ImportCom.xiaostudy.service.Person;9 Ten  Public classTest { One  A      Public Static voidMain (string[] args) { -ApplicationContext AC =NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); -Person person = ac.getbean (' person ', person.class); the System.out.println (person); -Personimple pi =(personimple) person; - System.out.println (Pi.getname ()); -Dao_demoimple Dao_demo =Pi.getdao_demo (); + System.out.println (Dao_demo); - Dao_demo.add (); + System.out.println (Dao_demo.getname ()); A         /*Person Person1 = Ac.getbean ("person", person.class); at Person Person2 = Ac.getbean ("person", person.class); - System.out.println (person1); - System.out.println (person2); - ( (Abstractapplicationcontext) AC). Close ();*/ -     } -  in}

Spring annotation Method Injects bean

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.