ApplicationInitlizer接口与Spring工具类
有了上述思想,我们就能想到在spring框架中,上下文是我们重点关切得对象。因此我们想着通过static来引用spring上下文,然后形成一个工具类。我们在项目得业务部分能够通过该工具类来操作我们得spring上下文。这块我们说一下spring扩展接口ApplicationContextInitializer,这个接口会在spring上下文刷新之前进行调用,相当于提前将容器进行对外得暴露,当然页提供了我们修改其属性得权力。ApplicationContextInitializer得执行在prepareContext中,这块之前也大概得说过了。
protected void applyInitializers(ConfigurableApplicationContext context) {
Iterator var2 = this.getInitializers().iterator();
while(var2.hasNext()) {
ApplicationContextInitializer initializer = (ApplicationContextInitializer)var2.next();
Class requiredType = GenericTypeResolver.resolveTypeArgument(initializer.getClass(), ApplicationContextInitializer.class);
Assert.isInstanceOf(requiredType, context, "Unable to call initializer.");
initializer.initialize(context);
}
}
public class MyBean {
private String name;
private String config;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getConfig() {
return config;
}
public void setConfig(String config) {
this.config = config;
}
public String toString() {
return "MyBean{" +
"name='" + name + '\'' +
", config='" + config + '\'' +
'}';
}
}
public class MySpringContentUtils {
public static ConfigurableApplicationContext context;
public static ConfigurableApplicationContext getContext() {
return context;
}
public static void setContext(ConfigurableApplicationContext context) {
MySpringContentUtils.context = context;
}
public static void registerBean(String name, Class factoryClazz, Class beanClazz, MutablePropertyValues mpv) throws BeanDefinitionStoreException {
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(beanClazz);
GenericBeanDefinition beanDefinition = (GenericBeanDefinition)beanDefinitionBuilder.getRawBeanDefinition();
beanDefinition.setBeanClass(factoryClazz == null ? beanClazz : factoryClazz);
beanDefinition.setPropertyValues(mpv);
BeanDefinitionRegistry beanFactory = (BeanDefinitionRegistry)context.getBeanFactory();
beanFactory.registerBeanDefinition(name, beanDefinition);
}
public staticT getBean(String name) throws BeansException {
return (T) context.getBean(name);
}
}
public class MyTianApplicationInitlize implements ApplicationContextInitializer {
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
System.out.println("我来了...宝");
MySpringContentUtils.setContext(configurableApplicationContext);
}
}
public static void main(String[] args) throws InterruptedException {
SpringApplication builder=new SpringApplicationBuilder(Demo1Application.class).initializers(new MyTianApplicationInitlize()).build();
builder.run( args);
System.out.println(env.toString());
MutablePropertyValues mpv = new MutablePropertyValues();
mpv.addPropertyValue("name","tianjingle_initlizer");
MySpringContentUtils.registerBean("lisi",null, MyBean.class,mpv);
MyBean myBean= (MyBean) MySpringContentUtils.getContext().getBean("lisi");
System.out.println(myBean.toString());
}
public static void main(String[] args) throws InterruptedException {
// SpringApplication builder=new SpringApplicationBuilder(Demo1Application.class).initializers(new MyTianApplicationInitlize()).build();
// builder.run( args);
SpringApplication.run(Demo1Application.class,args);
System.out.println(env.toString());
MutablePropertyValues mpv = new MutablePropertyValues();
mpv.addPropertyValue("name","tianjingle_initlizer");
MySpringContentUtils.registerBean("lisi",null, MyBean.class,mpv);
MyBean myBean= (MyBean) MySpringContentUtils.getContext().getBean("lisi");
System.out.println(myBean.toString());
}
首页图片来自:周冬雨-Sogou百科
https://baike.sogou.com/v39745781.htm
评论