美文网首页JAVA后台开发_从入门到精通
27 Spring由构造方法自动装配

27 Spring由构造方法自动装配

作者: 笑Skr人啊 | 来源:发表于2017-08-22 16:48 被阅读13次

    在Spring中,可以使用“通过构造自动装配”,实际上是按构造函数的参数类型自动装配。 这意味着,如果一个bean的数据类型与其他bean的构造器参数的数据类型是相同的,那么将自动装配。
    下面看看Spring构造函数自动装配的一个完整例子。

    1. Beans

    这里有两个 beans, 分别是:developer 和 language

    package com.gp6.autoAssemblyBean.constructor;
    
    public class Language {
        private String name;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    
    
    
    package com.gp6.autoAssemblyBean.constructor;
    
    public class Developer {
        private Language language;
        
        //构造函数
        public Developer(Language language) {
            this.language = language;
        }
    
        public Language getLanguage() {
            return language;
        }
    
        public void setLanguage(Language language) {
            this.language = language;
        }
    }
    
    
    
    
    
    <!-- 通过ref手动装配-->
    <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-2.5.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
        <!-- 由于Developer中有构造函数,此方法不适用 -->
        <!-- <bean id="developer" class="com.gp6.autoAssemblyBean.constructor.Developer" >
            <property name="language" ref="language" />
        </bean>
        
        <bean id="language" class="com.gp6.autoAssemblyBean.constructor.Language" >
            <property name="name" value="chinese" /> 
        </bean>
         -->
    
        <bean id="developer" class="com.gp6.autoAssemblyBean.constructor.Developer">
            <constructor-arg>
                <ref bean="language" />
            </constructor-arg>
        </bean>
            
        <bean id="language" class="com.gp6.autoAssemblyBean.constructor.Language" >
            <property name="name" value="Java" />
        </bean>
        
    </beans>  
    
    
    
    package com.gp6.autoAssemblyBean.constructor;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test {
        public static void main( String[] args ) {
            ApplicationContext context =  new ClassPathXmlApplicationContext("com/gp6/autoAssemblyBean/constructor/applicationContext.xml");
    
            Developer developer = (Developer)context.getBean("developer");
            
            System.out.println(developer.getLanguage().getName());
            
        }
    }
    
    
    

    随着自动装配用构造函数启用后,你可以不设置构造器属性。Spring会找到兼容的数据类型,并自动装配它。

    <!-- 通过类型Constructor装配 -->
    <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-2.5.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
        <bean id="developer" class="com.gp6.autoAssemblyBean.constructor.Developer" autowire="constructor" />
            
        <bean id="language" class="com.gp6.autoAssemblyBean.constructor.Language" >
            <property name="name" value="Java" />
        </bean>
        
    </beans> 
    
    

    相关文章

      网友评论

        本文标题:27 Spring由构造方法自动装配

        本文链接:https://www.haomeiwen.com/subject/zvmldxtx.html