簡體   English   中英

從外部類調用時解析@Value失敗-Spring Java

[英]resolving @Value fails when invoked from external class - spring java

在屬性文件(config.properties)中,我定義了幾個屬性:

my.property.first=xyz
my.property.second=12

我創建了一個類來讀取這些屬性:

package my.package.first;
............

@Configuration
public Class MyProperties {

    @Value("${my.property.first}") private String propertyFirst;

    @Value ("${my.property.second}") private String propertySecond;

    public String getPropertyFirst() {
        return propertyFirst;
    }

    public int getPropertySecond() {
        return propertySecond
    }

    @Bean 
    public MyProperties getInstance() {
        return this;
    } 
}

現在,我想在放置在其他包中的類中使用這些屬性:

package my.otherpackage.third;

import my.property.package.first.MyProperties;
.............................
public class GetMyProperties{

    AnnotationConfigApplicationContext context  = new AnnotationConfigApplicationContext(MyProperties.class);

    MyProperties myProperties =context.getBean("getInstance",MyProperties.class);

    //This returns ${my.property.first}
    String propertyFirst = myProperties.getPropertyFirst();

    // This returns ${my.property.second}
    int propertySecond = context.getBeanFactory().resolveEmbeddedValue(("${my.property.first}"));
}

我嘗試通過僅使用注釋來解決此問題。

我使用Spring 4。

謝謝

MyProperties實際上不是@Configuration類,它是一個bean。 給它注解@Component並將@Bean聲明移到另一個@Configuration類。 春季為bean定義的位置無關緊要(只要它在配置類中),如果您的應用程序中有@ComponentScan或@SpringBootApplication,它將選擇它。你做。 您制作的MyProperties類@Component

您需要:

@Component
public class MyProperties {
    @Value("${my.property.first}") private String propertyFirst;

    @Value ("${my.property.second}") private String propertySecond;

    public String getPropertyFirst() {
        return propertyFirst;
    }

    public void setPropertyFirst(String propertyFirst){
        this.propertyFirst = propertyFirst;
    }

    public int getPropertySecond() {
        return propertySecond
    }

    public void setPropertySecond(String propertySecond){
        this.propertySecond= propertySecond;
    }
}



@Configuration
public class Config {
    @Bean 
    public MyProperties getInstance() {
        return new MyProperties();
    } 
}



package my.otherpackage.third;

import my.property.package.first.MyProperties;
.....
@EnableAutoConfiguration
@ComponentScan(basePackages = {"my"})
public class GetMyProperties{
    public static void main(String[] args){
        AnnotationConfigApplicationContext context  = new AnnotationConfigApplicationContext(GetMyProperties.class);

        MyProperties myProperties =context.getBean("getInstance",MyProperties.class);

        //This returns the value of ${my.property.first}
        String propertyFirst = myProperties.getPropertyFirst();

        // This returns the value of ${my.property.second}
        int propertySecond = myProperties.getPropertySecond();
    }
}

暫無
暫無

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

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