入门注解@DependsOn
siye@r480:~/svlution/workspace/springcore4322$ tree src/
src/
├── main
│ ├── java
│ │ ├── log4j.properties
│ │ └── ocn
│ │ └── site
│ │ └── springioc
│ │ ├── domain
│ │ │ ├── PersonInit.java
│ │ │ ├── Person.java
│ │ │ └── User.java
│ │ └── setup
│ │ └── Appconfig.java
│ └── resources
└── test├── java│ └── ocn│ └── site│ └── springioc│ └── domain│ └── Runtest.java└── resources15 directories, 6 files
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.22.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.3.22.RELEASE</version><scope>test</scope>
</dependency>
package ocn.site.springioc.domain;import org.springframework.stereotype.Component;@Component
public class Person {private int id;public int getId() {return id;}public void setId(int id) {this.id = id;}}
package ocn.site.springioc.domain;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;@Component("personInit")
@Lazy
public class PersonInit {private Person person;public Person getPerson() {return person;}// 该注解必须标注在setter方法上,否则不会生效配置。@Autowiredpublic void setPerson(Person person) {person.setId(34);this.person = person;}}
package ocn.site.springioc.domain;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;//等价于 bean元素的 depend-on属性
//
//对于 A依赖于B,C依赖于B,但是A与C之间的依赖关系不是很明显.
//可是A的实例化,需要C的实例化.
//此时可使用强制依赖的注解标记.
//在实例化A的时候,强制实例化C
@Component
@DependsOn("personInit") // 不使用该注解的话,person的初始化配置不会被注入
public class User {private @Autowired Person person;public Person getPerson() {return person;}public void setPerson(Person person) {this.person = person;}}
package ocn.site.springioc.setup;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;@Configuration
@ComponentScan({ "ocn.site.springioc.domain" })
public class Appconfig {}
package ocn.site.springioc.domain;import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;import ocn.site.springioc.setup.Appconfig;@RunWith(SpringRunner.class)
@ContextConfiguration(classes = Appconfig.class)
public class Runtest {private final Logger logger = Logger.getLogger(this.getClass());private @Autowired User user;@Testpublic void run() throws Exception {logger.info(user.getPerson().getId());}}
19-09-09 09:20:24 org.springframework.test.context.support.DefaultTestContextBootstrapper =====>>> Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
19-09-09 09:20:24 org.springframework.test.context.support.DefaultTestContextBootstrapper =====>>> Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@3b81a1bc, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@64616ca2, org.springframework.test.context.support.DirtiesContextTestExecutionListener@13fee20c]
19-09-09 09:20:24 org.springframework.context.support.GenericApplicationContext =====>>> Refreshing org.springframework.context.support.GenericApplicationContext@3a82f6ef: startup date [Mon Sep 09 09:20:24 CST 2019]; root of context hierarchy
19-09-09 09:20:24 ocn.site.springioc.domain.Runtest =====>>> 34
19-09-09 09:20:24 org.springframework.context.support.GenericApplicationContext =====>>> Closing org.springframework.context.support.GenericApplicationContext@3a82f6ef: startup date [Mon Sep 09 09:20:24 CST 2019]; root of context hierarchy
入门注解@DependsOn相关推荐
- Spring 使用注解@DependsOn控制Bean加载顺序
文章目录 1. 前言 2. 代码实现 1. 前言 默认情况下,Spring加载Bean的顺序是不确定的(或者可以理解为,按编译后的class文件顺序加载).当我们需要控制Bean加载顺序以满足特定的需 ...
- Spring高级之注解@DependsOn详解(超详细)
定义/作用 @DependsOn注解可以定义在类和方法上,意思是我这个组件要依赖于另一个组件,也就是说被依赖的组件会比该组件先注册到IOC容器中. 使用场景: 需要用到观察者模式的情况下通常都需要用到 ...
- 了解和入门注解的应用
2019独角兽企业重金招聘Python工程师标准>>> 一.概述 jdk的java.lang包中提供的最基本的annotation 1.@SuppressWarnings(" ...
- Spring : Bean依赖注解(@DependsOn)
1.美图 2.概述 @DependsOn:控制Bean加载顺序.指定先加载@DependsOn对应的Bean. 3.源码 @Target({ElementType.TYPE, ElementType. ...
- Hibernate入门注解笔记
@Entity 代表实体 映射一张表 @Table 定义表的属性 @Embeddable 定义类级别可以被嵌入 @Id 指定主键 @GeneratedValue 指定主键生成策略 @Column指定列 ...
- boost.asio基础篇 小白入门注解
参考资料:https://blog.csdn.net/caoshangpa/article/details/79231740 一个基础的同步客户端 //使用asio的所有程序都需要至少有一个io_co ...
- 40 个 Spring Boot 常用注解
以下文章来源方志朋的博客,回复"666"获面试宝典 作者 | 谭朝红 链接 | ramostear.com 一.Spring Web MVC 与 Spring Bean 注解 Sp ...
- 40 个 SpringBoot 常用注解
以下文章来源方志朋的博客,回复"666"获面试宝典 来源:https://ramostear.com/ 一.Spring Web MVC 与 Spring Bean 注解 Spri ...
- 40 个 SpringBoot 常用的注解,你知道几个?
一.Spring Web MVC 与 Spring Bean 注解 Spring Web MVC 注解 @RequestMapping @RequestMapping注解的主要用途是将Web请求与请求 ...
- 40 个 SpringBoot 常用注解:让生产力爆表!
作者 | 谭朝红 来源 | www.ramostear.com 一.Spring Web MVC 与 Spring Bean 注解 Spring Web MVC 注解 @RequestMapping ...
最新文章
- [ZJOI2011]细胞——斐波那契数列+矩阵加速+dp
- windows核心编程-第一章 对程序错误的处理
- [转]微信的一道前端面试题
- rank--求矩阵的秩
- “Xavier”安卓木马分析:可静默收集数据并远程代码执行
- OpenCV中高斯混合背景建模算法汇总
- java term_[ElasticSearch]Java API 之 词条查询(Term Level Query)
- 华为k662c的虚拟服务器,华为k662c路由器怎么设置
- Android高版本开机广播,android3.1以上,假如程序没有启动过,怎么获取开机广播呢?...
- 常见数据结构List之LinkedList
- Storm Trident示例shuffleparallelismHint
- CCF NOI1075 F函数
- 网页另存显示不全_word另存为选项没有PDF格式怎么办?别忘了还有这招!
- 帝豪gs车机系统wince_平顶山到河南,帝豪GS俱乐总部,帝豪GS两年用车感受
- 表格超出_?那些年Word表格你肯定踩过的坑
- [论文笔记]JED:Towards Real-Time Multi-Object Tracking
- 有什么办法可以让微信群二维码永久有效?这类的二维码生成器怎么制作?
- 软件是怎么开发出来的?怎么进行软件开发流程
- delphi 安装控件时提示系统找不到指定的模块的解决
- Mac Book文件夹加密
热门文章
- 在计算机中 总线简称,微机中的总线一般分为几等
- 苹果a7处理器_苹果历代cpu性能对比
- android checkboxpreference属性,如何更改android中CheckBoxPreference标题的文本颜色?
- 设置华表Cell插件外观时的“闪烁”问题
- 计算机硬件维修的步骤和方法,计算机硬件组装与维护教程
- 非线性动力学_非线性随机动力学团队2019大事件回顾
- BP 神经网络算法原理
- linux dvd 刻录_如何将任何视频文件刻录到可播放的DVD
- 关于安卓手机无法将外置声卡的效果录入到手机自拍视频上的问题。
- Android实现选择题答题(包括单选、多选和答题卡)