'Spring - @Value annotation not working when using context.getBean
Here is my app config
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Value("${sample.config.application-resources-encoding}")
private String sMessageSourceEncoding_;
public WebMvcConfig() {
}
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource bean = new ReloadableResourceBundleMessageSource();
bean.setBasename("classpath:messages");
bean.setDefaultEncoding(sMessageSourceEncoding_);
return bean;
}
}
Here is where I use context.getBean
public class Message {
private MessageSource ms;
public Message() {
ApplicationContext context = new AnnotationConfigApplicationContext(WebMvcConfig.class);
ms = (MessageSource) context.getBean("messageSource");
((AnnotationConfigApplicationContext) context).close();
}
}
At first when WebMvcConfig is loaded, the @Value annotation is working fine but if I use context.getBean, the @Value annotation does not work anymore. Is there any way to make it work using context.getBean?
I know I can just use @Autowired but I'm trying to migrate a struts1 to spring and I need the MessageSource in new instance but I couldn't make the @Value annotation to work the way I want.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
