上篇回顾
在上一篇中小编主要讲了:
- Spring的简介
- Spring IOC的概述
- Spring bean基本对象的使用:(包含以下几点)
- Bean 对象的配置
- Bean 对象的构建
- Bean 对象的作用域
- spring Bean的生命周期
- Bean 对象的延迟加载
Spring Bean 的依赖注入
Spring 容器中的Bean对象通常会存在一定的依赖关系,而这种依赖关系的实现在Spring 框架中要借助于DI机制。其中DI就是借助对象管理对象依赖关系的一个过程。
Spring依赖注入时,可以实现基本值注入、Bean对象注入、集合注入等。
Spring中主要提供两种:
- set注入:借助set方法参数实现其属性值的注入。
public class JdbcTemplate {
private DataSource dataSource;
//注意:
//1.如果这里的setDataSource改成setDs则会注入失败,需要把jdbcTemplate1引用的name=“dataSource”改为name="Ds"
//2.注入的类型需要匹配,指的是:ref=“dataSource1”引用的对象类型需要与setDataSource(DataSource dataSource)这个set方法里面的参数类型相同,如果改成setDataSource(Date date)则注入失败,因为类型不匹配
public void setDataSource(DataSource dataSource) {
System.out.println("==set==");
this.dataSource = dataSource;
}
public DataSource getDataSource() {
return dataSource;
}
}
<!-- 底层会调用set方法将dataSource1属性值注入到jdbcTemplate1当中 -->
<bean id="dataSource1" class="com.company.spring.util.DataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- property中的ref属性用于引用spring中的其他bean对象属性 -->
<bean id="jdbcTemplate1" class="com.company.spring.injection.JdbcTemplate">
<property name="dataSource" ref="dataSource1"/>
</bean>
- 构造注入:借助构造方法的参数实现对类中属性值得注入。
public class JdbcTemplate {
private DataSource dataSource;
public JdbcTemplate(DataSource dataSource){
this.dataSource=dataSource;
}
}
<!-- 底层会调用构造方法为其属性赋值 -->
<bean id="dataSource2" class="com.company.spring.util.DataSource">
<constructor-arg value="com.mysql.jdbc.Driver"/>
<constructor-arg value="jdbc:mysql:///test"/>
<constructor-arg value="root"/>
<constructor-arg value="root"/>
</bean>
<bean id="jdbcTemplate2" class="com.company.spring.injection.JdbcTemplate">
<constructor-arg ref="dataSource2"/>
</bean>
数组、集合值的注入
spring-config.xml
<bean id="sessionFactory" class="com.bean.SqlSessionFactory">
<!-- 对数组类型的变量进行值的注入 -->
<property name="mapperLocations">
<list>
<value>mapperA</value>
<value>mapperB</value>
</list>
</property>
<!-- 对list集合类型的变量进行值的注入 -->
<property name="list">
<list>
<value>listA</value>
<value>listB</value>
</list>
</property>
<!-- 对map集合类型的变量进行值的注入 -->
<property name="map">
<map>
<entry key="key1" value="100"/>
<entry key="key2" value="200"/>
</map>
</property>
</bean>
实体类SqlSessionFactory
public class SqlSessionFactory{
private String[] mapperLocations;
private List<String> list;
private Map<String, Object> map;
public void setMapperLocations(String[] mapperLocations) {
this.mapperLocations= mapperLocations;
}
public String[] getMapperLocations() {
return mapperLocations;
}
public void setList(List<String> list) {
this.list= list;
}
public List<String> getList() {
return list;
}
public void setMap(Map<String, Object> map) {
this.map= map;
}
public Map<String, Object> getMap() {
return map;
}
}
Junit测试
@junit
public class test{
private ClassPathXmlApplicationContext app;
@Before
public void init(){
app = new ClassPathXmlApplicationContext("spring-config.xml")
}
@Test
public void testDate(){
SqlSessionFactory sql= app.getBean("sessionFactory", SqlSessionFactory.class);
String[] mappers = sql.getMapperLocations();
List<String> list = sql.getList();
Map<String, Object> map = sql.getMap();
System.out.println(Arrays.toString(mappers));
System.out.println(list);
System.out.println(map);
}
@After
public void closeTest(){
app.close();
}
}
控制台输出
[mapperA,mapperA]
[listA,listB]
{key1="100",key2="200"}
spring El表达式
config.properties
username=root
password=123
spring-config.xml
spring底层会加载类路径下的config.properties文件,并以key-value的形式将内容存储到Properties(Map)对象
<beans>
<util:properties id="cfg" location="classPath:config.properties"/>
<property name="map">
<map>
<!-- 从Properties中通过spring El表达式取值 -->
<entry key="key1" value="#{cfg.username}"/>
<entry key="key2" value="#{cfg.password}"/>
</map>
</property>
</beans>
Spring的自动装配
Spring应用中还可以按照一定规则自动为对象属性注入值,此机制实现的可以借助Bean标签的autowire属性进行配置。此属性的值有如下几种:
- default:不执行自动装配(默认)
- byName:按名字进行装配
- byType:按类型进行装配
- constructor:按构造方法参数类型进行装配
持续更新中。。。