Spring学习笔记1
学习内容
配置Spring |
普通属性的注入 |
自定义属性编辑器 |
配置Spring
核心包
日志记录
%Spring%/lib/jarkata-commons/commons-logging.jar %Spring%/lib/log4j/log4j-1.2.14.jar |
拷贝Spring配置文件
%Spring%/samples/jpetstore/war/WEB-INF/applicationContext.xml |
拷贝Spring日志配置文件
samples/jpetstore/war/WEB-INF/log4j.properties |
单元测试
%Spring%lib/junit/junit-4.4.jar |
为MyEclipse添加XML文件模板
Window/Preferences/MyEclipse/Files and Editors/XML/XML Catalog 点击Add
|
|
普通属性的注入
1.新建一个Bean文件
-
-
private String strValue;private int intValue;private List listValue;private String[] arrayValue;private Set setValue;private Map mapValue;
|
生成相应的get和set方法 |
2.建立配置文件 applicationContext-Injection_Attribute.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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><bean id="injection" class="bean.Injection"><property name="strValue" value="fengda" /><property name="intValue" value="123" /><property name="listValue"><list><value>list1</value><value>list2</value></list></property><property name="arrayValue"><list><value>array1</value><value>array2</value></list></property><property name="setValue"><set><value>set1</value><value>set2</value></set></property><property name="mapValue"><map><entry key="k1" value="v1" /><entry key="k2" value="v2"/></map></property></bean></beans>
|
|
3.新建单元测试类InjectionTest
-
-
private BeanFactory factory;protected void setUp() throws Exception { this.factory = new ClassPathXmlApplicationContext( "applicationContext-Injection_Attribute.xml" );}public void testInjection(){ Injection injection = (Injection) this.factory.getBean( "injection" ); System.out.println( "strValue: " + injection.getStrValue() ); System.out.println( "intValue: " + injection.getIntValue() ); System.out.println( "listValue: " + injection.getListValue() ); System.out.println( "arrayValue: " + injection.getArrayValue() ); System.out.println( "mapValue: " + injection.getMapValue() ); System.out.println( "setValue" + injection.getSetValue() ); }
|
|
自定义属性编辑器
作用 |
将spring配置文件中的字符串转换成相应的对象进行注入 |
步骤 |
继承PropertyEditorSuppprt 重写:setAsTest(test) 将属性编辑器注册到spring中 |
实例 |
将字符串转换成时间格式输出 |
1.
新建Bean类Injection_Date.java |
private Date dateValue; 生成其get和set方法 |
2.
配置文件信息applicationContext-Injection_Date.xml |
-
-
<bean id="injection_Date" class="bean.Injection_Date"> <property name="dateValue"> <value>2008/11/01</value> </property> </bean>
|
3.
新建类:UtilDatePropertyEditor 继承PropertyEditorSuppprt 重写:setAsTest(test) |
-
-
private String format; public String getFormat() { return format; } public void setFormat(String format) { this.format = format; } public void setAsText(String text) throws IllegalArgumentException { SimpleDateFormat sdf = new SimpleDateFormat( format ); try { Date d = sdf.parse(text); this.setValue( d ); } catch (ParseException e) { e.printStackTrace(); } }
|
4.
注册属性编辑器 注入到CustomEditorConfigurer的Map中,有set方法就可以注入 |
-
-
<bean id="util" class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="java.util.Date"> <bean class="util.UtilDatePropertyEditor"> <property name="format"> <value>yyyy/MM/dd</value> </property> </bean> </entry> </map> </property> </bean>
|
5.
单元测试 |
-
-
private BeanFactory factory; protected void setUp() throws Exception { String[] xml = { "applicationContext-util.xml" , "applicationContext-Injection_Date.xml" }; this.factory = new ClassPathXmlApplicationContext( xml );
|
文章来源: blog.csdn.net,作者:fengda2870,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/fengda2870/article/details/3347699
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
评论(0)