spring中service类代理的加载过程

编程

@RestController

public class HelloController {

@Autowired

UserService userService;

}

@Service

public class UserServiceImpl implements UserService {

}

关键步骤

  • spring容器初始化HelloController
  • 属性填充userService
  • AutowiredAnnotationBeanPostProcessor完成userService填充
  • spring容器初始化userService
  • AnnotationAwareAspectJAutoProxyCreator将userService封装成代理类

流程

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean创建bean

createBeanInstance创建HelloController实例

populateBean属性设置(设置UserService)

AutowiredAnnotationBeanPostProcessor#postProcessProperties设置Autowired属性

findAutowiringMetadata查找所有需要注入的属性

metadata.inject属性注入

element.inject(target, beanName, pvs)

org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.AutowiredFieldElement#inject

beanFactory.resolveDependency从beanFactory中获取需要注入的对象

field.set(bean, value);利用反射进行属性设置

initializeBean初始化完成后的一些后置操作,userService创建时会在此处创建代理

@Autowired注解

Note that actual injection is performed through a BeanPostProcessor which in turn means that you cannot use @Autowired to inject references into BeanPostProcessor or BeanFactoryPostProcessor types. Please consult the javadoc for the AutowiredAnnotationBeanPostProcessor class (which, by default, checks for the presence of this annotation).

请注意,实际的注入是通过BeanPostProcessor执行的,这意味着您不能使用@Autowired将引用注入到BeanPostProcessor或BeanFactoryPostProcessor类型中。请咨询javadoc以获得AutowiredAnnotationBeanPostProcessor类(在默认情况下,该类检查是否存在此注释)。

@Autowired通过AutowiredAnnotationBeanPostProcessor来完成注入

AutowiredAnnotationBeanPostProcessor类

主要靠这个类完成属性注入

参考

  • https://blog.csdn.net/qq_26344609/article/details/78905985

以上是 spring中service类代理的加载过程 的全部内容, 来源链接: utcz.com/z/514980.html

回到顶部