1. 程式人生 > >spring中Bean的注入(1)

spring中Bean的注入(1)

Bean的注入包含的內容:

XML注入

構造方法注入、靜態工廠注入、例項工廠注入,屬性注入的幾種方式(構造方法注入,set方法注入,p名稱空間注入,物件,集合、陣列、Map,Properties)

Java注入:@Configuration、@Bean

自動注入:

        XML:<component-scan/>

        Java:@ComponentScan

@Repository

@Service

@Controller

@Component

Profile:

        Java:@Profile

        XML:<beans profile="dev"

條件註解:

        @Conditional

Bean的生命週期:

        singleton、prototype、request、session

混合配置:

        @ImportResource

本篇文章介紹的就是XML注入

xml注入包括的內容,

1. 構造方法注入

2. 靜態工廠注入

3. 例項工廠注入

4. 屬性注入:

  4.1 構造方法注入

  4.2 set方法注入

  4.3 p名稱空間注入

  4.4 陣列/集合/Map/Properties/物件

構造方法注入

1、引入的依賴以及相關的類

2、封裝userbean 

public class user {
    private Integer id;
    private String name;
    private String address;
}

3、建立XML中的相關配置

<bean class="com.tang.user" name="user1"/>

4、初始化spring容器使用單元測試

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
    private ClassPathXmlApplicationContext ctx;
    public static void main(String[] args) {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        user user1 = (user) ctx.getBean("user1");
        user1.sayHello("張三");
    }
    @Before
    public void before(){
        ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    }
    @Test
    public void test(){
        user user1 = (user) ctx.getBean("user1");
        user1.sayHello("張三");
    } 
}

5、結果

-------------
hello 張三 !

靜態工廠注入

1、2、同上

3、定義類的靜態方法

public class UserFactory {
    public static user getInstance(){
        return new user();
    }
}

4、在XML中進行配置

<bean class="com.tang.UserFactory" factory-method="getInstance" id="user2"/>

5、使用單元測試進行測試

@Before
public void before(){
    ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
}

@Test
public void test3() {
    user user2 = (user) ctx.getBean("user2");
    user2.sayHello("zhaoliu");
}

例項工廠注入

1、2同上

3、工廠方法示例

public class UserFactory2 {
    public user getInstance(){
        return new user();
    }
}

4、配置示例

<!--例項工廠-->
<bean class="com.tang.UserFactory2" id="userFactory2"/>
<bean class="com.tang.user" factory-bean="userFactory2" factory-method="getInstance" id="user3"/>

5、使用單元測試進行測試

@Test
public void test3() {
    user user3 = (user) ctx.getBean("user3");
    user3.sayHello("zhao");
}

6、結果

-------------
hello zhao !

屬性注入的幾種方式(構造方法注入,set方法注入,p名稱空間注入,物件,集合、陣列、Map,Properties)

1、匯入的依賴及相關類

2、封裝bean

public class Cat{
    private Integer id;
    private String name;
    private String color;
}
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class User {
    private Integer id;
    private String name;
    private String add;
    private Cat cat;
    private List<String> favorites;
    private Book[] books;
    private Map<String,Object> ca;
    private Properties props;
}
public class Book {
    private Integer id;
    private String name;
    private String author;
}

3、XML配置   使用封裝的book

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-   beans.xsd">

    <!-- 構造方法注入 -->
    <bean class="com.tang.Book" id="book1">
        <constructor-arg name="id" value="1"/>
        <constructor-arg name="name" value="三國演義"/>
        <constructor-arg name="author" value="羅貫中"/>
    </bean>
    <!-- 下標定位引數 -->
    <bean class="com.tang.Book" id="book2">
        <constructor-arg index="0" value="2"/>
        <constructor-arg index="1" value="紅樓夢"/>
        <constructor-arg index="2" value="曹雪芹"/>
    </bean>
    <!-- set方法注入就是利用物件屬性的set方法給屬性賦值 -->
    <bean class="com.tang.Book" id="book3">
        <property name="author" value="施耐庵"/>
        <property name="id" value="3"/>
        <property name="name" value="水滸傳"/>
    </bean>
    <!-- p名稱空間注入本質上還是set方法注入,只是寫法不同(注意:p名稱空間注入,需要有無參構造方法) -->
    <bean class="com.tang.Book" id="book4" p:id="4" p:author="吳承恩" p:name="西遊記"/>
