簡體   English   中英

如何使用Spring注釋將Properties文件中的值注入到現有實例(不受Spring管理)的字段中?

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

這是我寫的:

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(僅重要部分)

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

輸出:

env is :null
env is :null

我的預期輸出:

env is :QA
env is :QA

如何使用Spring注釋將“屬性”文件中的值注入到現有實例的字段中?

可以給您的課加上注釋嗎

@PropertySource(“ classpath:/com/myProject/config/properties/database.properties”)並具有如下所示的變量:

@Autowired私有環境env;

現在,您可以通過以下方式訪問所有屬性:

env.getProperty(“ database.connection.driver”)

我只是忘了將此添加到我的spring配置文件中。

<context:annotation-config/>

哇。

和下面的行

@Value( "#{env}" )

應該替換為

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

如果要使用SpEL表示法#{}來訪問屬性值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM