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、引入的依赖以及相关的类

java工具类中怎么注入别的对象 java注入bean_bean

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、导入的依赖及相关类

java工具类中怎么注入别的对象 java注入bean_bean_02

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);
    }
}

之后使用单元测试依次进行测试即可