@PostConstruct、@Autowired以及构造函数的执⾏顺序结论先⾏:构造函数 -> PostConstruct -> @Autowired 依次执⾏
由于项⽬需要启动时加载⼀个配置信息,所以想到了⽤@PostConstruct,如下所⽰:
@Configuration
public class BUrCenterConfig {
@Value("${b.ur.url}")
public String urCenterUrl;
@Value("${b.ur.appId}")
public String urCenterAppId;
@PostConstruct
public void tDefaultConfig() {
UrCenterConfig.SetInterfaceUrl(urCenterUrl);
UrCenterConfig.SetDefaultAppId(urCenterAppId);
挥汗如雨}
}
BeanTest.java
@Service
public class BeanTest {
@Autowired
private BeanTest2 beanTest2;
public BeanTest() {
System.out.println("this is BeanTest construct. ");
1677年}
@PostConstruct
private void init() {
System.out.println("this is BeanTest init function. ");脸上长斑怎么祛斑
}
女大学生的沙龙室2
}
BeanTest2.java
@Service
public class BeanTest2 {
@PostConstruct
private void init() {
System.out.println("this is BeanTest2 init function. ");
}
public BeanTest2() {
椭圆焦距
System.out.println("this is BeanTest2 construct. ");
}
void test2() {
天寒地冻造句
System.out.println("this is BeanTest2 test2 function. ");
}
}
启动项⽬,输出结果如下:
this is BeanTest construct.
什么是目标奥林匹斯神话
this is BeanTest2 construct.
this is BeanTest2 init function.
this is BeanTest init function.
this is BeanTest2 test2 function.