Spring Finishing Series (--spring) dependency injection, assembling dependencies between objects

Source: Internet
Author: User

This article has compiled the implementation of spring container registration bean, this article will collate the implementation of spring injection bean, most of the articles are introduced according to the latitude of setter and constructor injection, this article will follow how to use setter and the latitude of the constructor to organize, that is, XML configuration injection, XML configuration automatic assembly, annotation automatic assembly three ways, Hope said not verbose. XML Configuration injection, manual assembly, providing setter methods or constructor constructors

1, need to provide setter method way:

XML configuration file for spring:

    <bean id= "Userdao" class= "Com.jsun.test.springDemo.ioc.User.UserDaoImpl" ></bean>

    <bean id= " UserService "class=" Com.jsun.test.springDemo.ioc.User.UserServiceImpl ">
        <!--Configuration Injection Properties--
        < Property Name= "Userdao" ref= "Userdao" ></property>
    </bean>

Userserviceimpl:

    Declares the Userdao attribute, whose name is not associated with the name of the property in XML, such as Userdaoalias
    private Userdao Userdao;

    Setter method, where the setxxxx xxxx is to match the value of the name attribute in the property tag in the XML file, with the first letter capitalized, with the associated public
    void Setuserdao (Userdao Userdao) {
        System.out.println ("Setuserdao injection");
        This.userdao = Userdao;
    }

2, the need to provide a constructor mode:

XML configuration file for spring:

    <bean id= "Userdao" class= "Com.jsun.test.springDemo.ioc.User.UserDaoImpl" ></bean>

    <bean id= " UserService "class=" Com.jsun.test.springDemo.ioc.User.UserServiceImpl ">
        <!--Configuration Injection Properties--
        < Constructor-arg name= "Userdao" ref= "Userdao" ></constructor-arg>
    </bean>

Userserviceimpl:

    Declares the Userdao property, which is independent of the Name property value in the <constructor-arg> tag
    private Userdao Userdao;

    The parameter name is associated with the name attribute value in the <constructor-arg> label public
    Userserviceimpl (Userdao userdao) {
        System.out.println ("constructor Userserviceimpl injection");
        This.userdao = Userdao;
    }
Second, XML configuration injection, automatic assembly, provide setter method or constructor constructor

Spring provides three ways to assemble automatically, either by adding the Autowire attribute to the bean label or by adding a global default-autowire property in beans, with the following property values:

1. ByName: automatically assemble other beans whose attributes have the same name into the corresponding attributes of the bean

XML configuration file for spring:

    <bean id= "Userdao" class= "Com.jsun.test.springDemo.ioc.User.UserDaoImpl" ></bean>

    <!-- Register beans and automatically assemble all property beans--
    <bean id= "UserService" class= " Com.jsun.test.springDemo.ioc.User.UserServiceImpl "autowire=" ByName "></bean>

Userserviceimpl:
As in the previous 1, the setter method needs to be provided in the same way that requires setter method

    Declares that the Userdao attribute is not associated with the
    private Userdao Userdao;

    Setter method, where the setxxxx xxxx is to match the value of the name attribute in the property tag in the XML file, with the first letter capitalized, with the associated public
    void Setuserdao (Userdao Userdao) {
        System.out.println ("Automatic assembly Byname-setuserdao Injection");
        This.userdao = Userdao;
    }

2, Bytype: the same type of beans with the attributes of other beans automatically assemble the corresponding attributes of the bean, if there are more than one type of bean, then throw an exception, and that the bytype can not be used for automatic assembly; Nothing happens if a matching bean is not found

XML configuration file for spring:

    <bean id= "Userdao" class= "Com.jsun.test.springDemo.ioc.User.UserDaoImpl" ></bean>

    <!-- Register beans and automatically assemble all property beans--
    <bean id= "UserService" class= " Com.jsun.test.springDemo.ioc.User.UserServiceImpl "autowire=" Bytype "></bean>

Userserviceimpl:

The Bytype method also relies on setter methods, but is no longer associated with setter method names, but is associated with setter parameter types, which must be the type or parent type of the injected bean

    Declares that the Userdao attribute name is not associated with the
    private Userdao Userdao;

    The parameter type of the setter method must be the type of the injected bean or the parent type public
    void Setuserdao (Userdao userdao) {
        System.out.println (" Automatic assembly Bytype-setuserdao Injection ");
        This.userdao = Userdao;
    }


Note: In the application context (Spring container), if there are multiple bean types that match the bean's automatic assembly properties, an error occurs because the Bytype method only allows matching a bean of the same type.

Spring provides two choices, either to set a preferred bean or to exclude some beans.

2.1), set the preferred bean:
The primary property of the <bean> element represents whether it is the preferred bean, and if the label is true, the bean will be the preferred bean, but spring defaults to true for each bean's primary property. Therefore, if you need to set the preferred bean, you need to label those non-preferred beans with the primary property as false.

XML configuration file for spring:

    <!--register Userdaoimpl as a non-preferred bean--
    <bean id= "Userdao" class= " Com.jsun.test.springDemo.ioc.User.UserDaoImpl "primary=" false "></bean>

    <!-- Register USERDAOIMPL2 as the preferred bean--
    <bean id= "UserDao2" class= "COM.JSUN.TEST.SPRINGDEMO.IOC.USER.USERDAOIMPL2" Primary= "true" ></bean>

    <!--settings are automatically assembled Bytype-
    <bean id= "UserService" class= " Com.jsun.test.springDemo.ioc.User.UserServiceImpl "autowire=" Bytype "></bean>

Userserviceimpl: The code does not change, the USERDAOIMPL2 instance of the Save method is executed.

2.2), exclude some beans:
In the process of automatic assembly, when you want to exclude certain beans, use the Autowire-candidate property to False.

XML configuration file for spring:

    <!--register Bean--
    <bean id= "Userdao" class= "Com.jsun.test.springDemo.ioc.User.UserDaoImpl" ></bean >

    <!--register Bean, but the bean is automatically assembled as a property when set to False, so the bean is not automatically assembled as a property--
    <bean id= "UserDao2" class= " Com.jsun.test.springDemo.ioc.User.UserDaoImpl2 "autowire-candidate=" false "></bean>

    <!-- Set the automatic assembly mode to Bytype--
    <bean id= "UserService" class= "Com.jsun.test.springDemo.ioc.User.UserServiceImpl" Autowire= "Bytype" ></bean>

Userserviceimpl: The code does not change, the Userdaoimpl instance of the Save method is executed.



3, constructor: The constructor of the bean with the same type of other beans are automatically assembled into the corresponding parameters of the constructor, if the container does not find the same type of constructor parameter of the bean, then throw an exception

XML configuration file for spring:

    <bean id= "Userdao" class= "Com.jsun.test.springDemo.ioc.User.UserDaoImpl" ></bean>

    <!-- Register beans and automatically assemble all property beans--
    <bean id= "UserService" class= "Com.jsun.test.springDemo.ioc.User.UserServiceImpl "Autowire=" ></bean> "constructor"



Userserviceimpl:

    Private Userdao Userdao;

    Constructor automatic assembly depends on the constructor parameter type, regardless of the name, the type must be the type of the injected bean or the parent type public
    Userserviceimpl (Userdao Userdao) {
        SYSTEM.OUT.PRINTLN ("Automatic assembly constructor Userserviceimpl injection");
        This.userdao = Userdao;
    }




4, add: Set the global default automatic assembly

If in one configuration file all the assemblies use the same automatic configuration, it is troublesome to add a Autowire property to each bean declaration, and to avoid such duplication, the default automatic assembly can be used.

The default automatic assembly method is to add an Default-autowire property to the element, but if the Autowire property is set directly on the bean, the default auto-assemble configuration is overwritten.

The Default-autowire property value is three, byname, Bytype, and constructor, just like the Autowire property value set by the individual bean above.

XML configuration file for spring:

  <beans ... Default-autowire= "ByName" >
</beans>

Userserviceimpl: refer to the form of the individual configuration bean above, the annotation is automatically assembled, can not provide setter method or constructor constructor

Before using the annotation assembly, the first way to open the annotation assembly is to add the following sentence in the configuration file <context:annotation-config></context:annotation-config> You can also use the <context:component-scan> label (the difference between the two is in the Spring finishing series (--spring), the bean scan and the registration implementation method mentioned) label.

Using @autowired annotations, automatic assembly can be implemented using attributes, setter methods, and constructor construction methods.

XML configuration file for spring:

    <!--turn on annotation configuration

    -<context:annotation-config></context:annotation-config> <!--register Bean- -
    <bean id= "Userdao" class= "Com.jsun.test.springDemo.ioc.User.UserDaoImpl" ></bean>

    <! --Register bean--
    <bean id= "UserService" class= "Com.jsun.test.springDemo.ioc.User.UserServiceImpl" ></ Bean>

Userserviceimpl:

1, the annotation on the setter method:

    Private Userdao Userdao;

    Add annotations, match injection is related to parameter type, independent of parameter name, setter method name, Member variable property name
    @Autowired public
    void Setuserdao (Userdao Userdao) {
        System.out.println ("Annotation Automatic assembly Setuserdao injection");
        This.userdao = Userdao;
    }

2, constructor construction method:

    Private Userdao Userdao;

    Add annotations, match injection is related to parameter type, independent of parameter name, Member variable property name
    @Autowired public
    Userserviceimpl (Userdao Userdao) {
        System.out.println ("Annotation automatic assembly constructor Userserviceimpl injection");
        This.userdao = Userdao;
    }

3. On the attributes of the declaration:

    Add annotations, followed by the attribute name matching injection, if not found then by the attribute type matching injection
    @Autowired
    private Userdao Userdao;


Use @autowired to throw exceptions and use with @qualifier annotations view: Spring finishing Series (ten)--@Autowired annotation automatic assembly, binding @qualifier filtering, and differences from @resource annotations


4. Supplement: @Value annotate basic type data or dynamic assembly data

    @Value ("I am the value injected into the strvalue attribute")
    private String strvalue;

Use expressions to dynamically calculate and assemble values of properties, such as using a spel expression to get a value from a system property

@Value ("#{systemproperties.myfavoritesong}")
private String strvalue;

You can also get the *.properties resource file data that is loaded in the spring context, just as you would with an El expression:

@Value ("${jdbc.url}")
private String URL;

Section reference article: Http://www.tuicool.com/articles/VNVRZny

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.