@Component public class InjectRandomIntAnnotationBeanPostProcessor
implements BeanPostProcessor {
@Override public Object postProcessBeforeInitialization(
Object bean, String beanName) throws BeansException {
for (Field f : ReflectionUtils.getAllFields(bean.getClass())) {
InjectRandomInt ann = f.getAnnotation(InjectRandomInt.class);
if (ann != null) {
int value = ThreadLocalRandom.current()
.nextInt(ann.min(), ann.max() + 1);
f.setAccessible(true);
try { f.set(bean, value); }
catch (IllegalAccessException e) {
throw new NotWritablePropertyException(
bean.getClass(), f.getName()); }
} }
return bean;
} }