default-autowire使用

Posted vvii

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了default-autowire使用相关的知识,希望对你有一定的参考价值。

???? ??????

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

  • 复用的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使用的主要内容,如果未能解决你的问题,请参考以下文章

微信小程序代码片段

webstorm代码片段的创建

Spring中的bean

VIM 代码片段插件 ultisnips 使用教程

Android课程---Android Studio使用小技巧:提取方法代码片段

使用 Git 来管理 Xcode 中的代码片段