Skip to content

YAML property injection should work when including the base spring-boot module. #821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
berlin-ab opened this issue May 8, 2014 · 4 comments

Comments

@berlin-ab
Copy link

When I include these dependencies:

dependencies {
    compile("org.springframework.boot:spring-boot:1+")
    compile("org.yaml:snakeyaml:1+")
}

And I have this config file:

example:
    someValue: foobar

Then I should be able to do this:

class Foo {
    @Value("${example.someValue}")
    String someValue;
}

@Autowired
public void example(Foo foo) {
    foo.someValue # should equal 'foobar'
}
@dsyer
Copy link
Member

dsyer commented May 8, 2014

You haven't shown all the code you would need to get a Foo created, so I can't tell if you're doing something wrong or not. The Foo has to actually be a Spring component. E.g. (in separate files and then glued together by a @ComponentScan):

@Component
public class Foo {
    @Value("${example.someValue}")
    String someValue;
}

@Component
public class Thing {
    @Autowired
    public void example(Foo foo) {
        foo.someValue # should equal 'foobar'
    }
}

@dsyer
Copy link
Member

dsyer commented May 8, 2014

Adam's sample app is here: https://github.com/berlin-ab/springbootblog/tree/blog/configuration. It works for me.

@dsyer dsyer added the question label May 8, 2014
@berlin-ab
Copy link
Author

I needed to create a bean:

    @Bean
    public PropertySourcesPlaceholderConfigurer configurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

It works now, but:

I do not think this should be necessary, but so far Spring Boot has not created Beans. I think the ability to configure using @Value should be out-of-the-box when including the base spring-boot package.

When using @value YAML values, I don't feel that I've left the core of Spring Boot. I've not yet asked for a web server, or a database, or a twitter client. I'm only looking for basic configuration information.

@dsyer
Copy link
Member

dsyer commented May 8, 2014

Nevertheless this is a Core Spring "feature" (the non-appearance of a PropertySourcesPlaceholderConfigurer in a virgin ApplicationContext) and the only Boot feature you are really using is the opinionated location of the "application.yml". If you want the @Value to "just work" then you need to use @EnableAutoConfiguration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants