zoukankan      html  css  js  c++  java
  • spring自动装配bean有哪些方式?

     spring 配置文件中 <bean> 节点的 autowire 参数可以控制 bean 自动装配的方式

    • default - 默认的方式和 "no" 方式一样
    • no - 不自动装配,需要使用 <ref />节点或参数
    • byName - 根据名称进行装配
    • byType - 根据类型进行装配
    • constructor - 根据构造函数进行装配

    文档解释

    Attribute : autowire
    Controls whether bean properties are "autowired". This is an 
     automagical process in which bean references don't need to 
     be coded explicitly in the XML bean definition file, but rather 
     the Spring container works out dependencies. The effective 
     default is "no". There are 4 modes: 1. "no" The traditional 
     Spring default. No automagical wiring. Bean references must 
     be defined in the XML file via the <ref/> element (or "ref" 
     attribute). We recommend this in most cases as it makes 
     documentation more explicit. Note that this default mode also 
     allows for annotation-driven autowiring, if activated. "no" 
     refers to externally driven autowiring only, not affecting any 
     autowiring demands that the bean class itself expresses. 2. 
     "byName" Autowiring by property name. If a bean of class Cat 
     exposes a "dog" property, Spring will try to set this to the 
     value of the bean "dog" in the current container. If there is no 
     matching bean by name, nothing special happens. 3. "byType" 
     Autowiring if there is exactly one bean of the property type in 
     the container. If there is more than one, a fatal error is raised, 
     and you cannot use byType autowiring for that bean. If there is 
     none, nothing special happens. 4. "constructor" Analogous to 
     "byType" for constructor arguments. If there is not exactly one 
     bean of the constructor argument type in the bean factory, a 
     fatal error is raised. Note that explicit dependencies, i.e. 
     "property" and "constructor-arg" elements, always override 
     autowiring. Note: This attribute will not be inherited by child 
     bean definitions. Hence, it needs to be specified per concrete 
     bean definition. It can be shared through the 'default-autowire' 
     attribute at the 'beans' level and potentially inherited from 
     outer 'beans' defaults in case of nested 'beans' sections (e.g. 
     with different profiles).
     
    Data Type : string
    Default Value : default
    Enumerated Values : 
        - default
        - no
        - byName
        - byType
        - constructor

    代码示例

    1、no 方式

    spring 配置文件,使用 ref 参数注入 bean,必须要有对象的 setter 方法,这里即 Person 的 setFr 方法。

    没有 <property name="fr" ref="fr"></property> 因没有注入 fr 属性,会报空指针错误。

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
            
        <bean id="person" class="constxiong.interview.assemble.Person" autowire="no">
            <property name="fr" ref="fr"></property>
        </bean>
        <bean id="fr" class="constxiong.interview.assemble.FishingRod"></bean>
            
    </beans>

    鱼竿 bean

    package constxiong.interview.assemble;
     
     
    /**
     * 鱼竿
     * @author ConstXiong
     * @date 2019-07-17 09:53:15
     */
    public class FishingRod {
     
        /**
         * 被使用
         */
        public void used() {
            System.out.println("钓鱼...");
        }
    }

    人 bean

    package constxiong.interview.assemble;
     
     
    /**
     * 人
     * @author ConstXiong
     * @date 2019-07-17 09:54:56
     */
    public class Person {
     
        private FishingRod fr;
        
        /**
         * 钓鱼
         */
        public void fish() {
            fr.used();
        }
        
        public void setFr(FishingRod fr) {
            this.fr = fr;
        }
        
    }

    测试代码

    package constxiong.interview.assemble;
     
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
     
    public class AssembleTest {
     
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("spring_assemble.xml");
            Person person = (Person)context.getBean("person");
            person.fish();
        }
        
    }

    2、byName 也是需要相应的 setter 方法才能注入

    修改 spring 配置文件 autowire="byName"

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
            
        <bean id="person" class="constxiong.interview.assemble.Person" autowire="byName"></bean>
        <bean id="fr" class="constxiong.interview.assemble.FishingRod"></bean>
            
    </beans>
     

    3、byType 也是需要相应的 setter 方法才能注入

    修改 spring 配置文件 autowire="byType"

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
            
        <bean id="person" class="constxiong.interview.assemble.Person" autowire="byType"></bean>
        <bean id="fr" class="constxiong.interview.assemble.FishingRod"></bean>
            
    </beans>

    其他不变

    4、constructor 无需 setter 方法,需要通过 构造方法注入 bean

    修改 spring 配置文件autowire="byType"

    Person 类去除 setFr 方法,添加构造方法设置 fr 属性

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
            
        <bean id="person" class="constxiong.interview.assemble.Person" autowire="constructor"></bean>
        <bean id="fr" class="constxiong.interview.assemble.FishingRod"></bean>
            
    </beans>
    package constxiong.interview.assemble;
     
     
    /**
     * 人
     * @author ConstXiong
     * @date 2019-07-17 09:54:56
     */
    public class Person {
     
        private FishingRod fr;
        
        public Person(FishingRod fr) {
            this.fr = fr;
        }
        
        /**
         * 钓鱼
         */
        public void fish() {
            fr.used();
        }
        
    }

    1、2、3、4 的测试结果一致,打印

    钓鱼...


    原文链接
     


     

  • 相关阅读:
    验证授权【msdn】
    实战 Comet 应用程序开发
    ASP.NET Forms验证 实现子域名(SubDomain)共享登陆下的缺陷 [转]
    分享WordPress博客搜索引擎优化的六点经验 博客园 cnbogs
    支持支付宝(Alipay)付款的三个美国主机商
    认证,授权2
    登录代码,程序不是作文
    Google 的PageRank值对网站成功有多重要
    SQL Server 2005 Service Broker 初探 [摘抄]
    jQuerySelectors(选择器)的使用(四五、内容篇&可见性篇) cnblogs zhuan
  • 原文地址:https://www.cnblogs.com/ConstXiong/p/12123282.html
Copyright © 2011-2022 走看看