2011-08-28 140 views
29

如果我有2个.properties文件安装在我的Spring XML像这样:春天的Util:通过注释属性注入一个bean

<util:properties id="serverProperties" location="file:./applications/MyApplication/server.properties"/> 
<util:properties id="someConfig" location="file:./applications/MyApplication/config.properties"/> 

如何通过注解注入这些属性文件与java.util.Properties豆?

如何通过Spring注释获取特定的属性?

干杯!

回答

44
@Autowired 
@Qualifier("serverProperties") 
private Properties serverProperties; 
@Autowired 
@Qualifier("someConfig") 
private Properties otherProperties; 

@Resource(name = "serverProperties") 
private Properties serverProperties; 
@Resource(name = "someConfig") 
private Properties otherProperties; 

通常,@Autowired用于通过型自动装配在Spring和@Resource用于按姓名。 @ Autowired + @ Qualifier可以加倍使用名义自动装配,但它的确适用于具有fine-tune the type能力的自动装配。

+0

感谢您的信息和代码示例! Works – NightWolf

2

您可以使用@PropertySource

@Configuration 
@PropertySource(name = "someName", value = {"classpath:a.properties", "classpath:b.properties"}) 
public class MyConfiguration { 
} 
+0

这很好,它完全消除了对XML配置的需求。 – bluecollarcoder

15

由于这个问题有很多次。我认为使用SpEL(Spring表达式语言)指出另一个选项是值得的 - 如果您需要特定的属性,可以使用特定的bean属性上的@Value注释来注入它们;

class SomeClass { 
    @Value("#{serverProperties['com.svr.prop']}") 
    private String aServerCfgProperty; 

    @Value("#{someConfig['another.config.setting']}") 
    private String someOtherProperty; 
} 

你不需要使用索引语法['index.val']你可以直接获取它;

@Value("#{someConfig}") 
private Properties someConfig 

@Value("#{serverProperties}") 
private Properties svrProps; 

我发现这很实用,并且使用通过@Resource/@ Autowired直接注入的属性对象。

使用带索引Properties对象的@Value的另一个好理由是,如果项目中的.properties文件很好,某些IDE(例如IntelliJ)可以重构实际的属性名称。另一个技巧是使用类似EProperties(它扩展了本地Java属性对象),如果你想在属性文件中进行包含/嵌套/替换,而不使用Spring的PropertiesPlaceholderConfigurer类(可悲的是不公开它的属性 - 使用SpEL索引['key'] bean需要成为Map<>的一个实例,即Java属性对象所做的扩展映射)...

最后,SpEL的另一个很好的特性是可以直接访问bean的属性。因此,举例来说,如果上述示例中的SomeClass是Spring bean,例如someClass然后在AnotherBeanClass中我们可以有;

@Value("#{someClass.someOtherProperty}") 
private String injectedBeanProp 

你也可以调用一个getter方法:

@Value("#{someClass.getSomeOtherProperty()}") 
private String injectedBeanProp 

看到这里的规划环境地政司指导; http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#expressions

+1

只是一个说明SpEL在春季3+ – NightWolf

+0

更多在这里; http://blog.nemccarthy.me/?p=304 – NightWolf

+0

我只是获得@Value注释的空值! –

0

大部分时间我将所有属性封装到一个实用程序中,并在我的应用程序中使用。这样你就不用担心/管理应用层中的每个属性文件。 Autowired setProps(...)会读取您加载的所有util:属性道具列表。

import java.util.List; 
import java.util.Properties; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Component; 

@Component 
public class AppProperiesProcessor { 

private List<Properties> props; 
private Properties mergedProperties; 

@Autowired 
public final void setProps(List<Properties> props) { 
    this.props = props; 
} 

public String getProp(final String keyVal) { 

    if (null == this.mergedProperties) { 
     this.mergedProperties = new Properties(); 

     for (Properties prop : this.props) { 
      this.mergedProperties.putAll(prop); 
     } 
    } 
    return mergedProperties.getProperty(keyVal); 
    } 
} 
1

XML文件

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:util="http://www.springframework.org/schema/util" 
xsi:schemaLocation=" 
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.0.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
http://www.springframework.org/schema/util 
http://www.springframework.org/schema/util/spring-util-4.0.xsd"> 
    <context:component-scan base-package="com.sha.home" /> 
    <mvc:annotation-driven/> 
    <util:properties id="dbProp" location="classpath:db.properties" /> 
    <!-- <context:property-placeholder location="classpath:db.properties"/> --> 

</beans> 
在Java文件 @Value( “#{} DBPROP”) 私人性质dbProperties

; System.out.println(“urllll”+ dbProperties.getProperty(“jdbc.url”));