Spring中属性注入详解

网友投稿 166 2023-07-02


Spring中属性注入详解

本文演示了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;

/**

* 常见属性的注入

* @see 包括int,String,Array,list,set,map,Date的注入

*/

public class Bean11 {

private Integer intValue;

private String strValue;

private String[] arrayValue;

private List listValue;

private Set http://setValue;

private Map mapValue;

private Date dateValue;

/* 七个属性的setter和getter略 */

}

package com.jadyer.model;

public class Bean22 {

private Bean33 bean33;

private Bean44 bean4422; //注入:与属性名无关,与setBean44()有关

private Bean55 bean55;

/* 三个属性的setter和getter略 */

}

package com.jadyer.model;

public class Bean33 {

private Integer id;

private String name;

private String sex;

/* 三个属性的setter和getter略 */

}

package com.jadyer.model;

public class Bean44 {

private Integer id;

private String name;

private String sex;

private Integer age;

/* 四个属性的setter和getter略 */

}

package com.jadyer.model;

public class Bean55 {

private String password;

/* 关于password的setter和getter略 */

}

然后是我们自定义的java.util.Date类型转换器

package com.jadyer.util;

import java.beans.PropertyEditorSupport;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

/**

* java.util.Date属性编辑器。相当于类型转换器。这里是将String转为Date型

* @see ----------------------------------------------------------------------------------------

* @see 该示例主要让大家知道Spring也有这种机制,不是让大家以后写属性编辑器

* @see 需要写属性编辑器的几率太少了,只要知道Spring也有类似的机制就可以了

* @see ----------------------------------------------------------------------------------------

* @see 所谓的属性编辑器,就是将Spring配置文件中的字符串转换成相应的Java对象

* @see Spring内置了一些属性编辑器,也可以自定义属性编辑器

* @see 自定义属性编辑器事,须继承PropertyEditorSupport类并覆写setAsText()方法

* @see 最后再将自定义的属性编辑器注入到Spring中,即可

* @see ----------------------------------------------------------------------------------------

*/

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文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="

http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-2.5.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-2.5.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-2.5.xsd"

default-lazy-init="true">

array11

array22

list11

list22

set11

set22

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="

http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-2.5.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-2.5.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-2.5.xsd"

default-lazy-init="true">

array11

array22

list11

list22

set11

set22

用到的针对公共实体类的applicationContext-common.xml文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="

http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-2.5.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-2.5.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-2.5.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="

http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-2.5.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-2.5.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-2.5.xsd">

用到的针对java.util.Date属性编辑器的applicationContext-editor.xml文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="

http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-2.5.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-2.5.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-2.5.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="

http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-2.5.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-2.5.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-2.5.xsd">

最后是使用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");

}

/**

* 该方法演示的是常见属性的注入,包括int,String,Array,list,set,map,Date的注入

* @see 其中Date类型的注入则是借助了Spring属性编辑器来实现的

*/

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());

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringMVC处理Form表单实例
下一篇:Java Web实现文件下载和乱码处理方法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~