简体   繁体   中英

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

In the property file (config.properties) I defined several properties:

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

I created a class to read these properties:

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

Now I want to use these properties in a class placed in some other package:

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}"));
}

I try to solve this by using only Annotations.

I use Spring 4.

Thanks

MyProperties isn't really a @Configuration class, it is a bean. Give it the annotation @Component and move the @Bean declaration to another @Configuration class. It doesn't matter where the bean is defined for spring (as long as it is in a configuration class), it'll pick it up if you have the @ComponentScan or @SpringBootApplication somewhere in your app, which I'm pretty sure you do. your MyProperties class you make @Component

You'll want:

@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();
    }
}

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