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