外国产品设计网站,淘客推广是什么,沈阳网站制作培训,网站的空间描述
使用GenericApplicationContext类的registerBean方法可以在项目运行时注入一个bean#xff0c;获取GenericApplicationContext可以继承ApplicationContextAware#xff0c;重写setApplicationContext#xff0c;里面的参数就是ApplicationContext。
继承ApplicationC…描述
使用GenericApplicationContext类的registerBean方法可以在项目运行时注入一个bean获取GenericApplicationContext可以继承ApplicationContextAware重写setApplicationContext里面的参数就是ApplicationContext。
继承ApplicationContextAware
RestController
RequestMapping(/register/bean)
public class RegisterBeanController implements ApplicationContextAware {private ApplicationContext applicationContext;// AnnotationConfigServletWebServerApplicationContextGetMapping(/example)public String registerBean() {GenericApplicationContext genericApplicationContext (GenericApplicationContext) this.applicationContext;// ExampleBean没有使用任何的Component注解。genericApplicationContext.registerBean(ExampleBean, ExampleBean.class);ExampleBean exampleBean (ExampleBean) applicationContext.getBean(ExampleBean);return exampleBean.getBeanData().getName();}Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {this.applicationContext applicationContext;}
}
BeanData
Component
public class BeanData {public String getName() {return BeanData;}
}ExampleBean
测试Autowrite是否生效。
public class ExampleBean {private String name;Autowiredprivate BeanData beanData;public String getName() {return name;}public void setName(String name) {this.name name;}public BeanData getBeanData() {return beanData;}
}