bean获取Spring容器
Person.java
1 public class Person implements ApplicationContextAware{ 2 3 private String name; 4 private int age; 5 private doHomeWork dohomework; 6 private ApplicationContext ctx; 7 /* 8 * Spring容器会检测容器中所有的Bean,如果发现某个Bean实现了ApplicationContextAware接口, 9 * Spring容器会在创建该Bean之后,自动调用该方法,调用该方法时,会将容器本身作为参数传给该方法。 10 * */ 11 @Override 12 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 13 this.ctx = applicationContext; 14 } 15 public ApplicationContext getApplicationContext() { 16 return this.ctx; 17 } 18 }
运行结果