入门注解@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
    └── resources

15 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方法上,否则不会生效配置。
	@Autowired
	public 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;

	@Test
	public 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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值