BAEL-4290: tracking down cause of Spring's “not eligible for auto-proxying” (#10593)

This commit is contained in:
Maciej Główka
2021-03-30 17:53:35 +02:00
committed by GitHub
parent eb35265244
commit ac33d1d438
9 changed files with 258 additions and 0 deletions
@@ -0,0 +1,12 @@
package com.baeldung.component.autoproxying;
import lombok.Getter;
import org.springframework.stereotype.Component;
@Getter
@Component
public class DataCache {
@RandomInt(min = 2, max = 10)
private int group;
private String name;
}
@@ -0,0 +1,33 @@
package com.baeldung.component.autoproxying;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Lazy;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Field;
public class EligibleForAutoProxyRandomIntProcessor implements BeanPostProcessor {
private final RandomIntGenerator randomIntGenerator;
@Lazy
public EligibleForAutoProxyRandomIntProcessor(RandomIntGenerator randomIntGenerator) {
this.randomIntGenerator = randomIntGenerator;
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
Field[] fields = bean.getClass().getDeclaredFields();
for (Field field : fields) {
RandomInt injectRandomInt = field.getAnnotation(RandomInt.class);
if (injectRandomInt != null) {
int min = injectRandomInt.min();
int max = injectRandomInt.max();
int randomValue = randomIntGenerator.generate(min, max);
field.setAccessible(true);
ReflectionUtils.setField(field, bean, randomValue);
}
}
return bean;
}
}
@@ -0,0 +1,31 @@
package com.baeldung.component.autoproxying;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Field;
public class NotEligibleForAutoProxyRandomIntProcessor implements BeanPostProcessor {
private final RandomIntGenerator randomIntGenerator;
public NotEligibleForAutoProxyRandomIntProcessor(RandomIntGenerator randomIntGenerator) {
this.randomIntGenerator = randomIntGenerator;
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
Field[] fields = bean.getClass().getDeclaredFields();
for (Field field : fields) {
RandomInt injectRandomInt = field.getAnnotation(RandomInt.class);
if (injectRandomInt != null) {
int min = injectRandomInt.min();
int max = injectRandomInt.max();
int randomValue = randomIntGenerator.generate(min, max);
field.setAccessible(true);
ReflectionUtils.setField(field, bean, randomValue);
}
}
return bean;
}
}
@@ -0,0 +1,11 @@
package com.baeldung.component.autoproxying;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface RandomInt {
int min();
int max();
}
@@ -0,0 +1,21 @@
package com.baeldung.component.autoproxying;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.Random;
@Slf4j
@Component
public class RandomIntGenerator {
private final Random random = new Random();
private final DataCache dataCache;
public RandomIntGenerator(DataCache dataCache) {
this.dataCache = dataCache;
}
public int generate(int min, int max) {
return random.nextInt(max - min) + min;
}
}