Created
July 12, 2024 22:47
-
-
Save dmgcodevil/2cd0c5bab3d9d40c768e44e134086047 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ```java | |
| import org.springframework.beans.BeansException; | |
| import org.springframework.beans.factory.config.BeanPostProcessor; | |
| import org.springframework.stereotype.Component; | |
| import java.lang.reflect.Field; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| @Component | |
| public class ValueAnnotationBeanPostProcessor implements BeanPostProcessor { | |
| private final List<BeanField> valueAnnotatedFields = new ArrayList<>(); | |
| @Override | |
| public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { | |
| Field[] fields = bean.getClass().getDeclaredFields(); | |
| for (Field field : fields) { | |
| if (field.isAnnotationPresent(Value.class)) { | |
| valueAnnotatedFields.add(new BeanField(bean, field)); | |
| } | |
| } | |
| return bean; | |
| } | |
| public List<BeanField> getValueAnnotatedFields() { | |
| return valueAnnotatedFields; | |
| } | |
| public static class BeanField { | |
| private final Object bean; | |
| private final Field field; | |
| public BeanField(Object bean, Field field) { | |
| this.bean = bean; | |
| this.field = field; | |
| } | |
| public Object getBean() { | |
| return bean; | |
| } | |
| public Field getField() { | |
| return field; | |
| } | |
| } | |
| } | |
| import org.springframework.beans.factory.annotation.Autowired; | |
| import org.springframework.context.ConfigurableApplicationContext; | |
| import org.springframework.stereotype.Service; | |
| import java.lang.reflect.Field; | |
| import java.util.List; | |
| @Service | |
| public class ValueAnnotatedBeanRefresher { | |
| @Autowired | |
| private ValueAnnotationBeanPostProcessor postProcessor; | |
| @Autowired | |
| private ConfigurableApplicationContext applicationContext; | |
| public void refreshBeans() { | |
| List<ValueAnnotationBeanPostProcessor.BeanField> fields = postProcessor.getValueAnnotatedFields(); | |
| for (ValueAnnotationBeanPostProcessor.BeanField beanField : fields) { | |
| Object bean = beanField.getBean(); | |
| Field field = beanField.getField(); | |
| field.setAccessible(true); | |
| try { | |
| String newValue = applicationContext.getEnvironment().getProperty(field.getAnnotation(Value.class).value().replace("${", "").replace("}", "")); | |
| field.set(bean, newValue); | |
| applicationContext.getBeanFactory().destroyBean(bean); | |
| applicationContext.getBeanFactory().initializeBean(bean, bean.getClass().getName()); | |
| } catch (IllegalAccessException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } | |
| } | |
| ``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment