Spring学习笔记1

来源:互联网 发布:软件维保协议 编辑:程序博客网 时间:2024/05/16 15:13

 

Spring学习笔记1

 

 

 

学习内容

配置Spring

普通属性的注入

自定义属性编辑器

 配置Spring

核心包

%Spring%/dist/spring.jar

日志记录

%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

Location:  

选择FileSystem 路径:Spring/dist/resources/spring-beans-2.5.xsd

KeyType:

Schema Location

Key: 

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

 

 

 

普通属性的注入

 1.新建一个Bean文件

 

  1. private String strValue;
  2. private int intValue;
  3. private List listValue;
  4. private String[] arrayValue;
  5. private Set setValue;
  6. private Map mapValue;

 

生成相应的get和set方法

 2.建立配置文件 applicationContext-Injection_Attribute.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="
  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  8. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  9. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
  10. <bean id="injection" class="bean.Injection">
  11. <property name="strValue" value="fengda" />
  12. <property name="intValue" value="123" />
  13. <property name="listValue">
  14. <list>
  15. <value>list1</value>
  16. <value>list2</value>
  17. </list>
  18. </property>
  19. <property name="arrayValue">
  20. <list>
  21. <value>array1</value>
  22. <value>array2</value>
  23. </list>
  24. </property>
  25. <property name="setValue">
  26. <set>
  27. <value>set1</value>
  28. <value>set2</value>
  29. </set>
  30. </property>
  31. <property name="mapValue">
  32. <map>
  33. <entry key="k1" value="v1" />
  34. <entry key="k2" value="v2"/>
  35. </map>
  36. </property>
  37. </bean>
  38. </beans>

 

 3.新建单元测试类InjectionTest

  1. private BeanFactory factory;
  2. protected void setUp() throws Exception {
  3.     this.factory =  new ClassPathXmlApplicationContext( 
  4.             "applicationContext-Injection_Attribute.xml" );
  5. }
  6. public void testInjection(){
  7.         
  8.     Injection injection = (Injection) this.factory.getBean( "injection" );
  9.     System.out.println( "strValue: " + injection.getStrValue() );
  10.     System.out.println( "intValue: " + injection.getIntValue() );
  11.     System.out.println( "listValue: " + injection.getListValue() );
  12.     System.out.println( "arrayValue: " + injection.getArrayValue() );
  13.     System.out.println( "mapValue: " + injection.getMapValue() );
  14.     System.out.println( "setValue" + injection.getSetValue() );
  15.     
  16. }

 

 

 

自定义属性编辑器

作用

将spring配置文件中的字符串转换成相应的对象进行注入

步骤

继承PropertyEditorSuppprt

重写:setAsTest(test)

将属性编辑器注册到spring中

实例

将字符串转换成时间格式输出

 1.

新建Bean类Injection_Date.java

private Date dateValue;

生成其get和set方法

 2.

配置文件信息applicationContext-Injection_Date.xml

  1.     <bean id="injection_Date" class="bean.Injection_Date">
  2.         <property name="dateValue">
  3.             <value>2008/11/01</value>
  4.         </property>
  5.     </bean>

 3.

新建类:UtilDatePropertyEditor

继承PropertyEditorSuppprt

重写:setAsTest(test)

  1.     private String format;
  2.     public String getFormat() {
  3.         return format;
  4.     }
  5.     public void setFormat(String format) {
  6.         this.format = format;
  7.     }
  8.     public void setAsText(String text) throws IllegalArgumentException {
  9.         SimpleDateFormat sdf = new SimpleDateFormat( format );
  10.         try {
  11.             Date d = sdf.parse(text);
  12.             this.setValue( d );
  13.         } catch (ParseException e) {
  14.             e.printStackTrace();
  15.         }
  16.     }

 4.

注册属性编辑器 注入到CustomEditorConfigurer的Map中,有set方法就可以注入

  1.     <bean id="util" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
  2.         <property name="customEditors">
  3.             <map>
  4.                 <entry key="java.util.Date">
  5.                     <!--内部Bean-->
  6.                     <bean class="util.UtilDatePropertyEditor">
  7.                         <property name="format">
  8.                             <value>yyyy/MM/dd</value>
  9.                         </property>
  10.                     </bean>
  11.                 </entry>
  12.             </map>
  13.         </property>
  14.     </bean>

 5.

单元测试

  1.     private BeanFactory factory;
  2.     
  3.     protected void setUp() throws Exception {
  4.         String[] xml = {  "applicationContext-util.xml" , 
  5.                 "applicationContext-Injection_Date.xml" };
  6.         this.factory = new ClassPathXmlApplicationContext( xml );
  7.         //构造方法支持字符串数组类型
  8.     }
  9.     public void testDate(){
  10.         Injection_Date date = (Injection_Date)this.factory.getBean( "injection_Date" );
  11.         System.out.println( "dateValue: " + date.getDateValue() );
  12.     }

原创粉丝点击