</beans>

使用單元測試進行驗證

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    private ClassPathXmlApplicationContext ctx;
    @Before
    public void before(){
        ctx = new ClassPathXmlApplicationContext("application.xml");
    }
    @Test
    public void test1(){
        Book book1 = (Book) ctx.getBean("book1");
        System.out.println(book1);
    }
    @Test
    public void test2(){
        Book book2 = (Book) ctx.getBean("book2");
        System.out.println(book2);
    }
    @Test
    public void test3(){
        Book book3 = (Book) ctx.getBean("book3");
        System.out.println(book3);
    }
    @Test
    public void test4(){
        Book book4 = (Book) ctx.getBean("book4");
        System.out.println(book4);
    }
}

之後使用單元測試依次進行測試即可

4、XML配置     封裝user

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 如果使用的是同一個cat,要用cat-ref,物件可以通過構造方法、set方法或者p名稱空間注入-->
    <bean class="com.tang.Cat" id="cat1">
        <property name="id" value="1"/>
        <property name="name" value="小白"/>
        <property name="color" value="白色"/>
    </bean>
    <bean class="com.tang.User" id="user1">
        <property name="name" value="張三"/>
        <property name="id" value="1"/>
        <property name="add" value="深圳"/>
        <property name="cat" ref="cat1"/>
    </bean>
    <bean class="com.tang.User" id="user2">
        <constructor-arg name="name" value="里斯"/>
        <constructor-arg name="id" value="2"/>
        <constructor-arg name="add" value="廣州"/>
        <constructor-arg name="cat" ref="cat1"/>
    </bean>
    <bean class="com.tang.User" id="user3" p:id="3" p:cat-ref="cat1" p:name="王五"/>


    <!--陣列和集合的注入方式-->
    <bean class="com.tang.User" id="user4">
        <property name="add" value="廣州" />
        <property name="id" value="4"/>
        <property name="name" value="王五"/>
        <property name="favorites">
            <list>
                <value>足球</value>
                <value>籃球</value>
            </list>
        </property>
        <property name="books">
            <list>
                <bean class="com.tang.Book">
                    <property name="name" value="三國演義"/>
                    <property name="id" value="1"/>
                </bean>
                <bean class="com.tang.Book">
                    <property name="name" value="水滸傳"/>
                    <property name="id" value="2"/>
                </bean>
            </list>
        </property>
    </bean>


    <bean class="com.tang.Book" id="book1">
        <property name="name" value="三國演義"/>
        <property name="id" value="1"/>
    </bean>
    <bean class="com.tang.Book" id="book2">
        <property name="id" value="2"/>
        <property name="name" value="紅樓夢"/>
    </bean>
    <bean class="com.tang.User" id="user5">
        <property name="add" value="廣州"/>
        <property name="id" value="4"/>
        <property name="name" value="王五"/>
    <!--集合方式注入-->
        <property name="favorites">
            <list>
                <value>足球</value>
                <value>籃球</value>
            </list>
        </property>
    <!--陣列方式注入-->
        <property name="books">
            <list>
                <ref bean="book1"></ref>
                <ref bean="book2"></ref>
            </list>
        </property>
    <!--map方式注入-->
        <property name="ca">
            <map>
                <entry key="age" value="99"/>
                <entry key="cat" value-ref="cat1"/>
            </map>
        </property>
    <!--properties注入-->
        <property name="props">
            <props>
                <prop key="gender">男</prop>
                <prop key="national">中國</prop>
            </props>
        </property>
    </bean>

</beans>

使用單元測試

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main2 {
private ClassPathXmlApplicationContext ctx;

    @Before
    public void before(){
        ctx = new ClassPathXmlApplicationContext("application2.xml");
    }
    @Test
    public void test(){
        User user1 = (User) ctx.getBean("user1");
        User user2 = (User) ctx.getBean("user2");
        User user3 = (User) ctx.getBean("user3");
        System.out.println(user1);
        System.out.println(user2);
        System.out.println(user3);
    }
    @Test
    public void test2(){
        User user4 = (User) ctx.getBean("user4");
        System.out.println(user4);
    }
    @Test
    public void test3(){
        User user5 = (User) ctx.getBean("user5");
        System.out.println(user5);
    }
}

之後使用單元測試依次進行測試即可