Essential attributes injection Spring-

Essential attributes injection Spring-

First new maven project, import dependence

	   <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.7.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
        </dependency>

Book.java

@Data
public class Book {
    private Integer id;
    private String name;
    private String address;
	//一定不能省略
    Book(){
       System.out.println("----book----");
    }
    Book(Integer id, String name,String address){
        this.id = id;
        this.name = name;
        this.address = address;
    }

}

Entity classes: Person.java


@Data
public class Person {
    private Integer id;
    private String name;
    private Book book;
    private String[] hobby;
    private Map<Integer,String> map;
}

  1. The basic properties of injection may be injected via the constructor, and a set

    1.1. Constructor, override the constructor

    <bean id="book" class="org.youyuan.bean.Book">
        <constructor-arg name="id" value="1"></constructor-arg>
        <constructor-arg name="name" value="三国演义"></constructor-arg>
        <constructor-arg name="address" value="jx"></constructor-arg>
    </bean>

1.2. Injection set, the entity class Book here must be set to provide a method, whether the person can not complete the injection.

<bean id="book1" class="org.youyuan.bean.Book">
        <property name="id" value="2"></property>
        <property name="name" value="西游记"></property>
        <property name="address" value="南昌"></property>
 </bean>
  1. Injection complex property, injected into the object, the Person Book introduced, use ref, book id corresponding to the above-Book
 <bean id="person" class="org.youyuan.bean.Person">
        <property name="book" ref="book"></property>
 </bean>

3. injected array (list, too)

<bean id="person" class="org.youyuan.bean.Person">
        <property name="book" ref="book1"></property>
        <property name="hobby">
            <array>
                <value>跑步</value>
                <value>跳水</value>
                <value>篮球</value>
            </array>
        </property>
 </bean>

3.map injection

<bean id="person" class="org.youyuan.bean.Person">
        <property name="book" ref="book1"></property>
        <property name="hobby">
            <array>
                <value>跑步</value>
                <value>跳水</value>
                <value>篮球</value>
            </array>
        </property>

        <property name="map">
            <map>
                <entry key="1" value="zs"></entry>
                <entry key="2" value="ls"></entry>
            </map>
        </property>
</bean>
Published 25 original articles · won praise 0 · Views 294

Guess you like

Origin blog.csdn.net/qq_42219004/article/details/105152571