user755806
user755806

Reputation: 6815

autowiring properties directly into bean using spring?

I am using Spring3. I have below property in one of the bean.

private Properties properties;

Here properties is util package property

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

Upvotes: 1

Views: 1397

Answers (2)

Sudarshan_SMD
Sudarshan_SMD

Reputation: 2587

When it comes to configuring software using certain properties such as URL's, passwords and unique keys in spring PropertyPlaceHolderConfigurer is your best bet.

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

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

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

Upvotes: 1

Mannekenpix
Mannekenpix

Reputation: 752

If you want to access some property values, there is an elegant way.

In your context.xml file :

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

In your class :

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

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

Upvotes: 1

Related Questions