Spring Use Tutorial (iii) NOTE 2

Source: Internet
Author: User

Use annotations when Bean has an association relationship between beans

• Automatic assembly of @Autowired annotations has a compatible typeThe single Bean attribute – constructors, normal fields (even non-public), all methods with parameters can be applied @Authwired Annotationsby default, all properties that use @Authwired annotations need to be set. When Spring can't find an exception is thrown when the matching Bean assembly property is If a property is allowed to not be set, you can set the Required property of the @Authwired annotation is false– By default, automatic assembly by type will not work when there are multiple type-compatible beans in the IOC container. At this point you can @QualifierThe name of the Bean is provided in the note. Spring allows each other @Qualifiter The name of the injected Bean specified by the input parameter of the– @Authwired annotations can also be applied to Array Typeattribute, Spring will automatically assemble all matching beans. – @Authwired annotations can also be applied to the Collection Properties, Spring reads the type information for the collection, and then automatically assembles all of the compatible bean.– @Authwired annotations in Java.util.Map, if the map has a key value of String, Spring automatically assembles a bean that is compatible with the map value type, at which point the bean's name is used as the key value
<?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-4.0.xsd ">        <!--Specify packages scanned by the spring IOC container -        <!--Resource-pattern Scan a specified resource -        <!--<context:component-scan base-package= "com.test.spring.annotation" resource-pattern= "Controller/*.class "></context:component-scan> -                <!--exclude-filter child nodes Specify the components that exclude those specified expressions -        <!--include-filter child nodes contain components of those expressions and need to be configured Use-default-filters to False -        <Context:component-scanBase-package= "Com.test.spring.annotation" >         <!--<context:exclude-filter type= "annotation" expression= "Org.springframework.stereotype.Controller"/>             <context:include-filter type= "Annotation" expression= "Org.springframework.stereotype.Controller"/>  -             <!---<context:exclude-filter type= "assignable" expression= "Com.test.spring.annotation.TestObject"/> <context:include-filter type= "assignable" expression= "Com.test.spring.annotation.TestObject"/&G             T  -        </Context:component-scan></Beans>
     Public Static voidMain (string[] args) {ApplicationContext con=NewClasspathxmlapplicationcontext ("Beans_annocation.xml");//testobject to= (testobject) Con.getbean ("Testobject");//System.out.println (to);//        //userservice ser= (userservice) Con.getbean ("UserService");//System.out.println (Ser);//        //Usercontroller ucon= (Usercontroller) Con.getbean ("Usercontroller");        System.out.println (Ucon); Ucon.execute ();//USERREPOSITORYIMLPL ur= (USERREPOSITORYIMLPL) Con.getbean ("Userrepository");//System.out.println (UR);            }
View Code
 PackageCom.test.spring.annotation.controller;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.stereotype.Controller;ImportCom.test.spring.annotation.service.UserService; @Controller Public classUsercontroller {@AutowiredPrivateUserService UserService; //@Autowired can be placed on a method with parameters.     Public voidSetuserservice (UserService userservice) { This. UserService =UserService; }     Public voidExecute () {System.out.println ("Usercontroller");            Userservice.add (); }}
View Code
 PackageCom.test.spring.annotation.service;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.beans.factory.annotation.Qualifier;ImportOrg.springframework.stereotype.Service;Importcom.test.spring.annotation.repository.UserRepository; @Service Public classUserService {@Autowired @Qualifier ("Userrepository")//specify which bean to assemble    Privateuserrepository userrepository; //can unload parameters before     Public voidSetuserrepository (@Qualifier ("Userrepository") Userrepository userrepository) { This. Userrepository =userrepository; }     Public voidAdd () {System.out.println ("UserService's Add");    Userrepository.save (); }}
View Code
 Packagecom.test.spring.annotation.repository;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.stereotype.Repository;ImportCom.test.spring.annotation.TestObject;//annotations can be identified by Value property values, and the bean's name in the IOC container@Repository ("Userrepository") Public classUserrepositoryimlplImplementsuserrepository{@Autowired (Required=false)    PrivateTestobject to;  Public voidSave () {System.out.println ("Save to Implement Interface"); }     }
View Code

Spring Use Tutorial (iii) NOTE 2

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.