Reputation: 560
cfg c= context.getBean(cfg.class);
First time it has to be work, but second time appear error:
No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Why?
@Configuration
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
class cfg{
public cfg(@Value("${xx}") String xx) {
System.out.println(xx);
}
}
application.properties
xx = 7
Also I found that if replace @Configuration with @Component or add (proxyBeanMethods = false) the problem goes away.
Upvotes: 0
Views: 374
Reputation: 17460
@Configuration
indicates that a class declares one or more @Bean
methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime. If you want @Value
to work you will need @PropertySource
annotation.
If you annotate it with @Component
, then it will be a fully-fledged Spring Bean on which @Value
works out of the box.
Upvotes: 1