`
JerryWang_SAP
  • 浏览: 970111 次
  • 性别: Icon_minigender_1
  • 来自: 成都
文章分类
社区版块
存档分类
最新评论

Spring框架里注解@Autowired的工作原理

阅读更多

Suppose I have a bean named HelloWorld which has a member attribute points to another bean User.

 

 

With annotation @Autowired, as long as getBean is called in the runtime, the returned HelloWorld instance will automatically have user attribute injected with User instance.

 

 

How is this behavior implemented by Spring framework?

(1) in Spring container implementation’s refresh method, all singleton beans will be initialized by default.

 

 

When the HelloWorld bean is initialized:

 

 

Since it has the following source code:

@Autowired
private User user;

In the runtime, this annotation is available in metadata via reflection. In metadata structure below, the targetClass points to HelloWorld bean, and injectedElements points to the User class to be injected.

 

 

(2) In doResolveDependency, the definition for User bean is searched based on this.beanDefinitionNames ( list in DefaultListableBeanFactory ):

 

 

Once found, the found result is added to array candidateNames:

 

 

Then the constructor of User bean class is called ( still triggered by getBean call ), the user instance is created by calling constructor:

The created user instance together with its name “user” is inserted to the map matchingBeans.

  1. Finally the user reference is set to user attribute of HelloWorld instance via reflection. Here the variable bean in line 569 points to HelloWorld instance, and value points to user instance.

Once field.set(bean, value) is done, we can observe in debugger that the user attribute in HelloWorld instance is already injected successfully.

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

0
0
分享到:
评论

相关推荐

    Spring框架中 @Autowired 和 @Resource 注解的区别

    Spring框架中 @Autowired 和 @Resource 注解的区别 在 spring 框架中,除了使用其特有的注解外,使用基于 JSR-250 的注解,它包括 @PostConstruct, @PreDestroy 和 @Resource 注释。  首先,咱们简单了解 @...

    详解 Spring注解的(List&Map)特殊注入功能

    项目框架是基于spring boot进行开发。其中有两处Spring的注解花费了大量的时间才弄明白到底是怎么用的,这也涉及到spring注解的一个特殊的注入功能。 首先,看到代码中有直接注入一个List和一个Map的。示例代码如下...

    Spring核心注解深入解析:提升开发效率

    Spring框架是Java开发中广泛使用的一个强大工具,它通过依赖注入和控制反转等核心概念,极大地提高了开发效率和应用的可维护性。在这份文档中,我们深入探讨了Spring的核心注解,包括但不限于@Component、@...

    Spring自动注解方式实例

    我idea软件用Maven项目搭建了一个Spring框架,对@Component和@Autowired注解方式写了一个例子,并用单元测试进行了测试,里面包含源码,以及Maven的配置文件pom.xml

    SpringBoot常用注解详解含使用示例(值得珍藏)

    本文将详细介绍Spring Boot中最常用的注解,包括@SpringBootApplication、@Component、@Service、@Repository、@Controller、@RequestMapping、@GetMapping、@PostMapping、@PutMapping、@DeleteMapping、@Autowired...

    基于注解的DI.docx

    常用注解: @Component、创建对象 ...@Autowired、spring框架中引用类型赋值的注解,支持byName、byType,默认byType @Resource、jdk中的注解,使用自动注入给引用数据类型赋值,支持byName、byType,默认byName

    Spring面试专题.pdf

    1、什么是 Spring 框架?Spring 框架有哪些主要模块? 2、使用 Spring 框架能带来哪些好处? 3、什么是控制反转(IOC)?什么是依赖注入? 4、请解释下 Spring 框架中的 IoC? 5、BeanFactory 和 ApplicationContext ...

    Spring面试题.zip

    1、什么是 Spring 框架?Spring 框架有哪些主要模块? 2、使用 Spring 框架能带来哪些好处? 3、什么是控制反转(IOC)?什么是依赖注入? 4、请解释下 Spring 框架中的 IoC? 5、BeanFactory 和 ApplicationContext ...

    springboot学习思维笔记.xmind

    @Enable*注解的工作原理 直接导入配置类 依据条件选择配置类 动态注册Bean 测试 Spring TestContext Framework集成测试 SpringMVC基础 Spring MVC概述 SpringMVC项目快速搭建 构建Maven项目...

    SSM框架面试题.pdf

    8. @Autowired是如何工作的? 9. 解释Spring MVC流程。 10. Spring框架中的单例Bean是线程安全的吗? 11. 什么是Spring Boot?与Spring有什么区别? 12. 解释Spring中的Java配置方式(@Configuration和@Bean注解)。...

    spring framework mvc 框架

    使用 Java 反射、注解等技术实现的简易 Spring MVC 框架。 实现的注解有:<code>@Autowired、<code>@Controller、<code>@RequestMapping 、<code>@Service </code> 核心的代码见 <code>DispatcherServlet</code> ...

    Spring面试题含答案.pdf

    40. @Autowired 注解 41. @Qualifier 注解 42.在 Spring 框架中如何更有效地使用 JDBC? 43. JdbcTemplate 44. Spring 对 DAO 的支持 45. 使用 Spring 通过什么方式访问 Hibernate? 46. Spring 支持的 ORM 47....

    25个经典的Spring面试问答

    什么是Spring框架Spring框架有哪些主要模块 使用Spring框架能带来哪些好处 什么是控制反转IOC什么是依赖注入 请解释下Spring框架中的IoC BeanFactory和ApplicationContext有什么区别 Spring有几种配置方式 如何用...

    Spring攻略(第二版 中文高清版).part1

    1.1.3 工作原理 3 1.2 配置Spring IoC容器中的Bean 4 1.2.1 问题 4 1.2.2 解决方案 4 1.2.3 工作原理 4 1.3 调用构造程序创建Bean 14 1.3.1 问题 14 1.3.2 解决方案 14 1.3.3 工作原理 14 1.4 ...

    Spring攻略(第二版 中文高清版).part2

    1.1.3 工作原理 3 1.2 配置Spring IoC容器中的Bean 4 1.2.1 问题 4 1.2.2 解决方案 4 1.2.3 工作原理 4 1.3 调用构造程序创建Bean 14 1.3.1 问题 14 1.3.2 解决方案 14 1.3.3 工作原理 14 1.4 ...

    lagou-transfer:Spring框架大作业

    大作业:自定义@Service、@Autowired、@Transactional注解类,完成基于注解的IOC容器(Bean对象创建及依赖注入维护)和声明式事务控制,写到转账工程中,并且可以实现转账成功和转账异常时事务回滚 编程题: 自定义...

    springlearning:Spring学习

    Spring 框架学习,完全弃用 XML 配置。 ch1 ch1-1 使用 Spring 注解声明实体类,相关注解为 @controller 、@servies、@respository、@Component。 实体类中使用注解进行依赖注入,例如 @Autowired 自动装载。 配置类...

    Spring-Framework-5:Spring框架5,Hibernate,Jsp,Spring Mvc,

    Spring_Framework_5_denemeleri 1-弹簧框架依赖项注入示例 这个项目是关于Spring依赖注入的。 我们正在创建一个Maven项目。 我们在pom.xml文件中包含spring-context.jar。 然后我们创建并设计beans.xml文件。 我们...

    spring+springmvc+mybatis的整合

    但是有一些部分自己没有能完成,主要是如何从spring容器里取出ApplicationContext,这个我的实现比较low,看了看讲义,才OK的。 我的实现: [java] view plain copy WebApplicationContext acc = ...

Global site tag (gtag.js) - Google Analytics