使用spring直接将属性自动装配到bean中?

时间:2014-01-31 12:23:09

标签: java spring spring-mvc

我正在使用Spring3。我在其中一个bean中有property以下。

private Properties properties;

此处属性为util包属性

    <bean id="properties" class="java.util.Properties">
            <constructor-arg>
                <props>
                    <prop key="id">${id}</prop>
                    <prop key="protocol">${protocol}</prop>
                    <prop key="username">${username}</prop>
                    <prop key="password">${password}</prop>
                </props>
            </constructor-arg>
        </bean>

    <bean id="propertyFactory"
        class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="ignoreResourceNotFound" value="false" />
        <property name="locations">
            <list>
                <value>classpath:conf/test.properties</value>
            </list>
        </property>
    </bean>
    <bean id="propertyConfigurer"
           class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="false"/>
        <property name="properties" ref="propertyFactory"/>
    </bean>

现在我如何直接从弹簧配置中注入属性?

谢谢!

2 个答案:

答案 0 :(得分:1)

如果您想访问某些属性值,可以采用一种优雅的方式。

在context.xml文件中:

<context:property-placeholder location="classpath:myfile.properties" />

在你班上:

@Value("${key.property}")
private int myNumberProperty;

@Value("${another.key.property}")
private String myStringProperty;

答案 1 :(得分:1)

在使用某些属性(如URL)配置软件时,春季PropertyPlaceHolderConfigurer中的密码和唯一键是您最好的选择。

从春季文件:

You use the PropertyPlaceholderConfigurer to externalize property values from a bean definition in a separate file using the standard Java Properties format. Doing so enables the person deploying an application to customize environment-specific properties such as database URLs and passwords, without the complexity or risk of modifying the main XML definition file or files for the container.

您要做的是,将所有配置数据放在属性文件中:

##JDBC related properties start here##
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.dialect=org.hibernate.dialect.MySQLDialect
jdbc.databaseURL=jdbc:mysql://localhost:3306/databaseName
jdbc.userName=root
jdbc.password=root  
##JDBC related properties end here##
## path Configuration start here##
path.name=mypath
path.type=httpfile
path.url=http://acdsds:8380/gis/
## path Configuration ends here##

然后,配置spring以访问外部属性文件(假设您的属性文件名为settings.properties):

<!--settings for accessing external property files--> 
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>                  
                <value>/yourPathToPropertiesFile/settings.properties</value>                   
            </list>
        </property>
    </bean>  

配置PropertyPlaceholderConfigurer后,只需使用@value注释,即可随时随地访问您的属性。

@Value("${path.name}")
String pathName;

您可以使用属性文件来配置数据源和许多其他内容:

<bean id="dataSource" destroy-method="close"
        class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.databaseURL}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>