美文网首页JAVA后台开发_从入门到精通
25 Spring由类型(Type)自动装配

25 Spring由类型(Type)自动装配

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

    在Spring中,“类型自动装配”的意思是如果一个bean的数据类型与其它bean属性的数据类型相同,将自动兼容装配它。
    例如,一个“persion” bean 公开以“ability”类数据类型作为属性,Spring会找到ability类相同的数据类型,并自动装配它的Bean。如果没有匹配找到,它什么也不做。
    可以通过自动装配 autowire="byType"象下面这样:

    <!-- person has a property type of class "ability" -->
    <bean id="person" class="com.gp6.autoAssemblyBean.Person" autowire="byType" />
        
    <bean id="invisible" class="com.gp6.autoAssemblyBean.Ability" >
        <property name="skill" value="Invisible" />
    </bean>
    
    

    看下面由Spring按类型自动装配的完整例子。

    1. Beans

    两个Bean,person 和 ability.

    package com.gp6.autoAssemblyBean.type;
    
    public class Ability {
        private String skill;
    
        public String getSkill() {
            return skill;
        }
    
        public void setSkill(String skill) {
            this.skill = skill;
        }
    }
    
    
    
    package com.gp6.autoAssemblyBean.type;
    
    public class Person {
        private Ability ability;
    
        public Ability getAbility() {
            return ability;
        }
    
        public void setAbility(Ability ability) {
            this.ability = ability;
        }
    }
    
    

    2. Spring Wiring

    通常情况下,明确地装配 bean:

    <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="person" class="com.gp6.autoAssemblyBean.type.Person">
            <property name="ability" ref="invisible" />
        </bean>
        
        <bean id="invisible" class="com.gp6.autoAssemblyBean.type.Ability" >
            <property name="skill" value="Invisible" />
        </bean>
        
    </beans>
    
    
    package com.gp6.autoAssemblyBean.type;
    
    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/type/applicationContext.xml");
    
            Person person = (Person)context.getBean("person");
            
            System.out.println(person.getAbility().getSkill());
            
        }
    }
    
    

    输出

    Invisible
    

    随着自动装配按类型启用后,可以保留ability属性未设置。Spring会发现相同的数据类型并自动装配它。

    
    <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="person" class="com.gp6.autoAssemblyBean.type.Person" autowire="byType">
        </bean>
        
        <bean id="invisible" class="com.gp6.autoAssemblyBean.type.Ability" >
            <property name="skill" value="Invisible" />
        </bean>
        
    </beans>
    
    

    输出

    Person [ability=Ability [skill=Invisible]]

    如果你有两个Bean,都是类“ability”相同的数据类型?

    <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="person" class="com.gp6.autoAssemblyBean.type.Person" autowire="byType">
        </bean>
        
        <bean id="invisible" class="com.gp6.autoAssemblyBean.type.Ability" >
            <property name="skill" value="Invisible" />
        </bean>
    
        <bean id="steal" class="com.gp6.autoAssemblyBean.type.Ability" >
            <property name="skill" value="Steal" />
        </bean>
        
    </beans>
    
    
    

    输出

    log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
    log4j:WARN Please initialize the log4j system properly.
    log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
    Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'person' defined in class path resource [com/gp6/autoAssemblyBean/type/applicationContext.xml]: Unsatisfied dependency expressed through bean property 'ability': : No unique bean of type [com.gp6.autoAssemblyBean.type.Ability] is defined: expected single matching bean but found 2: [invisible, steal]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.gp6.autoAssemblyBean.type.Ability] is defined: expected single matching bean but found 2: [invisible, steal]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1199)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1091)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
        at com.gp6.autoAssemblyBean.type.Test.main(Test.java:8)
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.gp6.autoAssemblyBean.type.Ability] is defined: expected single matching bean but found 2: [invisible, steal]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:800)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1184)
        ... 13 more
    
    

    相关文章

      网友评论

        本文标题:25 Spring由类型(Type)自动装配

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