5、DI依赖注入
5.1、构造注入
已经在Spring系列2的IOC中提到了,可以回去看:Spring的IOC
5.2、Set方式注入【重点】
【环境搭建】
1、复杂类型
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
2、真实测试对象
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> id;
private Set<String> games;
private String wife;
private Properties info;
}
3、applicationContext.xml配置文件
<bean id="student" class="com.only.pojo.Student">
<!--第一种:普通值注入,value -->
<property name="name" value="午夜"/>
</bean>
4、测试类
public class MyTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) context.getBean("student");
System.out.println(student);
}
}
5、完善注入信息:配置文件applicationContext.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" xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd">
<bean id="address" class="com.only.pojo.Address">
<property name="address" value="广州"/>
</bean>
<bean id="student" class="com.only.pojo.Student">
<!--普通值注入,value -->
<property name="name" value="午夜"/>
<!--bean注入,ref -->
<property name="address" ref="address"/>
<!--数组注入 -->
<property name="books">
<array>
<value>红楼梦</value>
<value>西游记</value>
<value>水浒传</value>
<value>三国演义</value>
</array>
</property>
<!-- List -->
<property name="hobbys">
<list>
<value>听歌</value>
<value>敲代码</value>
<value>写书</value>
</list>
</property>
<!-- Map -->
<property name="id">
<map>
<entry key="身份证" value="12345678987654321"/>
<entry key="银行卡" value="11111111111111111"/>
</map>
</property>
<!-- Set -->
<property name="games">
<set>
<value>LOL</value>
<value>王者荣耀</value>
<value>凡尔赛</value>
</set>
</property>
<!-- null -->
<property name="wife">
<null/>
</property>
<!-- properties -->
<property name="info">
<props>
<prop key="学号">201821001111</prop>
<prop key="卡号">201800000000</prop>
<prop key="性别">男</prop>
<prop key="url">jdbc</prop>
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
</beans>
6、测试结果
5.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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- p命名空间注入,可以直接注入属性的值-->
<bean id="user" class="com.only.pojo.User" p:name="吴夜" p:age="18"/>
<!-- c命名空间注入,通过构造器注入:construct-args -->
<bean id="user2" class="com.only.pojo.User" c:age="18" c:name="吴夜2"/>
</beans>
【注意点】:p命名和c命名不能直接使用,需要导入xml约束。
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
6.4、spel注入
<bean name="user" class="com.only.pojo.User" >
<property name="name" value="#{user.name}"></property>
<property name="age" value="#{user.age}"></property>
</bean>
6.5、Bean的作用域Scope
1、singleton单例模式(Spring的默认机制)
<bean id="user2" class="com.only.pojo.User" scope="singleton"/>
2、prototype原型模式:每次从容器中get的时候,都会产生一个新对象。
<bean id="user2" class="com.only.pojo.User" scope="prototype"/>
3、其余的request、session、application,这些只能在web开发中使用到。