04-spring dependency injection

A, spring Dependence injection

  1. Dependency Injection:
    • Dependency Injection
  2. IOC's role:
    • Reducing the coupling (dependencies) between programs
  3. Dependency management relations:
    • After all come to spring maintenance
    • In the current class need to use objects of other classes, provided by the spring for us, we just need to explain in the configuration file
  4. Maintaining relationships of dependence:
    • Dependency injection is called
  5. Dependency Injection:
    • You can inject data: There are three types of
      1. Basic types and String
      2. Other bean types (in the configuration file or the configuration notes had bean)
      3. Complex Type / collection type
    • Injection method: There are three
      1. Using the constructor provided
      2. Using the set method to provide
      3. Use annotations provided

Second, the constructor injection

Generally do not

Adapt to the label: constructor-Arg

Location tag appear: internal bean labels

Tag attributes:

type: specifies the data type of data to be injected, the data type is a function of certain parameters or type configuration.

index: injecting data used to specify the index position specified parameter assignment given constructor. Location parameter index from scratch.

name: used to specify the configuration parameters to specify the name of the parameter assignment.

=============================== three or more assigned to the constructor for that parameter assignment ====== ====================

value: String for providing basic types and data types

ref: other bean types for the specified data. It refers to appear in the spring ioc core container had bean object

Advantage:

When acquiring bean object, inject data is a must, otherwise the object can not be created successfully.

Drawbacks:

Examples of ways to change the bean object, so that when we create objects, if less than this data must also be provided

Constructor AccountServiceImpl 1. implementation class

public class AccountServiceImpl implements IAccountService {

    //如果是经常变化的数据,并不适用于注入的方式
    private String name;
    private Integer age;
    private Date birthday;

    public AccountServiceImpl(String name, Integer age, Date birthday) {
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

    public void saveChange() {
        System.out.println("保存了"+name+","+age+","+birthday);
    }
}

2.bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <bean id="accountService" class="com.service.impl.AccountServiceImpl">
        <constructor-arg type="java.lang.String" name="name" value="test"/>
        <constructor-arg name="age" value="18"/>
        <constructor-arg name="birthday" ref="now"/>
    </bean>

    <!--配置一个日期对象-->
    <bean id="now" class="java.util.Date"/>
</beans>

Three, set injection method (more common)

set method is more commonly used

Label involved: Property

Location appears: internal bean labels

Tag attributes:

name: used to specify the configuration parameters to specify the name of the parameter assignment.

Note: The name of the attribute name is not here, but setName methods Name

value: String for providing basic types and data types

ref: other bean types for the specified data. It refers to appear in the spring ioc core container had bean object

Advantage:

There is no clear limit when you create an object, you can use the default constructor

Drawbacks:

If there is a member must have a value, it is possible to get the object set method is not performed

1. The implementation class AccountServiceImpl2

public class AccountServiceImpl2 implements IAccountService {
    private String name;
    private Integer age;
    private Date birthday;

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public void saveChange() {
        System.out.println("保存了"+name+","+age+","+birthday);
    }
}

2. bean.xml

    <bean id="accountService2" class="com.service.impl.AccountServiceImpl2">
        <property name="name" value="卡兹克"/>
        <property name="age" value="19"/>
        <property name="birthday" ref="now"/>
    </bean>

Fourth, the complex type injection / collection type injection

List structure used to inject a tag set:

  1. List structure used to inject a tag set:
    • ·List Array set
  2. Map for injection tags set structure:
    • Map props
  3. The same configuration, the label may be interchanged

1.AccountServiceImpl3

public class AccountServiceImpl3 implements IAccountService {
    private String[] myStrings;
    private List<String> myList;
    private Set<String> mySet;
    private Map<String,String> myMap;
    private Properties myProps;

    public void setMyStrings(String[] myStrings) {
        this.myStrings = myStrings;
    }

    public void setMyList(List<String> myList) {
        this.myList = myList;
    }

    public void setMySet(Set<String> mySet) {
        this.mySet = mySet;
    }

    public void setMyMap(Map<String, String> myMap) {
        this.myMap = myMap;
    }

    public void setMyProps(Properties myProps) {
        this.myProps = myProps;
    }

    public void saveChange() {
        System.out.println(Arrays.toString(myStrings));
        System.out.println(myList);
        System.out.println(mySet);
        System.out.println(myMap);
        System.out.println(myProps);
    }
}

2.bean.xml

    <bean id="accountService2" class="com.service.impl.AccountServiceImpl2">
        <property name="name" value="卡兹克"/>
        <property name="age" value="19"/>
        <property name="birthday" ref="now"/>
    </bean>

    <bean id="accountService3" class="com.service.impl.AccountServiceImpl3">
        <property name="myStrings">
            <array>
                <value>aaa</value>
                <value>bbb</value>
                <value>ccc</value>
            </array>
        </property>
        <property name="myList">
            <list>
                <value>aaa</value>
                <value>bbb</value>
                <value>ccc</value>
            </list>
        </property>
        <property name="mySet">
            <set>
                <value>aaa</value>
                <value>bbb</value>
                <value>ccc</value>
            </set>
        </property>

        <property name="myMap">
            <map>
                <entry key="testA" value="aaa"/>
                <entry key="testB" value="bbb"/>
                <entry key="testC">
                    <value>ccc</value>
                </entry>
            </map>
        </property>

        <property name="myProps">
            <props>
                <prop key="testA">aaa</prop>
                <prop key="testB">bbb</prop>
            </props>
        </property>
    </bean>

Guess you like

Origin www.cnblogs.com/zuiren/p/11415417.html