相信看过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接口并实现setUs……