2012-05-10 29 views
3

如果我有一个PropertyPlaceholderConfigurer通过XML文件传入,是否可以让我的Spring @Configuration使用它作为它处理的所有bean的属性来源?属性与Spring @Configuration

@Configuration 
@ComponentScan(value = { "x.y.z }) 
@ImportResource({ "classpath:remote-properties/applicationContext.xml",}) 
public class CoreConfiguration implements TransactionManagementConfigurer { 

    @Resource(name = "com.c.h.c.PropertyPlaceholderConfigurer") 
    public PropertyPlaceholderConfigurer pp; 

    @Bean 
    public PropertyPlaceholderConfigurer propertiesFactoryBean() { 
     return pp; 
    } 
} 

通过上述,它从来没有击中我的pp断点。如果我删除@Bean和该方法,我可以验证pp已填充。那么我怎么才能使用配置注册呢?

+0

您是否浏览过http://forum.springsource.org/showthread.php?107194-PropertyPlaceholderConfigurer-and-javconfig –

+0

@Rob我无法直接使用属性文件。我必须从该应用内容中使用PropertyPlaceholderConfigurer bean,或者加载具有第二个属性文件的url的属性文件,然后加载该属性文件。我知道这听起来很荒谬,但这是我解决多个环境问题的团队。 – AHungerArtist

+0

@Rob更重要的是,我不仅需要'@Configuration'类本身的属性,还可以在'@ComponentScan'对'@Service'注释类执行魔术时使用它们(所以'@Value'注释将会得到正确填充)。 – AHungerArtist

回答

3

我觉得很愚蠢。我错过了我的@Value注释中的一个右大括号。我无法想象我曾看过多少次而错过了它。

因此,如果在应用上下文中有PropertyPlaceHolderConfigurer,那么您的@ImportResource将无任何问题。你甚至不需要把它作为@Resource