简体   繁体   中英

How do I inject a value from Properties file to a field of an existing instance (not managed by Spring) using Spring annotations?

Here's what I wrote:

com.MyTest.java

package com;

import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {

    @Value( "${env}" )
    public String env;

    @Value( "#{env}" )
    public String env2;

    @Before
    public void init() {
        ClassPathXmlApplicationContext appCtx = new ClassPathXmlApplicationContext( 
            "myTest.xml"
         );
        appCtx.getBeanFactory().autowireBean( this );
        // appCtx.getAutowireCapableBeanFactory().autowireBean( this ); // doesn't make a difference
    }

    @Test
    public void test() {
        System.out.println( "env is :"+ this.env );
        System.out.println( "env is :"+ this.env2 );
    }

}

myTest.properties

env=QA

myTest.xml

<?xml version="1.0" encoding="UTF-8"?>
<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:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
    ">


    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:myTest.properties</value>
            </list>
        </property> 
        <property name="ignoreUnresolvablePlaceholders" value="false"/>
    </bean>

</beans>

pom.xml (important parts only)

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.1.5.RELEASE</version>
        </dependency>

Output:

env is :null
env is :null

My expected output:

env is :QA
env is :QA

How do I inject a value from Properties file to a field of an existing instance using Spring annotations?

Can you please try annotating your class

@PropertySource("classpath:/com/myProject/config/properties/database.properties") And have a variable like this:

@Autowired private Environment env;

Now you can access to all your properties in this way:

env.getProperty("database.connection.driver")

I simply forgot to add this to my spring configuration file.

<context:annotation-config/>

Wow.

and the following line

@Value( "#{env}" )

should be replaced with

@Value( "#{'${env}'}" )

If you want to use #{} which is a SpEL notation to access properties values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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