浅谈Spring入门

开发 后端
本文是Spring入门的文章,包括怎样启动Spring容器。Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的。

Spring入门—反射技术

无参数的

Java代码 

  1. Class.forName(className).newInstance;     
  2. //有参数的     
  3. Class.forName(className).getDeclaredConstructor(String.class).newInstance(“黎明”);     
  4. //通过反射获取属性     
  5. Introspector.getBeanInfo(Person.class).getPropertyDescriptors()     
  6. //通过反射机制修改bean属性的值     
  7. Person person=(Person)Class.forName(className).getDeclaredConstructor(String.class).newInstance("黎明");     
  8. PropertyDescriptor[] ps = Introspector.getBeanInfo(Person.class).getPropertyDescriptors();     
  9. for(PropertyDescriptor p :ps){     
  10. System.out.println(p.getName());     
  11. if(p.getName().equals("name")){     
  12. Method setter=p.getWriteMethod();     
  13. if(setter!=null){     
  14. setter.setAccessible(true);//允许访问private属性     
  15. setter.invoke(person, "小燕子");     
  16.     
  17. //通过反射机制修改bean字段的值     
  18. Field field=Person.class.getDeclaredField("name");     
  19. field.setAccessible(true);//允许访问private字段     
  20. field.set( person , "sss");     
  21.      
  22.  
  23.    
  24. Class.forName(className).newInstance;  
  25. //有参数的  
  26. Class.forName(className).getDeclaredConstructor(String.class).newInstance(“黎明”);  
  27. //通过反射获取属性  
  28. Introspector.getBeanInfo(Person.class).getPropertyDescriptors()  
  29. //通过反射机制修改bean属性的值  
  30. Person person=(Person)Class.forName(className).getDeclaredConstructor(String.class).newInstance("黎明");  
  31. PropertyDescriptor[] ps = Introspector.getBeanInfo(Person.class).getPropertyDescriptors();  
  32. for(PropertyDescriptor p :ps){  
  33. System.out.println(p.getName());  
  34. if(p.getName().equals("name")){  
  35. Method setter=p.getWriteMethod();  
  36. if(setter!=null){  
  37. setter.setAccessible(true);//允许访问private属性  
  38. setter.invoke(person, "小燕子");  
  39.  
  40. //通过反射机制修改bean字段的值  
  41. Field field=Person.class.getDeclaredField("name");  
  42. field.setAccessible(true);//允许访问private字段  
  43. field.set( person , "sss"); 

Spring提供了声明式的事务管理

软件的解耦合,不是硬编码

Spring 需要的jar

Dist\spring.jar

lib\jakarta-commons\commons-logging.jar

如果使用了切面编程(AOP),还需要下列jar文件

  1. lib/aspectj/aspectjweaver.jar和aspectjrt.jar   
  2. lib/cglib/cglib-nodep-2.1_3.jar  

如果使用了JSR-250中的注解,如@Resource/@PostConstruct/@PreDestroy,还需要下列jar文件

lib\j2ee\common-annotations.jar

配置文件beans.xml

Java代码

  1. <?xml version="1.0" encoding="UTF-8"?>     
  2. t;beans xmlns="http://www.springframework.org/schema/beans"    
  3.      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  4.      xsi:schemaLocation="http://www.springframework.org/schema/beans     
  5.          http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">     
  6.     
  7. <bean id="xx" class="junit.test.Person" lazy-init="true">     
  8. </bean>     
  9. t;/beans>    
  10.  
  11.   <?xml version="1.0" encoding="UTF-8"?> 
  12. <beans xmlns="http://www.springframework.org/schema/beans" 
  13.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  14.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  15.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
  16.  
  17.   <bean id="xx" class="junit.test.Person" lazy-init="true"> 
  18.   </bean> 
  19. </beans> 

怎么启动spring容器Java代码 

  1. ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");     
  2. Person s = (Person)context.getBean("xx");    
  3.  
  4.    
  5. ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");  
  6. Person s = (Person)context.getBean("xx"); 


默认bean是容器启动时实例化,只在容器中创建一次,spring中的对象一直存在容器中,是单例模式

表 3.4. Bean作用域

作用域       描述
singleton    在每个Spring IoC容器中一个bean定义对应一个对象实例。
prototype    一个bean定义对应多个对象实例。
request       在一次HTTP请求中,一个bean定义对应一个实例;即每次HTTP请求将会有各自的bean实例, 它们依据某个bean定义创建而成。该作用域仅在基于web的Spring ApplicationContext情形下有效。
session        在一个HTTP Session中,一个bean定义对应一个实例。该作用域仅在基于web的Spring ApplicationContext情形下有效。
global session     在一个全局的HTTP Session中,一个bean定义对应一个实例。典型情况下,仅在使用portlet context的时候有效。该作用域仅在基于web的Spring ApplicationContext情形下有效。

Spring入门—利用工厂方法创建bean

Java代码 
 

  1. public class PersonFactory {     
  2. public static Person createPerson(){     
  3.     return new Person();     
  4. }     
  5. public Person createPerson2(){     
  6.     return new Person();     
  7. }     
  8.  
  9.   public class PersonFactory {  
  10.  public static Person createPerson(){  
  11.   return new Person();  
  12.  }  
  13.  public Person createPerson2(){  
  14.   return new Person();  
  15.  }  

Java代码 

  1. //使用静态工厂方法实例化     
  2. <bean id="person" class="bean.PersonFactory" factory-method="createPerson"></bean>     
  3. 使用实例工厂方法实例化     
  4. <bean id="personFactory" class="bean.PersonFactory"></bean>     
  5.  <bean id="person2" factory-bean="personFactory" factory-method="createPerson2"></bean>    
  6.  
  7.     
  8. //使用静态工厂方法实例化  
  9. <bean id="person" class="bean.PersonFactory" factory-method="createPerson"></bean> 
  10. 使用实例工厂方法实例化  
  11. <bean id="personFactory" class="bean.PersonFactory"></bean> 
  12.  <bean id="person2" factory-bean="personFactory" factory-method="createPerson2"></bean> 

Spring入门—依赖注入

Java代码 

  1. public class Person {     
  2.     
  3.     private Integer id;     
  4.     private String name="aa";     
  5.     private IDCard idcard;     
  6.          
  7.     public IDCard getIdcard() {     
  8.         return idcard;     
  9.     }     
  10.     
  11.     public void setIdcard(IDCard idcard) {     
  12.         this.idcard = idcard;     
  13.     }     
  14.     
  15.     public Person(String name) {     
  16.         this.name = name;     
  17.     }     
  18.          
  19.     public Person() {     
  20.     }     
  21.     public Integer getId() {     
  22.         return id;     
  23.     }     
  24.     public void setId(Integer id) {     
  25.         this.id = id;     
  26.     }     
  27.     public String getName() {     
  28.         return name;     
  29.     }     
  30.     public void setName(String name) {     
  31.         this.name = name;     
  32.     }     
  33.          
  34. }     
  35. public class IDCard {     
  36.     private String no;     
  37.     
  38.     public String getNo() {     
  39.         return no;     
  40.     }     
  41.     
  42.     public void setNo(String no) {     
  43.         this.no = no;     
  44.     }     
  45. }    
  46.  
  47.     
  48. public class Person {  
  49.  
  50.  private Integer id;  
  51.  private String name="aa";  
  52.  private IDCard idcard;  
  53.    
  54.  public IDCard getIdcard() {  
  55.   return idcard;  
  56.  }  
  57.  
  58.  public void setIdcard(IDCard idcard) {  
  59.   this.idcard = idcard;  
  60.  }  
  61.  
  62.  public Person(String name) {  
  63.   this.name = name;  
  64.  }  
  65.    
  66.  public Person() {  
  67.  }  
  68.  public Integer getId() {  
  69.   return id;  
  70.  }  
  71.  public void setId(Integer id) {  
  72.   this.id = id;  
  73.  }  
  74.  public String getName() {  
  75.   return name;  
  76.  }  
  77.  public void setName(String name) {  
  78.   this.name = name;  
  79.  }  
  80.    
  81. }  
  82. public class IDCard {  
  83.  private String no;  
  84.  
  85.  public String getNo() {  
  86.   return no;  
  87.  }  
  88.  
  89.  public void setNo(String no) {  
  90.   this.no = no;  
  91.  }  


***种方法
Java代码

  1. <bean id="xx" class="junit.test.Person" lazy-init="true">     
  2. <property name="idcard">     
  3. <bean class="junit.test.IDCard">     
  4.    <property name="no" value="9999"></property>     
  5.    </bean>     
  6.  </property>     
  7. </bean>    
  8.  
  9.   <bean id="xx" class="junit.test.Person" lazy-init="true"> 
  10.   <property name="idcard"> 
  11.   <bean class="junit.test.IDCard"> 
  12.      <property name="no" value="9999"></property> 
  13.      </bean> 
  14.    </property> 
  15.   </bean> 

第二种方法

Java代码 
   

  1. <bean id="aa" class="junit.test.IDCard">     
  2. <property name="no" value="88888888"></property>     
  3. </bean>     
  4.  <bean id="xx" class="junit.test.Person" lazy-init="true">     
  5.   <property name="idcard" ref="aa">     
  6.         </property>     
  7.   </bean>    
  8. <bean id="aa" class="junit.test.IDCard"> 
  9. <property name="no" value="88888888"></property> 
  10. </bean> 
  11. <bean id="xx" class="junit.test.Person" lazy-init="true"> 
  12.   <property name="idcard" ref="aa"> 
  13.   </property> 
  14.   </bean> 

为属性配置null值

Java代码 
 

  1. <property name="name"><null/></property>     
  2.     
  3.     
  4. public class Person {     
  5.     private String name="ss";     
  6. public Person(){}     
  7.     public Person(String name) {     
  8.         this.name = name;     
  9.     }     
  10.     public String getName() {     
  11.         return name;     
  12.     }     
  13.     public void setName(String name) {     
  14.         this.name = name;     
  15.     }     
  16.          
  17.     public void say(){     
  18.         System.out.println("我说了");     
  19.     }     
  20. }    
  21.  
  22.  <property name="name"><null/></property> 
  23.  
  24.  
  25. public class Person {  
  26.  private String name="ss";  
  27. public Person(){}  
  28.  public Person(String name) {  
  29.   this.name = name;  
  30.  }  
  31.  public String getName() {  
  32.   return name;  
  33.  }  
  34.  public void setName(String name) {  
  35.   this.name = name;  
  36.  }  
  37.    
  38.  public void say(){  
  39.   System.out.println("我说了");  
  40.  }  

初始化bean执行say方法相当于测试单元的@BeforeClass

  1. <bean id="xxx" class="bean.Person" scope="singleton" lazy-init="false" init-method="say">  

集合依赖注入

Java代码 
 

  1. <property name="lists">     
  2.          <list>     
  3.             <value>1111</value>     
  4.             <value>2222</value>     
  5.             <value>3333</value>     
  6.             <value>4444</value>     
  7.          </list>     
  8. </property>     
  9. for(String s : p.getLists){     
  10.             System.out.println(s);     
  11.         }     
  12. <property name="sets">     
  13.          <set>     
  14.             <value>TTT</value>     
  15.             <value>YYY</value>     
  16.           </set>     
  17. </property>     
  18. for(String s : p.getSets){     
  19.             System.out.println(s);     
  20.         }     
  21.     
  22. <property name="maps">     
  23.       <map>     
  24.             <entry key="key1" value="value1"></entry>     
  25.             <entry key="key2" value="value2"></entry>     
  26.        </map>     
  27. </property>     
  28. for(String key : p.getMaps().keySet()){     
  29.             System.out.println(key+"="+ p. getMaps ().get(key));     
  30.         }     
  31. Properties 是注入     
  32. <property name="propers">     
  33.        <props>     
  34.             <prop key="proper1">value1</prop>     
  35.             <prop key="proper2">value2</prop>     
  36.        </props>     
  37. </property>     
  38. for(Object key : p.getPropers().keySet()){     
  39.             System.out.println(key+"="+ p.getPropers().get(key));     
  40.         }   

【编辑推荐】

  1. Struts2教程:拦截器概述
  2. Struts2教程:上传任意多个文件
  3. Struts2教程:在Action类中获得HttpServletResponse对象
  4. Struts2教程:使用Validation框架验证数据
  5. Struts2教程:使用validate方法验证数据
责任编辑:彭凡 来源: javaeye
相关推荐

2009-07-14 18:28:58

Swing入门

2009-06-30 17:28:08

JSP学习

2009-06-29 17:54:47

Spring事务隔离

2009-07-15 11:04:02

MyEclipse F

2009-06-26 14:04:15

Quartz配置

2009-06-30 09:55:24

Spring运作机制

2011-06-28 14:02:34

QT ARM

2009-06-29 17:17:57

Spring

2011-02-28 13:34:51

SpringMVC

2009-03-06 14:34:31

StrutsHibernateSpring

2009-07-10 15:24:33

MyEclipse开发Spring

2009-06-22 14:03:00

java教材程序设计

2009-09-27 09:29:56

OSGi和SpringSpring动态模型Spring DM

2009-06-25 14:45:05

Spring2.5

2009-06-29 15:51:48

Spring容器

2009-07-23 13:30:46

JDBC事务

2012-02-14 14:05:59

JavaSpring

2009-06-11 10:37:58

netbeans spMVC基础

2010-05-12 16:25:07

Subversion入

2010-04-20 10:23:06

Oracle入门
点赞
收藏

51CTO技术栈公众号