对于对象的注入,我们使用ref方式,可以指定注入的对象,下面看下属性的注入,以及当spring无法转换基本类型进行注入时,如何编写一个类似转换器的东西来完成注入。
【属性注入】
常见属性的注入:int,String,list,set,map的注入
【属性编辑器和作用】
- 将spring配置文件中的字符串转换成相应的Java对象
- spring内置了一些属性编辑器,也可以自定义属性编辑器
Bean1.java
package com.spring.tl.injection;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Created by 滕柳 on 2018/6/4.
*/
public class Bean1 {
private String strValue;
private int intValue;
private List listValue;
private Set setValue;
private String[] arrayValue;
private Map mapValue;
public String getStrValue() {
return strValue;
}
public void setStrValue(String strValue) {
this.strValue = strValue;
}
public int getIntValue() {
return intValue;
}
public void setIntValue(int intValue) {
this.intValue = intValue;
}
public List getListValue() {
return listValue;
}
public void setListValue(List listValue) {
this.listValue = listValue;
}
public Set getSetValue() {
return setValue;
}
public void setSetValue(Set setValue) {
this.setValue = setValue;
}
public String[] getArrayValue() {
return arrayValue;
}
public void setArrayValue(String[] arrayValue) {
this.arrayValue = arrayValue;
}
public Map getMapValue() {
return mapValue;
}
public void setMapValue(Map mapValue) {
this.mapValue = mapValue;
}
}
applicationContext-injection.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns="http://www.springframework.org/schema/beans" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="bean1" class="com.spring.tl.injection.Bean1">
<property name="strValue" value="Hello_tl"/>
<property name="intValue" value="123"/>
<property name="listValue" >
<list>
<value>list1</value>
<value>list2</value>
</list>
</property>
<property name="arrayValue" >
<list>
<value>arrayValue1</value>
<value>arrayValue2</value>
</list>
</property>
<property name="mapValue">
<map>
<entry key="k1" value="v1"/>
<entry key="k2" value="k2"/>
</map>
</property>
<property name="setValue" >
<set>
<value>set1</value>
<value>set2</value>
</set>
</property>
</bean>
</beans>
InjectionTest
package com.spring.tl.client;
import com.spring.tl.injection.Bean1;
import junit.framework.TestCase;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by 滕柳 on 2018/6/4.
*/
public class InjectionTest extends TestCase {
private BeanFactory factory;
@Override
protected void setUp() throws Exception{
//factory=new ClassPathXmlApplicationContext("injection.xml");//一个配置文件
//多个配置文件数组形式展示
// String[] configLocations=new String[]{"injection.xml","applicationContext-editer.xml"};
// factory=new ClassPathXmlApplicationContext(configLocations);
//多个配置文件统一展示
factory=new ClassPathXmlApplicationContext("classpath*:/applicationContext-*.xml");
}
@Override
protected void tearDown() throws Exception{}
@Test
public void testInjection(){
Bean1 bean=(Bean1) factory.getBean("bean1");
System.out.println(bean.getStrValue());
System.out.println(bean.getIntValue());
System.out.println(bean.getListValue());
System.out.println(bean.getSetValue());
System.out.println(bean.getMapValue());
System.out.println(bean.getArrayValue());
}
}
【自定义属性编辑器】
- 集成PropertyEditorSupport
- 覆盖setAsTest()方法
- 将自定义的属性编辑器注入到spring中
看此博客:spring2和spring4自定义属性编辑器的差别----处理日期格式
我们添加了日期的属性,不能自动注入到spring中,所以我们要转一下,来实践一下
Bean1.java
applicationContext-injection.xml
UtilDatePropertyEditor.java
package com.spring.tl.injection;
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by 滕柳 on 2018/6/4.
*/
public class UtilDatePropertyEditor extends PropertyEditorSupport {
@Override
public void setAsText(String text) throws IllegalArgumentException{
System.out.println("-----setAsTest---"+text);
try {
Date date=new SimpleDateFormat("yyyy-MM-dd").parse(text);
this.setValue(date);
}catch (ParseException e){
e.printStackTrace();
throw new IllegalArgumentException(text);
}
}
}
applicationContext-editer.xml
【多配置文件的读取方式】
- 可以采用数组
- 可以采用*配置模式
看此博客:Spring ClassPathXmlApplicationContext和FileSystemXmlApplicationContext读取配置文件的方法
【减少spring的配置文件】
- 通过<bean>标签将公共的配置文件提取出来,然后指定<bean>标签中的abstract属性为true
- 在其他<bean>标签中指定其parent即可
代码实现:
Bean2中包含Bean3,Bean4,Bean5其中Bean3和Bean4中有相同的属性
Bean2.java
Bean3.java
Bean4.java
Bean5.java
1、正常配置:
applicationContext-injection.xml
<bean id="bean2" class="com.spring.tl.injection.Bean2">
<property name="bean3" ref="bean3"></property>
<property name="bean4">
<ref bean="bean4"></ref>
</property>
<property name="bean5" ref="bean5"></property>
</bean>
<bean id="bean3" class="com.spring.tl.injection.Bean3">
<property name="id" value="100"></property>
<property name="age" value="22"></property>
<property name="name" value="张三"></property>
</bean>
<bean id="bean4" class="com.spring.tl.injection.Bean4">
<property name="id" value="100"></property>
<property name="name" value="李四"></property>
<property name="age" value="23"></property>
<property name="sex" value="女"></property>
</bean>
<bean id="bean5" class="com.spring.tl.injection.Bean5">
<property name="password">
<value>123</value>
</property>
</bean>
2、减少spring的配置文件:
applicationContext-injection.xml
<bean id="bean2" class="com.spring.tl.injection.Bean2">
<property name="bean3" ref="bean3"></property>
<property name="bean4">
<ref bean="bean4"></ref>
</property>
<property name="bean5" ref="bean5"></property>
</bean>
<bean id="bean5" class="com.spring.tl.injection.Bean5">
<property name="password">
<value>123</value>
</property>
</bean>
applicationContext-common.xml
测试时要扫描全部的配置文件
【配置延迟加载】
1、spring配置文件中的beans默认情况下是及时加载的,有时候一些类加载耗时很厉害,我们可以通过配置将其设置为延迟加载。即延迟配置文件(bean)的初始化
2、spring配置文件中的beans节点有一个属性default-lazy-init和bean节点有一个属性是lazy-init,将这个属性的值设置为true时就可以实现延迟加载,放到beans节点上时会影响其下的所有子bean,放在bean上时只影响一个bean。