自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

淺談Spring入門

開發(fā) 后端
本文是Spring入門的文章,包括怎樣啟動(dòng)Spring容器。Spring 是一個(gè)開源框架,是為了解決企業(yè)應(yīng)用程序開發(fā)復(fù)雜性而創(chuàng)建的。

Spring入門—反射技術(shù)

無參數(shù)的

Java代碼 

  1. Class.forName(className).newInstance;     
  2. //有參數(shù)的     
  3. Class.forName(className).getDeclaredConstructor(String.class).newInstance(“黎明”);     
  4. //通過反射獲取屬性     
  5. Introspector.getBeanInfo(Person.class).getPropertyDescriptors()     
  6. //通過反射機(jī)制修改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. //通過反射機(jī)制修改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. //有參數(shù)的  
  26. Class.forName(className).getDeclaredConstructor(String.class).newInstance(“黎明”);  
  27. //通過反射獲取屬性  
  28. Introspector.getBeanInfo(Person.class).getPropertyDescriptors()  
  29. //通過反射機(jī)制修改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. //通過反射機(jī)制修改bean字段的值  
  41. Field field=Person.class.getDeclaredField("name");  
  42. field.setAccessible(true);//允許訪問private字段  
  43. field.set( person , "sss"); 

Spring提供了聲明式的事務(wù)管理

軟件的解耦合,不是硬編碼

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> 

怎么啟動(dòng)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"); 


默認(rèn)bean是容器啟動(dòng)時(shí)實(shí)例化,只在容器中創(chuàng)建一次,spring中的對象一直存在容器中,是單例模式

表 3.4. Bean作用域

作用域       描述
singleton    在每個(gè)Spring IoC容器中一個(gè)bean定義對應(yīng)一個(gè)對象實(shí)例。
prototype    一個(gè)bean定義對應(yīng)多個(gè)對象實(shí)例。
request       在一次HTTP請求中,一個(gè)bean定義對應(yīng)一個(gè)實(shí)例;即每次HTTP請求將會(huì)有各自的bean實(shí)例, 它們依據(jù)某個(gè)bean定義創(chuàng)建而成。該作用域僅在基于web的Spring ApplicationContext情形下有效。
session        在一個(gè)HTTP Session中,一個(gè)bean定義對應(yīng)一個(gè)實(shí)例。該作用域僅在基于web的Spring ApplicationContext情形下有效。
global session     在一個(gè)全局的HTTP Session中,一個(gè)bean定義對應(yīng)一個(gè)實(shí)例。典型情況下,僅在使用portlet context的時(shí)候有效。該作用域僅在基于web的Spring ApplicationContext情形下有效。

Spring入門—利用工廠方法創(chuàng)建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. //使用靜態(tài)工廠方法實(shí)例化     
  2. <bean id="person" class="bean.PersonFactory" factory-method="createPerson"></bean>     
  3. 使用實(shí)例工廠方法實(shí)例化     
  4. <bean id="personFactory" class="bean.PersonFactory"></bean>     
  5.  <bean id="person2" factory-bean="personFactory" factory-method="createPerson2"></bean>    
  6.  
  7.     
  8. //使用靜態(tài)工廠方法實(shí)例化  
  9. <bean id="person" class="bean.PersonFactory" factory-method="createPerson"></bean> 
  10. 使用實(shí)例工廠方法實(shí)例化  
  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執(zhí)行say方法相當(dāng)于測試單元的@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教程:上傳任意多個(gè)文件
  3. Struts2教程:在Action類中獲得HttpServletResponse對象
  4. Struts2教程:使用Validation框架驗(yàn)證數(shù)據(jù)
  5. Struts2教程:使用validate方法驗(yàn)證數(shù)據(jù)
責(zé)任編輯:彭凡 來源: javaeye
相關(guān)推薦

2009-07-14 18:28:58

Swing入門

2009-06-30 17:28:08

JSP學(xué)習(xí)

2009-06-29 17:54:47

Spring事務(wù)隔離

2009-07-15 11:04:02

MyEclipse F

2009-06-30 09:55:24

Spring運(yùn)作機(jī)制

2009-06-26 14:04:15

Quartz配置

2011-06-28 14:02:34

QT ARM

2009-06-29 17:17:57

Spring

2011-02-28 13:34:51

SpringMVC

2009-06-22 14:03:00

java教材程序設(shè)計(jì)

2009-03-06 14:34:31

StrutsHibernateSpring

2009-07-10 15:24:33

MyEclipse開發(fā)Spring

2009-06-25 14:45:05

Spring2.5

2009-09-27 09:29:56

OSGi和SpringSpring動(dòng)態(tài)模型Spring DM

2010-05-12 16:25:07

Subversion入

2010-04-20 10:23:06

Oracle入門

2009-06-29 15:51:48

Spring容器

2009-07-23 13:30:46

JDBC事務(wù)

2023-10-08 08:28:10

Spring事務(wù)管理

2009-06-11 10:37:58

netbeans spMVC基礎(chǔ)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)