1.构造器注入

前面写过了

2.Set方式注入【重点】

  • 依赖注入:Set注入!
    • 依赖:bean对象的创建依赖于容器
    • 注入:bean对象中的所有属性,由容器来注入

【环境搭建】

  1. 复杂类型
public class Address {
    private String address;

    public String getAddress(){
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}
  1. 真实测试对象
public class Student {

    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    private Properties info;
    }
  1. beans.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="student" class="com.mine.pojo.Student">
            <!--第一种,普通值注入-->
            <property name="name" value="Violet"/>
        </bean>
</beans>
  1. 测试类
public class MyTest {
    public static void main(String[] args){
        ApplicationContext  context = new ClassPathXmlApplicationContext("beans.xml");
        //        Student.class不用每次强转
        Student student = context.getBean("student",Student.class);
        System.out.println(student);
    }
}
  1. 完善注入信息
<?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="address" class="com.mine.pojo.Address">
        <property name="address" value="冰"/>
    </bean>

    <bean id="student" class="com.mine.pojo.Student">
        <!--第一种,普通值注入-->
        <property name="name" value="Violet"/>
        <!--第二种bean注入,ref-->
        <property name="address" ref="address"/>
        <!--第三种数组注入-->
        <property name="books">
            <array>
                <value>唱</value>
                <value>跳</value>
                <value>Rap</value>
            </array>
        </property>
        <!--List-->
        <property name="hobbys">
            <list>
                <value>菜</value>
                <value>虚</value>
                <value>坤</value>
            </list>
        </property>
        <!--Map-->
        <property name="card">
            <map>
                <entry key="我凝视着恒星" value="期待着那场风暴"/>
                <entry key="梦醒" value="路在何方"/>
            </map>
        </property>
        <!--set-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>COC</value>
                <value>BOB</value>
            </set>
        </property>
        <!--null值注入-->
        <property name="wife">
            <null></null>
        </property>
        <!--Properties-->
        <property name="info">
            <props>
                <prop key="driver">201819244205</prop>
                <prop key="username">root</prop>
                <prop key="password">162166</prop>
            </props>
        </property>
    </bean>

</beans>

3.拓展方式注入

我们可以使用p命名空间和c命名空间

<?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"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--p命名空间注入,可以直接注入属性的值,property-->
    <bean id="user" class="com.mine.pojo.User" p:name="冰" p:age="19"/>

    <!--c命名空间,User类需要有有参构造和无参构造,
        通过构造器注入:construct-args-->
    <bean id="user2" class="com.mine.pojo.User" c:name="Violet" c:age="18"/>
</beans>

测试类

@Test
    public void testUser(){
        ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
//        使用User.class不用每次强转
        User user =  context.getBean("user2",User.class);
        System.out.println(user);
    }

4.注意

p和c命名空间不能直接使用需要导入xml约束

xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"

5.官方文档

6.bean作用域

  • 单例模式 (Spring默认机制)
<!--scope="singleton"设置单例模式-->
<bean id="user2" class="com.mine.pojo.User" c:name="Violet" c:age="18" scope="singleton"/>
  • 原型模式:每次从容器中get时,产生新的对象
<!--scope="prototype"设置原型模式-->
<bean id="user2" class="com.mine.pojo.User" c:name="Violet" c:age="18" scope="prototype"/>
  • 其余的request、session、application、这些个只能在web开发中使用到!