简单的注入
准备bean
static class Bean1 {
@Autowired
private Bean2 bean2;
@Autowired
public void setBean2(Bean2 bean2) {
this.bean2 = bean2;
}
@Autowired
private Optional<Bean2> bean3;
@Autowired
private ObjectFactory<Bean2> bean4;
@Autowired
@Lazy
private Bean2 bean5;
}
@Component("bean2")
static class Bean2 {
}
处理注入
@Configuration
public class TestAutowired {
@SneakyThrows
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(TestAutowired.class);
DefaultListableBeanFactory beanFactory = applicationContext.getDefaultListableBeanFactory();
DependencyDescriptor dd1 = new DependencyDescriptor(Bean1.class.getDeclaredField("bean2"), true);
System.out.println(beanFactory.doResolveDependency(dd1, "bean1", null, null));
DependencyDescriptor dd2 = new DependencyDescriptor(new MethodParameter(Bean1.class.getMethod("setBean2", Bean2.class), 0), true);
System.out.println(beanFactory.doResolveDependency(dd2, "bean1", null, null));
DependencyDescriptor dd3 = new DependencyDescriptor(Bean1.class.getDeclaredField("bean3"), true);
System.out.println("bean3类型:" + dd3.getDependencyType());
if (dd3.getDependencyType() == Optional.class) {
dd3.increaseNestingLevel();
System.out.println("内嵌级别+1后:" + dd3.getDependencyType());
Object ret = beanFactory.doResolveDependency(dd3, "bean1", null, null);
System.out.println(Optional.ofNullable(ret));
}
DependencyDescriptor dd4 = new DependencyDescriptor(Bean1.class.getDeclaredField("bean4"), true);
System.out.println("bean4类型:" + dd4.getDependencyType());
if (dd4.getDependencyType() == ObjectFactory.class) {
dd4.increaseNestingLevel();
System.out.println("内嵌级别+1后:" + dd4.getDependencyType());
Object ret = beanFactory.doResolveDependency(dd4, "bean1", null, null);
ObjectFactory objectFactory = new ObjectFactory() {
@Override
public Object getObject() throws BeansException {
return ret;
}
};
System.out.println(objectFactory.getObject());
}
DependencyDescriptor dd5 = new DependencyDescriptor(Bean1.class.getDeclaredField("bean5"), true);
ContextAnnotationAutowireCandidateResolver resolver = new ContextAnnotationAutowireCandidateResolver();
resolver.setBeanFactory(beanFactory);
Object proxy = resolver.getLazyResolutionProxyIfNecessary(dd5, "bean1");
System.out.println(proxy);
System.out.println(proxy.getClass());
}
}
复杂的注入
准备bean
interface Service {
}
@Component("service1")
static class Service1 implements Service {
}
@Primary
@Component("service2")
static class Service2 implements Service {
}
@Component("service3")
static class Service3 implements Service {
}
interface DAO<T> {
}
static class Student {
}
static class Teacher {
}
@Component("DAO1")
static class DAO1 implements DAO<Student> {
}
@Component("DAO2")
static class DAO2 implements DAO<Teacher> {
}
目标类
static class Target {
@Autowired
private Service[] serviceArr;
@Autowired
private List<Service> serviceList;
@Autowired
private ConfigurableApplicationContext applicationContext;
@Autowired
private DAO<Teacher> dao;
@Autowired
@Qualifier("service2")
private Service service;
}
注入-数组
@SneakyThrows
private static void testArray(DefaultListableBeanFactory beanFactory) {
DependencyDescriptor serviceArr = new DependencyDescriptor(Target.class.getDeclaredField("serviceArr"), true);
if (serviceArr.getDependencyType().isArray()) {
Class<?> componentType = serviceArr.getDependencyType().getComponentType();
System.out.println("数组类的真实类型:" + componentType);
String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, componentType);
List<Object> retBeans = new ArrayList<>();
for (String beanName : beanNames) {
System.out.println("findBean=>" + beanName);
Object ret = serviceArr.resolveCandidate(beanName, componentType, beanFactory);
retBeans.add(ret);
}
Object ret = beanFactory.getTypeConverter().convertIfNecessary(retBeans, serviceArr.getDependencyType());
System.out.println("最终注入的数组:" + ret);
}
}
注入-list
@SneakyThrows
private static void testList(DefaultListableBeanFactory beanFactory) {
DependencyDescriptor serviceList = new DependencyDescriptor(Target.class.getDeclaredField("serviceList"), true);
if (serviceList.getDependencyType() == List.class) {
Class<?> resolveType = serviceList.getResolvableType().getGeneric().resolve();
System.out.println("list的真实类型:" + resolveType);
String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, resolveType);
List<Object> ret = new ArrayList<>();
for (String beanName : beanNames) {
Object bean = serviceList.resolveCandidate(beanName, resolveType, beanFactory);
ret.add(bean);
}
System.out.println(ret);
}
}
注入-ApplicationContext
@SneakyThrows
private static void testApplicationContext(DefaultListableBeanFactory beanFactory) {
DependencyDescriptor applicationContext = new DependencyDescriptor(Target.class.getDeclaredField("applicationContext"), true);
Field resolvableDependencies = DefaultListableBeanFactory.class.getDeclaredField("resolvableDependencies");
resolvableDependencies.setAccessible(true);
Map<Class<?>, Object> resolvableDependencyMap = (Map<Class<?>, Object>) resolvableDependencies.get(beanFactory);
resolvableDependencyMap.forEach((k, v) -> {
System.out.println("key: " + k + ", value:" + v);
});
for (Map.Entry<Class<?>, Object> entry : resolvableDependencyMap.entrySet()) {
if (entry.getKey().isAssignableFrom(applicationContext.getDependencyType())) {
System.out.println("找到了applicationContext:" + entry.getValue());
break;
}
}
}
注入-泛型
@SneakyThrows
private static void testGeneric(DefaultListableBeanFactory beanFactory) {
DependencyDescriptor dao = new DependencyDescriptor(Target.class.getDeclaredField("dao"), true);
Class<?> dependencyType = dao.getDependencyType();
ContextAnnotationAutowireCandidateResolver resolver = new ContextAnnotationAutowireCandidateResolver();
resolver.setBeanFactory(beanFactory);
String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, dependencyType);
for (String beanName : beanNames) {
System.out.println("找到beanName:" + beanName);
BeanDefinition beanDefinition = beanFactory.getMergedBeanDefinition(beanName);
if (resolver.isAutowireCandidate(new BeanDefinitionHolder(beanDefinition, beanName), dao)) {
System.out.println("当前beanName:" + beanName + "匹配");
System.out.println(dao.resolveCandidate(beanName, dependencyType, beanFactory));
} else {
System.out.println("当前beanName:" + beanName + "不匹配");
}
}
}
注入-@Qualifier
@SneakyThrows
private static void testQualifier(DefaultListableBeanFactory beanFactory) {
DependencyDescriptor service = new DependencyDescriptor(Target.class.getDeclaredField("service"), true);
Class<?> dependencyType = service.getDependencyType();
ContextAnnotationAutowireCandidateResolver resolver = new ContextAnnotationAutowireCandidateResolver();
resolver.setBeanFactory(beanFactory);
String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, dependencyType);
for (String beanName : beanNames) {
System.out.println("找到beanName:" + beanName);
BeanDefinition beanDefinition = beanFactory.getMergedBeanDefinition(beanName);
if (resolver.isAutowireCandidate(new BeanDefinitionHolder(beanDefinition, beanName), service)) {
System.out.println("当前beanName:" + beanName + "匹配");
System.out.println(service.resolveCandidate(beanName, dependencyType, beanFactory));
} else {
System.out.println("当前beanName:" + beanName + "不匹配");
}
}
}
目标类
static class Target2 {
@Autowired
private Service service;
@Autowired
private Service service3;
}
注入-@Primary
@SneakyThrows
private static void testPrimary(DefaultListableBeanFactory beanFactory) {
DependencyDescriptor service = new DependencyDescriptor(Target2.class.getDeclaredField("service"), true);
Class<?> dependencyType = service.getDependencyType();
String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, dependencyType);
for (String beanName : beanNames) {
System.out.println("找到beanName:" + beanName);
BeanDefinition beanDefinition = beanFactory.getMergedBeanDefinition(beanName);
if (beanDefinition.isPrimary()) {
System.out.println("当前beanName:" + beanName + "匹配");
System.out.println(service.resolveCandidate(beanName, dependencyType, beanFactory));
} else {
System.out.println("当前beanName:" + beanName + "不匹配");
}
}
}
注入-byName
@SneakyThrows
private static void testName(DefaultListableBeanFactory beanFactory) {
DependencyDescriptor service3 = new DependencyDescriptor(Target2.class.getDeclaredField("service3"), true);
Class<?> dependencyType = service3.getDependencyType();
String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, dependencyType);
for (String beanName : beanNames) {
System.out.println("找到beanName:" + beanName);
if (beanName.equals(service3.getDependencyName())) {
System.out.println("当前beanName:" + beanName + "匹配");
System.out.println(service3.resolveCandidate(beanName, dependencyType, beanFactory));
} else {
System.out.println("当前beanName:" + beanName + "不匹配");
}
}
}