Spring自动注入

来源:互联网 发布:linux免费杀毒软件 编辑:程序博客网 时间:2024/05/21 17:11

Spring提供了四种自动注入的方式,分别是byName,byType,constructor,autodetect。下面我们一一介绍。

 

首先介绍的是byName方式,试图在容器中寻找和需要自动装配的属性名相同的Bean(或ID)。

下面是一个示例。

我们定义一个接口Description.java:

view plain
  1. package com.spring.di;  
  2.   
  3. public interface Description {  
  4.     public void say();  
  5. }  


 

我们再定义一个接口的具体实现PeopleDescription.java:

view plain
  1. package com.spring.di;  
  2.   
  3. public class PeopleDescription implements Description{  
  4.   
  5.     public void say() {  
  6.         System.out.println("我今年23岁了");  
  7.     }  
  8.       
  9. }  

 

我们再定义一个接口People.java:

view plain
  1. package com.spring.di;  
  2.   
  3. public interface People {  
  4.     public void say();  
  5. }  


我们写一个具体实现Jack.java:

view plain
  1. package com.spring.di;  
  2.   
  3. public class Jack implements People{  
  4.     private Description description;  
  5.       
  6.     public void setDescription(Description description) {  
  7.         this.description = description;  
  8.     }  
  9.   
  10.     public void say() {  
  11.         description.say();  
  12.     }  
  13.   
  14. }  


我们写一个Spring的配置文件ditest.xml:

view plain
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.          xmlns:aop="http://www.springframework.org/schema/aop"  
  6.          xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.           http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  8.           http://www.springframework.org/schema/aop   
  9.           http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">  
  10.        
  11.      <bean id="description" class="com.spring.di.PeopleDescription"/>  
  12.      <bean id="jack" class="com.spring.di.Jack" autowire="byName"/>  
  13.            
  14. </beans>  



我们写一个测试类DiTest.java:

view plain
  1. package com.spring.di;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class DiTest {  
  7.       
  8.     public static void main(String[] args) {  
  9.         ApplicationContext ctx=new ClassPathXmlApplicationContext("ditest.xml");  
  10.         People p1=(People) ctx.getBean("jack");  
  11.         p1.say();  
  12.     }  
  13. }  


程序输出:

view plain
  1. 我今年23岁了  


 

 

下面我们介绍byType方式,试图在容器中寻找一个与需要自动注入的属性类型相同的Bean,限制是配置文件中只能有一个符合要求的Bean。

我们修改ditest.xml配置文件如下:

view plain
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.          xmlns:aop="http://www.springframework.org/schema/aop"  
  6.          xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.           http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  8.           http://www.springframework.org/schema/aop   
  9.           http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">  
  10.        
  11.      <bean id="des" class="com.spring.di.PeopleDescription"/>  
  12.      <bean id="jack" class="com.spring.di.Jack" autowire="byType"/>  
  13.            
  14. </beans>  


运行结果:

view plain
  1. 我今年23岁了  


 

下面我们介绍constructor,试图在容器中查找与需要自动装配的Bean的构造函数参数一致的一个或多个Bean。

我们修改Jack.java文件如下:

view plain
  1. package com.spring.di;  
  2.   
  3. public class Jack implements People{  
  4.     private Description description;  
  5.       
  6.     public Jack(Description description) {  
  7.         this.description = description;  
  8.     }  
  9.   
  10.     public void say() {  
  11.         description.say();  
  12.     }  
  13.   
  14. }  


我们修改ditest.xml文件如下:

view plain
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.          xmlns:aop="http://www.springframework.org/schema/aop"  
  6.          xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.           http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  8.           http://www.springframework.org/schema/aop   
  9.           http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">  
  10.        
  11.      <bean id="des" class="com.spring.di.PeopleDescription"/>  
  12.      <bean id="jack" class="com.spring.di.Jack" autowire="constructor"/>  
  13.            
  14. </beans>  


运行结果:

view plain
  1. 我今年23岁了  


 

最后一种方式是autodetect,首先尝试使用constructor来自动装配,然后使用byType方式。

这种方式与前面类似,不再演示。

原创粉丝点击