Initial commit for "Spring Custom Annotation with Dynamic Qualifier"

article.
This commit is contained in:
xsalefter
2015-10-21 00:50:44 +07:00
parent 6aec841b73
commit 32ea1e0a1e
10 changed files with 394 additions and 0 deletions
@@ -0,0 +1,9 @@
package org.baeldung.customannotation;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("org.baeldung.customannotation")
public class CustomAnnotationConfiguration {
}
@@ -0,0 +1,14 @@
package org.baeldung.customannotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
@Documented
public @interface DataAccess {
Class<?>entity();
}
@@ -0,0 +1,39 @@
package org.baeldung.customannotation;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.ReflectionUtils.FieldCallback;
@Component
public class DataAccessAnnotationProcessor implements BeanPostProcessor {
private ConfigurableListableBeanFactory configurableListableBeanFactory;
@Autowired
public DataAccessAnnotationProcessor(ConfigurableListableBeanFactory bf) {
configurableListableBeanFactory = bf;
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
scanDataAccessAnnotation(bean, beanName);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
return bean;
}
protected void scanDataAccessAnnotation(Object bean, String beanName) {
Class<?> managedBeanClass = bean.getClass();
FieldCallback fcb = new DataAccessFieldCallback(configurableListableBeanFactory, bean);
ReflectionUtils.doWithFields(managedBeanClass, fcb);
}
}
@@ -0,0 +1,104 @@
package org.baeldung.customannotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.ReflectionUtils.FieldCallback;
public final class DataAccessFieldCallback implements FieldCallback {
private static Logger logger = LoggerFactory.getLogger(DataAccessFieldCallback.class);
private static int AUTOWIRE_MODE = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME;
private static String ERROR_ENTITY_VALUE_NOT_SAME = "@DataAccess(entity) "
+ "value should have same type with injected generic type.";
private static String WARN_NON_GENERIC_VALUE = "@DataAccess annotation assigned "
+ "to raw (non-generic) declaration. This will make your code less type-safe.";
private static String ERROR_CREATE_INSTANCE = "Cannot create instance of "
+ "type '{}' or instance creation is failed because: {}";
private ConfigurableListableBeanFactory configurableListableBeanFactory;
private Object bean;
public DataAccessFieldCallback(final ConfigurableListableBeanFactory bf, final Object bean) {
configurableListableBeanFactory = bf;
this.bean = bean;
}
@Override
public void doWith(final Field field)
throws IllegalArgumentException, IllegalAccessException {
if (!field.isAnnotationPresent(DataAccess.class)) {
return;
}
ReflectionUtils.makeAccessible(field);
final Type fieldGenericType = field.getGenericType();
// In this example, get actual "GenericDAO' type.
final Class<?> generic = field.getType();
final Class<?> classValue = field.getDeclaredAnnotation(DataAccess.class).entity();
if (genericTypeIsValid(classValue, fieldGenericType)) {
final String beanName = classValue.getSimpleName() + generic.getSimpleName();
final Object beanInstance = getBeanInstance(beanName, generic, classValue);
field.set(bean, beanInstance);
} else {
throw new IllegalArgumentException(ERROR_ENTITY_VALUE_NOT_SAME);
}
}
/**
* For example, if user write:
* <pre>
* &#064;DataAccess(entity=Person.class)
* private GenericDAO&lt;Account&gt; personGenericDAO;
* </pre>
* then this is should be failed.
*/
public boolean genericTypeIsValid(final Class<?> clazz, final Type field) {
if (field instanceof ParameterizedType) {
final ParameterizedType parameterizedType = (ParameterizedType) field;
final Type type = parameterizedType.getActualTypeArguments()[0];
return type.equals(clazz);
} else {
logger.warn(WARN_NON_GENERIC_VALUE);
return true;
}
}
public final Object getBeanInstance(final String beanName, final Class<?> genericClass, final Class<?> paramClass) {
Object daoInstance = null;
if (!configurableListableBeanFactory.containsBean(beanName)) {
logger.info("Creating new DataAccess bean named '{}'.", beanName);
Object toRegister = null;
try {
final Constructor<?> ctr = genericClass.getConstructor(Class.class);
toRegister = ctr.newInstance(paramClass);
} catch (final Exception e) {
logger.error(ERROR_CREATE_INSTANCE, genericClass.getTypeName(), e);
throw new RuntimeException(e);
}
daoInstance = configurableListableBeanFactory.initializeBean(toRegister, beanName);
configurableListableBeanFactory.autowireBeanProperties(daoInstance, AUTOWIRE_MODE, true);
configurableListableBeanFactory.registerSingleton(beanName, daoInstance);
logger.info("Bean named '{}' created successfully.", beanName);
} else {
daoInstance = configurableListableBeanFactory.getBean(beanName);
logger.info("Bean named '{}' already exist used as current bean reference.", beanName);
}
return daoInstance;
}
}
@@ -0,0 +1,30 @@
package org.baeldung.customannotation;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
public class GenericDAO<E> {
private Class<E> entityClass;
private String message;
public GenericDAO(Class<E> entityClass) {
this.entityClass = entityClass;
}
public List<E> findAll() {
message = "Would create findAll query from " + entityClass.getSimpleName();
return Collections.emptyList();
}
public Optional<E> persist(E toPersist) {
message = "Would create persist query from " + toPersist.getClass().getSimpleName();
return Optional.empty();
}
/** Only used for unit-testing. */
public final String getMessage() {
return message;
}
}