本文演示了int、String、数组、list、set、map、Date等属性的注入。
其中Date类型的注入则是借助了Spring提供的属性编辑器来实现的,首先是用到的五个实体类
package com.jadyer.model;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Bean11 {
private Integer intValue;
private String strValue;
private String[] arrayValue;
private List listValue;
private Set setValue;
private Map mapValue;
private Date datevalue;
}
package com.jadyer.model;
public class Bean22 {
private Bean33 bean33;
private Bean44 bean4422; //注入:与属性名无关,与setBean44()有关
private Bean55 bean55;
}
package com.jadyer.model;
public class Bean33 {
private Integer id;
private String name;
private String sex;
}
package com.jadyer.model;
public class Bean44 {
private Integer id;
private String name;
private String sex;
private Integer age;
}
package com.jadyer.model;
public class Bean55 {
private String password;
}
然后是我们自定义的java.util.Date类型转换器
package com.jadyer.util;
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class UtilDatePropertyEditor extends PropertyEditorSupport {
private String pattern; //将转换的格式放到配置文件中,让Spring注入进来
public void setPattern(String pattern) {
this.pattern = pattern;
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
System.out.println("======UtilDatePropertyEditor.setAsText()======" + text);
try {
Date date = new SimpleDateFormat(pattern).parse(text);
this.setValue(date); //注意:这里放进去的是一个java.util.Date对象,故输出的时间是默认的格式
} catch (ParseException e) {
e.printStackTrace();
throw new IllegalArgumentException(text); //继续向上抛参数非法的异常
}
}
}
用到的针对所有实体类的applicationContext-beans.xml文件
array11 array22
list11 list22 set11 set22
用到的针对公共实体类的applicationContext-common.xml文件
用到的针对java.util.Date属性编辑器的applicationContext-editor.xml文件
最后是使用JUnit3.8写的单元测试类
package com.jadyer.junit;
import junit.framework.TestCase;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.jadyer.model.Bean11;
import com.jadyer.model.Bean22;
public class PropertyInjectionTest extends TestCase {
private ApplicationContext factory;
@Override
protected void setUp() throws Exception {
//factory = new ClassPathXmlApplicationContext("applicationContext.xml");
//这样就会把两个配置文件作为一个来使用,表面上看是作为两个使用的
//其实内部是作为一个使用的,所以在多个配置文件中,里面的id不能重复
//但是name属性可以重复,就好像人的身份证编号和名字的区别是一样的
//String[] configLocations = new String[]{"applicationContext.xml", "applicationContext-editor.xml"};
//factory = new ClassPathXmlApplicationContext(configLocations);
//业界流行的一句话:约定优于配置
//所以说当有了一个统一的比较好的约定之后,就可以利用框架提供的功能,减少配置量
//另外:如果没有读取到applicationContext-*.xml,此时即便存在applicationContext.xml,它也不会读的
factory = new ClassPathXmlApplicationContext("applicationContext-*.xml");
}
public void testInjection11() {
//Bean11 bean11 = new Bean11(); //若简单的new,那么它的属性是不会被注入的。注入的前提必须是从IoC容器中拿出来的,才会注入
Bean11 bean11 = (Bean11)factory.getBean("bean11"); //此时bean11就是从IoC容器中获取到的,所以它的依赖就会被全部注入
System.out.println("bean11.intValue=" + bean11.getIntValue());
System.out.println("bean11.strValue=" + bean11.getStrValue());
System.out.println("bean11.arrayValue=" + bean11.getArrayValue());
System.out.println("bean11.listValue=" + bean11.getListValue());
System.out.println("bean11.setValue=" + bean11.getSetValue());
System.out.println("bean11.mapValue=" + bean11.getMapValue());
System.out.println("bean11.datevalue=" + bean11.getDatevalue());
}
public void testInjection22() {
Bean22 bean22 = (Bean22)factory.getBean("bean22");
System.out.println("bean22.bean33.id=" + bean22.getBean33().getId());
System.out.println("bean22.bean33.name=" + bean22.getBean33().getName());
System.out.println("bean22.bean33.sex=" + bean22.getBean33().getSex());
System.out.println("bean22.bean44.id=" + bean22.getBean44().getId());
System.out.println("bean22.bean44.name=" + bean22.getBean44().getName());
System.out.println("bean22.bean44.sex=" + bean22.getBean44().getSex());
System.out.println("bean22.bean44.age=" + bean22.getBean44().getAge());
System.out.println("bean22.bean55.password=" + bean22.getBean55().getPassword());
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



