com.nepxion.matrix.registrar.AbstractRegistrar Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of matrix-aop Show documentation
Show all versions of matrix-aop Show documentation
Nepxion Matrix is an AOP framework integrated with Spring AutoProxy, Spring Registrar and Spring Import Selector
package com.nepxion.matrix.registrar;
/**
* Title: Nepxion Matrix
* Description: Nepxion Matrix AOP
* Copyright: Copyright (c) 2017-2050
* Company: Nepxion
* @author Haojun Ren
* @version 1.0
*/
import java.lang.annotation.Annotation;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.aopalliance.intercept.MethodInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
public abstract class AbstractRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware, BeanClassLoaderAware, EnvironmentAware {
private static final Logger LOG = LoggerFactory.getLogger(AbstractRegistrar.class);
static {
String logoShown = System.getProperty("nepxion.logo.shown", "true");
if (Boolean.valueOf(logoShown)) {
System.out.println("");
System.out.println("╔═╗╔═╗ ╔╗");
System.out.println("║║╚╝║║ ╔╝╚╗");
System.out.println("║╔╗╔╗╠══╬╗╔╬═╦╦╗╔╗");
System.out.println("║║║║║║╔╗║║║║╔╬╬╬╬╝");
System.out.println("║║║║║║╔╗║║╚╣║║╠╬╬╗");
System.out.println("╚╝╚╝╚╩╝╚╝╚═╩╝╚╩╝╚╝");
System.out.println("Nepxion Matrix - Registrar v2.0.4");
System.out.println("");
}
}
private ResourceLoader resourceLoader;
private ClassLoader classLoader;
private Environment environment;
public AbstractRegistrar() {
}
@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
}
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
@Override
public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
registerAnnotations(metadata, registry);
}
public void registerAnnotations(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
ClassPathScanningCandidateComponentProvider scanner = getScanner();
scanner.setResourceLoader(this.resourceLoader);
AnnotationTypeFilter annotationTypeFilter = new AnnotationTypeFilter(getAnnotationClass());
scanner.addIncludeFilter(annotationTypeFilter);
Set basePackages = getBasePackages(metadata);
for (String basePackage : basePackages) {
Set candidateComponents = scanner.findCandidateComponents(basePackage);
for (BeanDefinition candidateComponent : candidateComponents) {
if (candidateComponent instanceof AnnotatedBeanDefinition) {
// verify annotated class is an interface
AnnotatedBeanDefinition beanDefinition = (AnnotatedBeanDefinition) candidateComponent;
AnnotationMetadata annotationMetadata = beanDefinition.getMetadata();
Map attributes = annotationMetadata.getAnnotationAttributes(getAnnotationClass().getCanonicalName());
registerAnnotation(registry, annotationMetadata, attributes);
}
}
}
}
private void registerAnnotation(BeanDefinitionRegistry registry, AnnotationMetadata annotationMetadata, Map attributes) {
String className = annotationMetadata.getClassName();
LOG.info("Found annotation [{}] in {} ", getAnnotationClass().getSimpleName(), className);
BeanDefinitionBuilder definition = BeanDefinitionBuilder.genericBeanDefinition(getBeanClass());
AbstractBeanDefinition beanDefinition = definition.getBeanDefinition();
customize(registry, annotationMetadata, attributes, definition);
try {
definition.addPropertyValue("interfaze", Class.forName(className));
} catch (ClassNotFoundException e) {
LOG.error("Get interface for name error", e);
}
definition.addPropertyValue("interceptor", getInterceptor(beanDefinition.getPropertyValues()));
definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
String alias = className;
BeanDefinitionHolder holder = new BeanDefinitionHolder(beanDefinition, className, new String[] { alias });
BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry);
}
protected ClassPathScanningCandidateComponentProvider getScanner() {
return new ClassPathScanningCandidateComponentProvider(false, this.environment) {
@Override
protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
if (beanDefinition.getMetadata().isIndependent()) {
if (beanDefinition.getMetadata().isInterface() && beanDefinition.getMetadata().getInterfaceNames().length == 1 && Annotation.class.getName().equals(beanDefinition.getMetadata().getInterfaceNames()[0])) {
try {
Class> target = ClassUtils.forName(beanDefinition.getMetadata().getClassName(), AbstractRegistrar.this.classLoader);
return !target.isAnnotation();
} catch (Exception ex) {
LOG.error("Could not load target class: " + beanDefinition.getMetadata().getClassName(), ex);
}
}
return true;
}
return false;
}
};
}
protected Set getBasePackages(AnnotationMetadata importingClassMetadata) {
Map attributes = importingClassMetadata.getAnnotationAttributes(getEnableAnnotationClass().getCanonicalName());
Set basePackages = new HashSet<>();
for (String pkg : (String[]) attributes.get("value")) {
if (StringUtils.hasText(pkg)) {
basePackages.add(pkg);
}
}
for (String pkg : (String[]) attributes.get("basePackages")) {
if (StringUtils.hasText(pkg)) {
basePackages.add(pkg);
}
}
for (Class> clazz : (Class[]) attributes.get("basePackageClasses")) {
basePackages.add(ClassUtils.getPackageName(clazz));
}
if (basePackages.isEmpty()) {
basePackages.add(ClassUtils.getPackageName(importingClassMetadata.getClassName()));
}
return basePackages;
}
protected void customize(BeanDefinitionRegistry registry, AnnotationMetadata annotationMetadata, Map attributes, BeanDefinitionBuilder definition) {
for (Map.Entry attribute : attributes.entrySet()) {
definition.addPropertyValue(attribute.getKey(), attribute.getValue());
}
}
protected abstract Class extends Annotation> getEnableAnnotationClass();
protected abstract Class extends Annotation> getAnnotationClass();
protected abstract Class> getBeanClass();
protected abstract MethodInterceptor getInterceptor(MutablePropertyValues annotationValues);
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy