spring dependency injection three ways

 

  In the actual environment manner IoC container divided into two categories, one is dependent lookup, through resource locator dependent lookup, to find the corresponding resource back; the other is dependent on injection, and is mainly used Spring Dependency injection. In general, injection dependency can be divided into three ways.
  • constructor injection.
  • setter injection.
  • interface injection.
  Constructor injection and setter injection is the main way, and interface injection is injected from the way other places, such as in a Web project, configuration data sources are often configured to go through a server (such as Tomcat), this time we can use JNDI It forms it over the interface to inject Spring IoC container. Here they are explained in detail.

Constructor injection

  Constructor implemented method of injection depends on the configuration, the method may be configured to have either no parameters parameters. In most cases, we are all created by the class constructor class object, Spring reflection of the way can also be used to complete the construction by using the injection method, which is the constructor injection principle.

public class Role {
    private Long id;
    private String roleName;
    private String note;


    public Role(String roleName, String note) {
        this.roleName = roleName;
        this.note = note;
    }

    /******** setter and getter *******/
}

  This time is no way to take advantage of no-argument constructor to create an object, in order to be able to create the object Spring correctly, you can do so like Listing.

<bean id="role1" class="com.ssm.chapter9.pojo.Role">
    <constructor-arg index="0" value="总经理"/>
    <constructor-arg index="1" value="公司管理者"/>
</bean>

  constructorarg element argument constructor for the class definition, which is used to define the parameters of the position index, and the value is a set value, defined by such a method of using the constructor will know Spring Such Role (String, String) to create the objects. Such injection is relatively simple, but the disadvantages are also obvious, because the parameters here is relatively small, so readability is good, but if the argument a lot, so this construction method is more complicated, and this time should be considered setter injection.

Use setter injection

  Spring setter injection is the most mainstream injection method, which uses JavaBean specification defined setter methods to complete the injection, flexible and high readability. It eliminates the possibility of multiple parameters when using constructor injection, the first constructor can be declared with no parameters, then use setter injection to set the corresponding value, in fact, be the reality reflected by Java technology. It is assumed here for the first class code listing Role added a constructor with no parameters, and then do the configuration code list.

<bean id="role2" class="com.ssm.chapter9.pojo.Role">
    <property name="roleName" value="高级工程师"/>
    <property name="note" value="重要人员"/>
</bean

  Such Spring constructor will not generate the object parameters invoked by reflection while injecting reflection value configured by the corresponding setter. This approach is the most major way Spring, widely used in the practical work.

Interface injection

  There are times when resources are not from their own system, but from the outside world, such as database connection resources can be fully configured in under Tomcat, and then go get it through JNDI form, so that the database connection resources are resources that belong to foreign development project, this time we the interface injection may be employed to obtain it in the form of

Guess you like

Origin www.cnblogs.com/ooo0/p/10962360.html