I have a BeanPostProcessor bean and I want to inject two string variables with values from application.properties file. However, they are not getting injected and left with a placeholder's value.
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
@Value("${property1}")
private String property1;
@Value("${property2}")
private String property2;
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
...
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
...
return bean;
}
}
In both method values of the variables are ${property1}
and ${property2}
. I've tried injecting them in a regular bean and it works fine, so it definitely has to do something with the bean being BeanPostProcessor.
Is there a way to inject values somehow? I'm using Spring Boot 2.2.0.
It's intended. See docs for @Value
:
Note that actual processing of the @Value annotation is performed by a BeanPostProcessor which in turn means that you cannot use @Value within BeanPostProcessor or BeanFactoryPostProcessor types. Please consult the javadoc for the AutowiredAnnotationBeanPostProcessor class (which, by default, checks for the presence of this annotation).
As a workaround, you can, for example, create your post processor as bean manually inside a class marked as @Configuration
and pass this value from there as a constructor parameter.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.