简体   繁体   English

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

[英]autowiring properties directly into bean using spring?

I am using Spring3 . 我正在使用Spring3 I have below property in one of the bean. 我在其中一个bean中具有以下property

private Properties properties;

Here properties is util package property 这里的属性是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>

Now how can I inject properties directly from spring configuration? 现在如何直接从弹簧配置中注入属性?

Thanks! 谢谢!

If you want to access some property values, there is an elegant way. 如果要访问某些属性值,则有一种优雅的方法。

In your context.xml file : 在您的context.xml文件中:

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

In your class : 在您的课程中:

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

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

When it comes to configuring software using certain properties such as URL's, passwords and unique keys in spring PropertyPlaceHolderConfigurer is your best bet. 在春季使用某些属性(例如URL,密码和唯一键)配置软件时, PropertyPlaceHolderConfigurer是您的最佳选择。

From spring document: 从春季文件:

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.

What you will do is, put all your configuration data in a properties file: 您要做的是,将所有配置数据放入属性文件中:

##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##

Then, configure spring to access external properties file(assuming your properties file is named settings.properties): 然后,配置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>  

After you have configured PropertyPlaceholderConfigurer, access your properties by simple using @value annotation, wherever you want. 配置PropertyPlaceholderConfigurer后,可以在任意位置使用@value注释简单地访问属性。

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

You can use properties file to configure you data source and many other stuff: 您可以使用属性文件来配置数据源和许多其他内容:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM