5.1 构造器注入
前面讲过了
5.2 set方式注入【重点】
-
依赖注入:set注入
-
依赖:bean对象的创建依赖于容器
-
注入:bean对象中所有属性,由容器来注入
-
【环境搭建】
1.复杂类型
public class Address {
private String address;
2.真实测试对象
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;
3.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="address" class="com.kuang.pojo.Address">
<property name="address" value="石家庄"/>
</bean>
<bean id="student" class="com.kuang.pojo.Student">
<property name="name" value="高壮壮" />
<property name="address" ref="address"/>
<property name="books">
<array>
<value>红楼梦</value>
<value>水浒传</value>
<value>三国演义</value>
<value>西游记</value>
</array>
</property>
<property name="hobbys">
<list>
<value>唱歌</value>
<value>聊天</value>
<value>吃火锅</value>
</list>
</property>
<property name="card">
<map>
<entry key="身份证" value="1951565857874988"/>
<entry key="银行卡" value="55489484545"/>
</map>
</property>
<property name="games">
<set>
<value>合金弹头</value>
<value>魂斗罗</value>
<value>超级玛丽</value>
</set>
</property>
<property name="wife">
<null/>
</property>
<property name="info">
<props>
<prop key="driver">445145</prop>
<prop key="url">localhost://3306</prop>
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
</beans>
4.测试
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
/*
* Student{name='高壮壮',
* address=Address{address='石家庄'},
* books=[红楼梦, 水浒传, 三国演义, 西游记],
* hobbys=[唱歌, 聊天, 吃火锅],
* card={身份证=1951565857874988,
* 银行卡=55489484545},
* games=[合金弹头, 魂斗罗, 超级玛丽],
* wife='null',
* info={password=123456, url=localhost://3306, driver=445145, username=root}}
*
* */
}
5.3 拓展方式注入
我们可以使用P命名空间 和 C命名空间注入
注:需要导入xml约束
xmlns:p="http://www.springframework.org/schema/p"
<!--p命名空间注入,可以直接注入属性 property -->
<bean id="user" class="com.hardy.pojo.User" p:age="18" p:name="hardy"/>
xmlns:c="http://www.springframework.org/schema/c"
<!--C命名空间注入,通过有参构造器注入,constructor -->
<bean id="user2" class="com.hardy.pojo.User" c:name="张三" c:age="23"/>
5.4 bean的作用域:6种`
singleton:全局只能有1个
prototype:每一个变量都有一个自己的
request 以下的只能在web中使用
session
application
websocket
5.4.1 单例模式(spring默认机制)
<!--C命名空间注入,通过有参构造器注入,constructor -->
<bean id="user2" class="com.hardy.pojo.User" c:name="张三" c:age="23" scope="singleton"/>
@Test
public void test(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("userbean.xml");
User user = (User) context.getBean("user2",User.class);
User user2 = (User) context.getBean("user2",User.class);
System.out.println(user==user2);
}
//结果为:true
5.4.2 原形模式 每一次从容器中get的时候,会产生新的对象
<!--C命名空间注入,通过有参构造器注入,constructor -->
<bean id="user2" class="com.hardy.pojo.User" c:name="张三" c:age="23" scope="prototype"/>
@Test
public void test(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("userbean.xml");
User user = (User) context.getBean("user2",User.class);
User user2 = (User) context.getBean("user2",User.class);
System.out.println(user==user2);
}
//结果为:false