Skip to content

@Component class using @Value cannot access @PropertySource proeprties [SPR-8516] #13161

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
spring-projects-issues opened this issue Jul 6, 2011 · 10 comments
Labels
status: declined A suggestion or change that we don't feel we should currently apply

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Jul 6, 2011

Marcel Overdijk opened SPR-8516 and commented

I'm using a @Configuration class like:

@Configuration
@ComponentScan(basePackages = {"nl.vooty"}, excludeFilters = {@Filter(type = FilterType.ANNOTATION, value = Configuration.class)})
@PropertySource("classpath:/META-INF/config.properties")
public class AppConfig {
    ..
}

In a @Component class I would like to inject a property value using the @Value annotation like:

@Component
public class AuthenticationFailureBadCredentialsListener implements ApplicationListener<AuthenticationFailureBadCredentialsEvent> {
    
    @Value("${authentication.max.login.failures}")
    private String maxLoginFailures;

    ..
}

Although the property exists, it's not injected in the variable.

When logging the value of the maxLoginFailures it is: "${authentication.max.login.failures}"

Note that I'm testing on M2.


Affects: 3.1 M2

Issue Links:

@spring-projects-issues
Copy link
Collaborator Author

Chris Beams commented

Hi Marcel,

This is actually as designed. Replacement of ${...} placeholders, whether in XML 'value' attributes or in @Value annotations must be done with a PropertyPlaceholderConfigurer or PropertySourcesPlaceholderConfigurer. The latter is aware of the Environment and thus any registered PropertySources. The latter is registered by default when using <context:property-placeholder/> under Spring 3.1. You can of course also register this as a @Bean, but it is recommended to declare the method as static. See @Bean annotation Javadoc for details why.

@spring-projects-issues
Copy link
Collaborator Author

Marcel Overdijk commented

OK Chris, my bad.
Was in the wrong impression that this was configured automagically.

Defining

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}

works perfectly.

@spring-projects-issues
Copy link
Collaborator Author

Chris Beams commented

Great!

@spring-projects-issues
Copy link
Collaborator Author

Mikhail Tokarev commented

Chris,

Why a replacement of ${...} placeholders for Resource type using @Value annotation works fine?

@Controller
public class MyController {

    @Value("${some.path}")
    private Resource resources;

i'm using spring 3.1.1.RELEASE

@spring-projects-issues
Copy link
Collaborator Author

Chris Beams commented

Mikhail,

I'm not sure exactly what you're asking here. Can you elaborate? Thanks.

@spring-projects-issues
Copy link
Collaborator Author

Mikhail Tokarev commented

I'm using @Configuration class like:

@Configuration
@PropertySource("classpath:/config.properties")
public class AppConfig {
    ...
}

In a @Component class I use @Value annotations like:

@Component
public class MyComponent {
    @Value("${folderPath}")
    private Resource folderPath;

    @Value("${propertyValue}")
    private String propertyValue;
}

My config.properties file:

folderPath=file:/tmp
propertyValue=test

The value of the propertyValue it is: "${propertyValue}". But the value of the folderPath it is: "file:/tmp".

@spring-projects-issues
Copy link
Collaborator Author

Chris Beams commented

Right. Have you added a PropertySourcesPlaceholderConfigurer @Bean as mentioned above?

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}

This is also described in @Bean Javadoc. See "A note on BeanFactoryPostProcessor-returning @Bean methods".

@spring-projects-issues
Copy link
Collaborator Author

Mikhail Tokarev commented

No, I have not added PSPC. I understand why it does not work for String, but I do not understand why it works for Resource.

@spring-projects-issues
Copy link
Collaborator Author

Chris Beams commented

Sorry Mikhail, I misunderstood your question. The reason it works by default for Resource is because ResourceEditor is at work here. ResourceEditor is, as of 3.1, aware of PropertySources via the PropertyResolver interface (which is ultimately your ApplicationContext's Environment).

So the @PropertySource you register up top is automatically being considered by ResourceEditor. Hope that helps!

@spring-projects-issues
Copy link
Collaborator Author

Mikhail Tokarev commented

ok, I see. In my opinion this is not an explicit behavior and I would like at this specify.
Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

No branches or pull requests

1 participant