0

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.

1
  • Is the class a Spring-managed bean? It's difficult to see, since you didn't show us. Commented Apr 20, 2017 at 21:39

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.