码迷,mamicode.com
首页 > 其他好文 > 详细

default-autowire使用

时间:2019-12-27 23:28:43      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:edit   ready   ini   null   cto   end   结果   source   enc   

???? ??????

共五种值可供选择,属于全局的设置

  • 复用的BeanA类
public class BeanA {
    private BeanB beanB;
    private BeanC beanC;

    public BeanB getBeanB() {
        return beanB;
    }

    public BeanA setBeanB(BeanB beanB) {
        this.beanB = beanB;
        return this;
    }

    public BeanC getBeanC() {
        return beanC;
    }

    public BeanA setBeanC(BeanC beanC) {
        this.beanC = beanC;
        return this;
    }
}
  • 复用的Test 类
import com.vi.springbean.BeanA;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class Test {
    @org.junit.Test
    public void test() {
        String path = "spring-config.xml";
        BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(path));

        BeanA bean = beanFactory.getBean(BeanA.class);

        System.out.println("beanB  "+bean.getBeanB());
        System.out.println("beanC  "+bean.getBeanC());
    }
}

default-autowire="byType"

  • 情况1
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd"
       default-autowire="byType">


        <bean id="beanA" class="com.vi.springbean.BeanA"></bean>
    <bean id="beanBB" class="com.vi.springbean.BeanB"></bean>
    <bean id="beanC" class="com.vi.springbean.BeanD"></bean>
</beans>

运行结果:
beanB com.vi.springbean.BeanB@568bf312
beanC null

  • 情况2
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd"
       default-autowire="byType">


    <bean id="beanA" class="com.vi.springbean.BeanA"></bean>
    <bean id="beanBB"  class="com.vi.springbean.BeanB"></bean>
    <bean id="beanCC" class="com.vi.springbean.BeanB"></bean>
</beans>
  • 运行结果:
    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘beanA‘ defined in class path resource [spring-config.xml]: Unsatisfied dependency expressed through bean property ‘beanB‘; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type ‘com.vi.springbean.BeanB‘ available: expected single matching bean but found 2: beanBB,beanCC

说明可以找不到,但是不能存在多个类型一样的,匹配时与bean的id无关

default-autowire="byName"

  • 情况1
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd"
       default-autowire="byName">


    <bean id="beanA" class="com.vi.springbean.BeanA"></bean>
    <bean id="beanB" class="com.vi.springbean.BeanB"></bean>
    <bean id="beanC2" class="com.vi.springbean.BeanB"></bean>
</beans>

运行结果:
beanB com.vi.springbean.BeanB@5d47c63f
beanC null

可以找不到,且可以存在多个同类型的

  • 情况2
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd"
       default-autowire="byName">


    <bean id="beanA" class="com.vi.springbean.BeanA"></bean>
    <bean id="beanB" class="com.vi.springbean.BeanB"></bean>
    <bean id="beanC" class="com.vi.springbean.BeanD"></bean>
</beans>

运行结果:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘beanA‘ defined in class path resource [spring-config.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type ‘com.vi.springbean.BeanD‘ to required type ‘com.vi.springbean.BeanC‘ for property ‘beanC‘; nested exception is java.lang.IllegalStateException: Cannot convert value of type ‘com.vi.springbean.BeanD‘ to required type ‘com.vi.springbean.BeanC‘ for property ‘beanC‘: no matching editors or conversion strategy found
说明当通过name匹配到后,类型不能不符也是错误的

  • 情况3
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd"
       default-autowire="byName">


<bean id="beanA" class="com.vi.springbean.BeanA"></bean>
<bean id="beanB" class="com.vi.springbean.BeanB"></bean>
<bean id="beanB" class="com.vi.springbean.BeanC"></bean>

</beans>

运行结果:
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Bean name ‘beanB‘ is already used in this element
Offending resource: class path resource [spring-config.xml]

此时按照name去匹配的,bean的id和BeanA的中的属性 name一致即可注入,可以找不到,找到之后类型需要一致,xml中bean id的值需保证唯一性

default-autowire="constructor"

// TODO

default-autowire="default"

// TODO

default-autowire="no"

// TODO

default-autowire使用

标签:edit   ready   ini   null   cto   end   结果   source   enc   

原文地址:https://www.cnblogs.com/VVII/p/12109779.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!