AUTOWIRE_NO的作用

本文探讨了AutowireCapableBeanFactory中的autowireMode参数如何影响Bean属性的注入过程。通过对比AutowireCapableBeanFactory.AUTOWIRE_NO与AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE两种模式,详细解析了它们的作用范围及其与@Autowired等注解的关系。

看公司框架里经常用到AutowireCapableBeanFactory.autowire(Class<?> beanClass, int autowireMode, boolean dependencyCheck)方式获得Bean


对于第二个参数,有时候用的AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, 有时候AutowireCapableBeanFactory.AUTOWIRE_NO


今天特地研究了一下。


AutowireCapableBeanFactory.AUTOWIRE_NO

	/**
	 * Constant that indicates no externally defined autowiring. Note that
	 * BeanFactoryAware etc and annotation-driven injection will still be applied.
	 * @see #createBean
	 * @see #autowire
	 * @see #autowireBeanProperties
	 */
	int AUTOWIRE_NO = 0;

先看一下这个说明。

这里其实已经说的比较明确了,不会对当前Bean进行外部类的注入,但是BeanFactoryAware和annotation-driven仍然会被应用

就是说Bean里面加了@Autowired的@Resource这类的依然会有作用


AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE

	/**
	 * Constant that indicates autowiring bean properties by type
	 * (applying to all bean property setters).
	 * @see #createBean
	 * @see #autowire
	 * @see #autowireBeanProperties
	 */
	int AUTOWIRE_BY_TYPE = 2;

会按照Bean Type进行property的注入


Example

@Data @EqualsAndHashCode
public class People {

    private Book book;
}

@Data @EqualsAndHashCode
@Component
public class Book {

    private String name;
}

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
//        People people = (People) context.getBean(People.class);
        AutowireCapableBeanFactory beanFactory = context.getAutowireCapableBeanFactory();
        People people = (People) beanFactory.autowire(People.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
        System.out.println(people.getBook());
    }

最终的结果是

Book(name=null) 即使在People里面并没有@Autowired Book


    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
//        People people = (People) context.getBean(People.class);
        AutowireCapableBeanFactory beanFactory = context.getAutowireCapableBeanFactory();
        People people = (People) beanFactory.autowire(People.class, AutowireCapableBeanFactory.AUTOWIRE_NO, false);
        System.out.println(people.getBook());
    }

最终的结果是

null

说明确实自动不会注入外部类

但是如果我们加上@Autowried


@Data @EqualsAndHashCode
public class People {

    @Autowired private Book book;
}

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
//        People people = (People) context.getBean(People.class);
        AutowireCapableBeanFactory beanFactory = context.getAutowireCapableBeanFactory();
        People people = (People) beanFactory.autowire(People.class, AutowireCapableBeanFactory.AUTOWIRE_NO, false);
        System.out.println(people.getBook());
    }
最终的结果是

Book(name=null)


Conclusion

AutowireCapableBeanFactory的autowireMode只作用Bean中没有被@Autowired @Resource等annotation修饰的properties

如果有@Autowired等修饰,autowireMode并不会影响他的作用


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值