Laodao
Laodao

Reputation: 1709

spring @Value doesn't load value from application.properties file

I tried to use spring @Value to get property from application.properties file. However, if I do

@Service
public class FooClass {

    @Value("${abc}")
    private String var;

    public String foo() {
        System.out.println(var);
    }
}

@Configuration
public class ConfigurationReader {
    @Bean
    public FooClass fooClass()
    {
        return new FooClass();
    }
}

I got "${abc}" assgined to var instead of the value in the property file. Any idea? guys. Thanks in advance.

Upvotes: 0

Views: 744

Answers (1)

StanislavL
StanislavL

Reputation: 57421

You suppress the autowiring by the call

@Bean
public FooClass fooClass()
{
    return new FooClass();
}

Here you in fact manually create a new instance rather than using spring created bean. Just remove the

@Bean
public FooClass fooClass()...

Check that FooClass's package is added to package scan to let spring create bean from your @Service annotation of FooClass

Upvotes: 1

Related Questions