spring 2.5 自动装配

 

 

  
  
  
  
  1. 有4种自动装配类型 
  2.     byname:在容器(说的应该是spring容器,即是在spring的配置文件中找,如果不是注解方式的话)中寻找和需要自动装配属性名(name)相同的bean或id如果没有找到相符的bean,该属性就没被装配 
  3.     个人理解:默认的好像就是按属性名装配,举例说明 
  4.     <?xml version="1.0" encoding="UTF-8"?> 
  5.     <beans 
  6.         xmlns="http://www.springframework.org/schema/beans" 
  7.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  8.         xmlns:aop="http://www.springframework.org/schema/aop" 
  9.         xmlns:tx="http://www.springframework.org/schema/tx" 
  10.         xmlns:p="http://www.springframework.org/schema/p" 
  11.         xsi:schemaLocation="http://www.springframework.org/schema/beans  
  12.                         http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
  13.                         http://www.springframework.org/schema/context   
  14.                         http://www.springframework.org/schema/context/spring-context-2.5.xsd 
  15.                             http://www.springframework.org/schema/aop 
  16.                         http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
  17.                             http://www.springframework.org/schema/tx 
  18.                         http://www.springframework.org/schema/tx/spring-tx-2.5.xsd                       
  19.                         " 
  20.                         default-autowire="byType" 
  21.                         > 
  22.      
  23.  
  24.         <bean id="kk"  class="test.ZTAction" autowire="byName"> 
  25.         </bean> 
  26.  
  27.         <bean id="p" class="Person"> 
  28.         </bean> 
  29.     </beans> 
  30.     上面这个xml例子里有一个bean kk,他的自动装配属性是按名称装配,也就是说如果类test.ZTAction中含有属性Person p,并且在spring容器中有一个bean的id或name为p,而且它的类型为Person 
  31.     那么类test.ZTAction初始化时,它的属性Person也会初始化 
  32.  
  33.  
  34.     byType:在容器中寻找一个与需要自动装配的属性类型相同的bean;如果没找到相符的bena,该属性就没有被装配上,如果找到超过一个相符的bean抛出异常 
  35.     <?xml version="1.0" encoding="UTF-8"?> 
  36.     <beans 
  37.         xmlns="http://www.springframework.org/schema/beans" 
  38.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  39.         xmlns:aop="http://www.springframework.org/schema/aop" 
  40.         xmlns:tx="http://www.springframework.org/schema/tx" 
  41.         xmlns:p="http://www.springframework.org/schema/p" 
  42.         xsi:schemaLocation="http://www.springframework.org/schema/beans  
  43.                         http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
  44.                         http://www.springframework.org/schema/context   
  45.                         http://www.springframework.org/schema/context/spring-context-2.5.xsd 
  46.                             http://www.springframework.org/schema/aop 
  47.                         http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
  48.                             http://www.springframework.org/schema/tx 
  49.                         http://www.springframework.org/schema/tx/spring-tx-2.5.xsd                       
  50.                         " 
  51.                         default-autowire="byType" 
  52.                         >    
  53.         <bean   class="test.TestAction" > 
  54.             <property name="st" > 
  55.                 <value>ok</value> 
  56.             </property>  
  57.         </bean> 
  58.  
  59.         <bean id="kk" class="test.ZTAction" autowire="byType"> 
  60.         </bean>  
  61.     </beans> 
  62.     注意:要在被装配的对象上加autowire 
  63.     上面这个xml例子里有一个bean kk 他的自动装配属性是按类型装配,也就是说类test.ZTAction中含有属性test.TestAction,并且在spring容器中有一个bean的class为test.TestAction 
  64.     那么类test.ZTAction初始化时,他的属性test.TestAction也会初始化 

 

你可能感兴趣的:(spring,职场,配置文件,休闲,装配)