2012-04-05 270 views
4

编辑使用PropertyOverrideConfigurer与注解的类:这是为什么公认的答案对我的作品一些解释,可能这可能是其他人的麻烦。在春季3

在我的富-APP-servlet.xml中,我有这样一行:

<context:component-scan base-package="com.foo.app" /> 

当我之前使用spring2,我所有的服务组件从applicationContext.xml中来了,但现在他们直接被带到在foo-app-servlet.xml中。在我的项目上,servlet有自己的覆盖集合,所以我需要覆盖servlet覆盖文件而不是applicationContext覆盖文件。

覆盖时,如果你还没有命名您的组件,然后它确实用它的小写版本,所以覆盖OrderService.foo,你这样做:

orderService.foo=bar 

末编辑

我正在从一个项目,从春季2.5升级到春季3,因此有xml和基于注释的配置。我们以前使用PropertyOverrideConfigurer在不同环境中更改属性以取得巨大成功。我现在正在研究使用authorize.net的代码,我需要确保我不会从开发环境向他们发送任何内容。

为了做到这一点,我想重写与PropertyOverrideConfigurer我的“测试模式”属性。这对于通过xml配置的bean非常适用,但我无法弄清楚如何使用注释配置的类来完成它。

这是我的applicationContext.xml覆盖片段:

<bean class="org.springframework.beans.factory.config.PropertyOverrideConfigurer"> 
    <property name="location" value="file:${user.home}/override.properties" /> 
    <property name="localOverride" value="true" /> 
    <property name="ignoreResourceNotFound" value="true" /> 
</bean> 

这里是与物业我想覆盖类:

@Component 
public class OrderService { 
    private static Log logger = LogFactory.getLog(OrderService.class); 

    @Autowired @Qualifier("OrderDAO") private OrderDAO orderDao; 

    @Autowired private SiteManager siteManager; 
    String authorizenetProperties = "classpath:authorizenet.properties"; 

    private Boolean testMode = false; 

    public Boolean getTestMode() { 
     return testMode; 
    } 

    public void setTestMode(Boolean testMode) { 
     this.testMode = testMode; 
    } 

} 

我已经试过了没几件事” t工作:

com.foo.services.OrderService.testMode=true 
OrderService.testMode=true 

是否有可能做我想在这里做的事情?春天3有一个新的首选方式吗?

回答

5

PropertyOverrideConfigurer使用在你的属性密钥文件作为bean name.bean财产。当@Component自动扫描您的配置时,Spring会将该bean命名为以小写字母开头的非限定类名。在你的情况下,OrderService bean应该被命名为orderService。所以,以下应该工作。

orderService.testMode=true 

您也可以通过传递一个名称组件注释像@Component("OrderService")@Component("com.foo.services.OrderService")命名豆。这些在Spring 3.x中都不是新的。

希望这有助于

+0

我相信,我尝试这样做,得到了一个错误,因为它试图组件扫描发生之前覆盖属性。我很快就会再来一次,让你知道。我认为我已经尝试过这个的原因是,它实际上是我尝试的第一件事。但是我没有像上面那样命名组件。 – samspot 2012-04-24 20:05:43

+0

不要这样做。我指定一个名字: @Component( “orderService”) 公共类OrderService {... 并确定这些在我的属性文件: orderService.authorizenetProperties = “富”; orderService.var =“bar”; 没有错误,但是在访问该类时未设置属性。 – samspot 2012-04-26 16:58:52

+0

好的......我想出了这里发生了什么。 Web应用程序servlet xml具有。正因为如此,它本身并不通过applicationContext.xml来拉取我的依赖关系。所以我需要使用servlet的覆盖文件,而不是applicationContext。谢谢你的明确答案,如果没有它,我不会发现它! – samspot 2012-04-26 17:07:41

0

发布此非最佳解决方案:

可以解决这个与另一个bean:

的applicationContext.xml:

<bean id="SiteProperties" class="com.foo.utilities.SiteProperties"> 
     <property name="serviceUrl" value="http://localhost:8080" /> 
     <property name="authorizationTestMode" value="false" /> 
    </bean> 

OrderService.java:

@Component 
public class OrderService { 
    private static Log logger = LogFactory.getLog(OrderService.class); 

    @Autowired @Qualifier("OrderDAO") private OrderDAO orderDao; 

    @Autowired private SiteManager siteManager; 
    @Autowired private SiteProperties siteProperties; 
    String authorizenetProperties = "classpath:authorizenet.properties"; 
} 

所以,只需创建一个xml配置的bean并注入注解配置的bean。

我还是想知道“正确”的方式做到这一点,如果有人知道。

谢谢!