ignoreDependencyInterface和ignoreDependencyType的作用?

 

什么是ignoreDependencyInterface和ignoreDependencyType?

​相信看过Spring源码的读者一定在遇到过这么两个方法:

/*** Ignore the given dependency interface for autowiring.* 忽略给定依赖接口的自动装配 * @see org.springframework.context.ApplicationContextAware*/void ignoreDependencyInterface(Class<?> ifc);/*** Ignore the given dependency type for autowiring:* 忽略给定依赖类型的自动装配* @param type the dependency type to ignore*/void ignoreDependencyType(Class<?> type);

只看官方文档的解释很可能理解错误,作者也去查阅了一些文章,但是描述的还是有点难以理解,于是自己实验证实了一下,话不多说,本文直接通过例子给大家详细证明这两个接口的意思和使用方法.

 

准备用来实验注入spring容器的bean

 

首先我们需要准备五个model,作为实验注入Spring容器中的实例对象,如下:

public class User {    }    //这个接口类用来测试ignoreDependencyInterface方法,看能否注入User    public interface UserAware {        void setUser(User user);    }    //这个用来测试ignoreDependencyType方法    public class Admin {    }    //这个就是和上面的做对比,不对它做任何处理    public class Role {    }    //实现了上面的UserAware接口并实现setUser方法,    //并且将User、Admin、Role属性注入   public class Person implements UserAware{    private User user;    private Admin admin;    private Role role;    public User getUser() {        return user;    }    //实现了UserAwre接口的setUser方法    public void setUser(User user) {        this.user = user;    }    public Admin getAdmin() {        return admin;    }    public void setAdmin(Admin admin) {        this.admin = admin;    }    public Role getRole() {        return role;    }    public void setRole(Role role) {        this.role = role;    }}

上面代码中各个对象之间的关系如下:

其中User、Admin、Role都是空的实例对象,UserAware是个接口,里面有一个setUser(User user)方法,Person类中有User、Admin、Role三个对象属性,并且实现了UserAwre接口和它的方法,然后对所有的属性添加get、set方法.

 

配置spring注入属性的xml文件

 

我们需要将上面的实例bean注入到Spring容器中,所以我们配置一个xml,以xml的方式将bean注入到spring容器中,如下:

<?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">    <!--这个default-autowire作者测试的时候并不会影响到测试结果,我们后面分析-->    <bean id = "user" class="ignore.model.User"></bean>    <bean id = "role" class="ignore.model.Role"></bean>    <bean id = "admin" class="ignore.model.Admin"></bean>    <bean id="person" class="ignore.model.Person"></bean></beans>

 

配置启动类

 

    @Test    public void testSimpleLoad(){        XmlBeanFactory beanFactory=new XmlBeanFactory(new ClassPathResource("ignore/beanFactoryTest.xml"));        //beanFactory.ignoreDependencyInterface(UserAware.class);//测试ignoreDependencyInterface方法作用        //beanFactory.ignoreDependencyType(Admin.class);//测试ignoreDependencyType方法作用        Person person = beanFactory.getBean("person", Person.class);        System.out.println("person"+person);    }

好了,所需要测试条件,我们都准备好,接下来我们按照下面5条的流程进行测试,每条我都会截图运行结果,我们通过结果就可以分析出最终的结果.

  1. 先测试不开启ignoreDependencyInterface方法和ignoreDependency方法的时候,获取person中的属性值有些哪些?

  2. 测试开启上面代码第五行,启动ignoreDependencyInterface(UserAware.class)方法后,获取person中的属性值有哪些?

  3. 测试关闭上面第五行,开启上面代码第六行,启动ignoreDependency(Admin.class)方法后,获取person中的属性值有哪些?

  4. 测试同时启用ignoreDependencyInterface方法和ignoreDependency方法后,获取的person中的属性值有哪些?

  5. 上面4的测试是依赖xml配置中default-autowire="byType"测试的,我们把它改成default-autowire="byName"后在测试一次第4条,结果又如何?

 

测试开始

 

1. 先测试不开启ignoreDependencyInterface方法和ignoreDependency方法的时候,获取person中的属性值有些哪些?运行结果截图如下:

图中我们可以看出,person中的三个属性值都能获取到.

2. 测试只开启ignoreDependencyInterface(UserAware.class)方法后,运行结果如下图:

上图中我们可以看到person对象中的user属性值为空,说明user属性值在开启了ignoreDependencyInterface(UserAware.class)方法后并不能自动装配到person中(注意,user只是不能自动注入到person对象中,Spring容器中依然有user实例),到这里我们可以得到一个小结论:ignoreDependencyInterface方法是忽略指定接口(UserAwre)的set方法的自动装配!

3.接下来测试只开启ignoreDependency(Admin.class)方法,运行结果如下图:

通过运行结果可以发现,person对象中的admin属性没有被自动装配到person对象中,所以我们得出一个小结论:ignoreDependency方法忽略指定依赖类型的自动装配,ignoreDependency和ignoreDependencyInterface不同的是一个前者按照class类型进行忽略自动装配,后者按照接口中的set方法进行忽略自动装配.

4.接下来我们同时开启ignoreDependency和ignoreDependencyInterface方法,运行截图如下:

上图中可以看出,user和admin都不能自动装配到person对象中.

5.接下来我们修改xml配置文件中的default-autowire="byType",修改为default-autowire="byName",然后在开启ignoreDependency和ignoreDependencyInterface方法的同时运行的结果截图如下:

通过运行结果我们可以分析出:ignoreDependency和ignoreDependencyInterface方法的忽略自动装配和自动绑定方式无关!

 

总结

 

好了通过上面的实验我们可以得出一下结论:

  1. ignoreDependencyInterface方法忽略指定依赖接口的自动装配指的是忽略接口中的set方法这样的自动装配方式

  2. ignoreDependency忽略指定依赖类型的自动装配指的是忽略参数class类型的自动装配.

  3. 以上两种忽略方式都和自动装配(自动绑定)的方式无关.

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值