Springboot注入ApplicationContext的几种方式
所属分类 springboot
浏览量 987
Autowired 注入
@Component
public class XxxBean {
@Autowired
private ApplicationContext applicationContext;
}
构造方法注入
@Component
public class XxxBean{
private ApplicationContext applicationContext;
public XxxBean(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
}
Java配置类中 构造函数注入
@Configuration
public class XxxConfig {
@Bean
public XxxBean bean2(ApplicationContext ctx){
System.out.println("******TestConfi.springInfoBean2.ctx="+ctx);
Environment environment = ctx.getEnvironment();
System.out.println("******TestConfi.springInfoBean2.environment="+environment);
System.out.println(environment.getProperty("app.name"));
return new XxxBean();
}
@Bean
public XxxBean bean1(Environment environment){
System.out.println("******TestConfi.springInfoBean.genvironment="+environment);
System.out.println(environment.getProperty("app.name"));
return new XxxBean();
}
}
ApplicationContextAware
@Component
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextHolder.applicationContext = applicationContext;
}
}
注意顺序 ,该bean先加载再使用
@DependsOn("springContextUtil")
上一篇
下一篇
SpringCloud VS Dubbo
Jedis的几种使用方式
zookeeper节点属性中的三个zxid
zookeeper四字命令conf结果说明
Zookeeper四字命令
降级 限流 熔断 简单理解