Spring 自动装配AutoWire

来源:互联网 发布:python 树莓派 编辑:程序博客网 时间:2024/05/15 07:41
引用:

在xml配置文件中,autowire有5种类型,可以在<bean/>元素中使用autowire属性指定

 

  • 模式                        说明   
  •  no                       不使用自动装配,必须通过ref元素指定依赖,默认设置。   
  • byName                    根据属性名自动装配。此选项将检查容器并根据名字查找与                   
  •                           属性完全一致的bean,并将其与属性自动装配。   
  • byType                    如果容器中存在一个与指定属性类型相同的bean,那么将与   
  •                           该属性自动装配;如果存在多个该类型bean,那么抛出异   
  •                           常,并指出不能使用byType方式进行自动装配;如果没有找   
  •                           到相匹配的bean,则什么事都不发生,也可以通过设置   
  •                           dependency-check="objects"让Spring抛出异常。   
  • constructor               与byType方式类似,不同之处在于它应用于构造器参数。如   
  •                           果容器中没有找到与构造器参数类型一致的bean,那么抛出   
  •                           异常。   
  • autodetect                通过bean类的自省机制(introspection)来决定是使用   
  •                           constructor还是byType方式进行自动装配。如果发现默认的   
  •                           构造器,那么将使用byType方式。 

    可以设置bean使自动装配失效: 
    采用xml格式配置bean时,将<bean/>元素的autowire-candidate属性设置为false,这样容器在查找自动装配对象时,将不考虑该bean,即它不会被考虑作为其它bean自动装配的候选者,但是该bean本身还是可以使用自动装配来注入其它bean的。


    测试:

    test文件:

    [java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
    1. package com.bjsxt.dao;  
    2.   
    3. import org.springframework.context.ApplicationContext;  
    4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
    5.   
    6. import com.bjsxt.model.User;  
    7. import com.bjsxt.service.UserService;  
    8.   
    9.   
    10.  class Test {  
    11.     public static void main(String[] args){  
    12.    ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");  
    13.           
    14.           
    15.         UserService service = (UserService)ctx.getBean("userService");  
    16.           
    17.         System.out.println(service.getUserDAO());  
    18.     }  
    19.   
    20. }  

    首先采用 "byName" 的方式.配置文件如下:

    [html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
    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.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
    5.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"  
    6.           >  
    7.   
    8.   <bean name="userDAO2" class="com.bjsxt.dao.impl.UserDAOImpl">  
    9.     <property name="daoId" value="1"></property>  
    10.   </bean>  
    11.     
    12.   <bean name="userDAO" class="com.bjsxt.dao.impl.UserDAOImpl">  
    13.     <property name="daoId" value="2"></property>  
    14.   </bean>  
    15.       
    16.   <bean id="userService" class="com.bjsxt.service.UserService" scope="prototype" autowire="byName">  
    17.     
    18.    <!--  autowire="byType"  
    19.    <property name="userDAO" ref="userDAO" />-->  
    20.   </bean>  
    21.     
    22.   
    23. </beans>  

    因为service中存在 名为userDAO的成员:

    [java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
    1. package com.bjsxt.service;  
    2. import com.bjsxt.dao.UserDAO;  
    3. import com.bjsxt.model.User;  
    4.   
    5.   
    6.   
    7. public class UserService {  
    8.       
    9.     private UserDAO userDAO;    
    10.     public void add(User user) {  
    11.         userDAO.save(user);  
    12.     }  
    13.     public UserDAO getUserDAO() {  
    14.         return userDAO;  
    15.     }  
    16.     public void setUserDAO(UserDAO userDAO) {  
    17.         this.userDAO = userDAO;  
    18.     }  
    19.       
    20.   
    21. }  

    所以采用byName方式匹配时, 选中的name为 userDAO的bean.

    Test输出如下:

    [java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
    1. <span style="background-color: rgb(255, 255, 255);"><span style="color:#ff6666;">INFO: Loading XML bean definitions from class path resource [beans.xml]  
    2. 五月 262014 10:42:23 上午 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory  
    3. INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@34b172ef]: org.springframework.beans.factory.support.DefaultListableBeanFactory@2ea8bf33  
    4. 五月 262014 10:42:23 上午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons  
    5. INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2ea8bf33: defining beans [userDAO2,userDAO,userService]; root of factory hierarchy</span></span><span style="color:#232323;">  
    6. daoId=2</span>  
    换成"byType"

    因为存在两个类型相同的bean. 程序报错

    [java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
    1. 五月 262014 10:45:23 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh  
    2. INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2c4a1908: display name [org.springframework.context.support.ClassPathXmlApplicationContext@2c4a1908]; startup date [Mon May 26 10:45:23 CST 2014]; root of context hierarchy  
    3. 五月 262014 10:45:23 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions  
    4. INFO: Loading XML bean definitions from class path resource [beans.xml]  
    5. 五月 262014 10:45:24 上午 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory  
    6. INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@2c4a1908]: org.springframework.beans.factory.support.DefaultListableBeanFactory@73354684  
    7. 五月 262014 10:45:24 上午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons  
    8. INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@73354684: defining beans [userDAO2,userDAO,userService]; root of factory hierarchy  
    9. Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService' defined in class path resource [beans.xml]: Unsatisfied dependency expressed through bean property 'userDAO': : No unique bean of type [com.bjsxt.dao.UserDAO] is defined: expected single matching bean but found 2: [userDAO2, userDAO]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.bjsxt.dao.UserDAO] is defined: expected single matching bean but found 2: [userDAO2, userDAO]  
    10.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1091)  
    11.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:982)  
    12.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)  
    13.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)  
    14.     at java.security.AccessController.doPrivileged(Native Method)  
    15.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)  
    16.     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:283)  
    17.     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)  
    18.     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)  
    19.     at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:880)  
    20.     at com.bjsxt.dao.Test.main(Test.java:15)  
    21. Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.bjsxt.dao.UserDAO] is defined: expected single matching bean but found 2: [userDAO2, userDAO]  
    22.     at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:621)  
    23.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1076)  
    24.     ... 10 more  


    在此基础上,如果将第二个bean的autowire-candidate 属性设为false:

    [java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
    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.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
    5.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"  
    6.           >  
    7.   
    8.   <bean name="userDAO2" class="com.bjsxt.dao.impl.UserDAOImpl">  
    9.     <property name="daoId" value="1"></property>  
    10.   </bean>  
    11.     
    12.   <bean name="userDAO" class="com.bjsxt.dao.impl.UserDAOImpl" autowire-candidate="false">  
    13.     <property name="daoId" value="2"></property>  
    14.   </bean>  
    15.       
    16.   <bean id="userService" class="com.bjsxt.service.UserService" scope="prototype" autowire="byType">  
    17.     
    18.    <!--  autowire="byType"  
    19.    <property name="userDAO" ref="userDAO" />-->  
    20.   </bean>  
    21.     
    22.   
    23. </beans>  

    则在匹配时只有第一个bean符合条件, 不发生冲突, 正常输出结果:

    [java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
    1. <span style="color:#ff6666;">五月 262014 10:46:46 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh  
    2. INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2c4a1908: display name [org.springframework.context.support.ClassPathXmlApplicationContext@2c4a1908]; startup date [Mon May 26 10:46:46 CST 2014]; root of context hierarchy  
    3. 五月 262014 10:46:46 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions  
    4. INFO: Loading XML bean definitions from class path resource [beans.xml]  
    5. 五月 262014 10:46:46 上午 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory  
    6. INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@2c4a1908]: org.springframework.beans.factory.support.DefaultListableBeanFactory@73354684  
    7. 五月 262014 10:46:46 上午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons  
    8. INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@73354684: defining beans [userDAO2,userDAO,userService]; root of factory hierarchy</span><span style="color:#232323;">  
    9. daoId=1</span>  

    AAA0010359
  • 0 0
    原创粉丝点击