动态改变春豆

如何使用java spring在运行时动态更改bean的属性? 我有一个bean mainView,它应该用作属性“class”“class1”或“class2”。 此决定应基于属性文件进行,其中属性“withSmartcard”为“Y”或“N”。

ApplicationContext的:

          

PropertyFile:

withSmartcard = Y

使用PropertyPlaceHolder管理属性文件..

  The service properties file   

并更改您的ref属性如下:

     

在属性文件some.where.MyApp.properties中,添加一个名为withSmartCardClassImplementation的键,它将具有class1或class2(您选择)作为值。

 withSmartCardClassImplementation=class1 

你想要PropertyPlaceholderConfigurer 。 手册的这一部分比我当场表现得更好。

在您的示例中,您需要将属性的值更改为class1class2 (spring上下文中所需bean的名称)。

或者,您的配置可能是:

         

配置文件包含:classToUse = fully.qualified.name.of.some.Class

在用户可编辑的配置文件中使用bean或类名是不可接受的,并且您确实需要使用“Y”和“N”作为配置参数值。 在这种情况下,你只需要在Java中执行此操作,Spring并不意味着完成。

mainView可以直接访问应用程序上下文:

 if (this.withSmartCards) { this.class_ = context.getBean("class1"); } else { this.class_ = context.getBean("class2"); } 

一个更干净的解决方案是将用户配置的处理封装在自己的类中,该类将执行上述操作以减少需要为ApplicationContextAware的类的数量,并根据需要将其注入到其他类中。

使用BeanFactoryPostProcessor ,您可以以编程方式注册类属性的定义。 使用FactoryBean ,您可以动态创建bean。 两者都是Spring的一些先进用法。

旁白:鉴于mainView和class1 / class2之间存在循环依赖关系,我不确定您的示例配置是否合法。

使用class级工厂。 可以根据您的财产返回实例。

我相信你可以编写一个实现BeanFactoryPostProcessor的类。 如果XML配置文件中存在此类的bean(与其他bean一起),Spring将自动调用其postProcessBeanFactory(ConfigurableListableBeanFactory)方法。 传递给此方法的ConfigurableListableBeanFactory对象可用于在Spring开始工作之前更改任何bean定义。

同意上面的@Olivier。

有很多方法可以做到这一点。

  1. 使用PropertyPlaceHolderConfigurer ..
  2. 使用BeanPostProcessor ..
  3. 使用Spring AOP,创建建议并操纵属性。

我会推荐上面的项目编号:1。