Search in sources :

Example 1 with MergedAnnotation

use of cn.taketoday.core.annotation.MergedAnnotation in project today-framework by TAKETODAY.

the class AspectAutoProxyCreator method addCandidateAdvisors.

protected void addCandidateAdvisors() {
    BeanFactory beanFactory = getFactory();
    Set<String> aspectBeanNames = beanFactory.getBeanNamesForAnnotation(Aspect.class);
    for (String name : aspectBeanNames) {
        log.info("Found Aspect: [{}]", name);
        Class<?> aspectClass = beanFactory.getType(name);
        Assert.state(aspectClass != null, "Cannot determine bean type");
        BeanDefinition aspectDef = beanFactory.getBeanDefinition(name);
        // around
        if (MethodInterceptor.class.isAssignableFrom(aspectClass)) {
            Stream<MergedAnnotation<Advice>> adviceAttributes = getAdviceAttributes(aspectDef);
            addCandidateAdvisors(aspectDef, null, adviceAttributes);
        }
        // annotations: @AfterReturning @Around @Before @After @AfterThrowing
        Method[] declaredMethods = ReflectionUtils.getDeclaredMethods(aspectClass);
        for (Method aspectMethod : declaredMethods) {
            Stream<MergedAnnotation<Advice>> adviceAttributes = getAdviceAttributes(aspectMethod);
            addCandidateAdvisors(aspectDef, aspectMethod, adviceAttributes);
        }
    }
}
Also used : MergedAnnotation(cn.taketoday.core.annotation.MergedAnnotation) BeanFactory(cn.taketoday.beans.factory.BeanFactory) AutowireCapableBeanFactory(cn.taketoday.beans.factory.config.AutowireCapableBeanFactory) Method(java.lang.reflect.Method) BeanDefinition(cn.taketoday.beans.factory.config.BeanDefinition) AnnotatedBeanDefinition(cn.taketoday.beans.factory.annotation.AnnotatedBeanDefinition)

Example 2 with MergedAnnotation

use of cn.taketoday.core.annotation.MergedAnnotation in project today-framework by TAKETODAY.

the class MapperScannerRegistrar method registerBeanDefinitions.

protected void registerBeanDefinitions(AnnotationMetadata annoMeta, MergedAnnotation<MapperScan> mapperScan, BeanDefinitionRegistry registry, String beanName) {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(MapperScannerConfigurer.class);
    builder.addPropertyValue("processPropertyPlaceHolders", true);
    Class<? extends Annotation> annotationClass = mapperScan.getClass("annotationClass");
    if (!Annotation.class.equals(annotationClass)) {
        builder.addPropertyValue("annotationClass", annotationClass);
    }
    Class<?> markerInterface = mapperScan.getClass("markerInterface");
    if (!Class.class.equals(markerInterface)) {
        builder.addPropertyValue("markerInterface", markerInterface);
    }
    Class<? extends BeanNameGenerator> generatorClass = mapperScan.getClass("nameGenerator");
    if (!BeanNameGenerator.class.equals(generatorClass)) {
        builder.addPropertyValue("nameGenerator", BeanUtils.newInstance(generatorClass));
    }
    Class<? extends MapperFactoryBean<?>> mapperFactoryBeanClass = mapperScan.getClass("factoryBean");
    if (!MapperFactoryBean.class.equals(mapperFactoryBeanClass)) {
        builder.addPropertyValue("mapperFactoryBeanClass", mapperFactoryBeanClass);
    }
    String sqlSessionTemplateRef = mapperScan.getString("sqlSessionTemplateRef");
    if (StringUtils.hasText(sqlSessionTemplateRef)) {
        builder.addPropertyValue("sqlSessionTemplateBeanName", sqlSessionTemplateRef);
    }
    String sqlSessionFactoryRef = mapperScan.getString("sqlSessionFactoryRef");
    if (StringUtils.hasText(sqlSessionFactoryRef)) {
        builder.addPropertyValue("sqlSessionFactoryBeanName", sqlSessionFactoryRef);
    }
    ArrayList<String> basePackages = new ArrayList<>();
    basePackages.addAll(Arrays.stream(mapperScan.getStringArray("value")).filter(StringUtils::hasText).toList());
    basePackages.addAll(Arrays.stream(mapperScan.getStringArray("basePackages")).filter(StringUtils::hasText).toList());
    basePackages.addAll(Arrays.stream(mapperScan.getClassArray("basePackageClasses")).map(ClassUtils::getPackageName).toList());
    if (basePackages.isEmpty()) {
        basePackages.add(getDefaultBasePackage(annoMeta));
    }
    String lazyInitialization = mapperScan.getString("lazyInitialization");
    if (StringUtils.hasText(lazyInitialization)) {
        builder.addPropertyValue("lazyInitialization", lazyInitialization);
    }
    String defaultScope = mapperScan.getString("defaultScope");
    if (StringUtils.hasText(defaultScope)) {
        builder.addPropertyValue("defaultScope", defaultScope);
    }
    builder.addPropertyValue("basePackage", StringUtils.collectionToCommaDelimitedString(basePackages));
    builder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
    registry.registerBeanDefinition(beanName, builder.getRawBeanDefinition());
}
Also used : MapperFactoryBean(cn.taketoday.orm.mybatis.mapper.MapperFactoryBean) ArrayList(java.util.ArrayList) Annotation(java.lang.annotation.Annotation) MergedAnnotation(cn.taketoday.core.annotation.MergedAnnotation) ClassUtils(cn.taketoday.util.ClassUtils) BeanNameGenerator(cn.taketoday.beans.factory.support.BeanNameGenerator) BeanDefinitionBuilder(cn.taketoday.beans.factory.support.BeanDefinitionBuilder) StringUtils(cn.taketoday.util.StringUtils)

Example 3 with MergedAnnotation

use of cn.taketoday.core.annotation.MergedAnnotation in project today-infrastructure by TAKETODAY.

the class MapperScannerRegistrar method registerBeanDefinitions.

protected void registerBeanDefinitions(AnnotationMetadata annoMeta, MergedAnnotation<MapperScan> mapperScan, BeanDefinitionRegistry registry, String beanName) {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(MapperScannerConfigurer.class);
    builder.addPropertyValue("processPropertyPlaceHolders", true);
    Class<? extends Annotation> annotationClass = mapperScan.getClass("annotationClass");
    if (!Annotation.class.equals(annotationClass)) {
        builder.addPropertyValue("annotationClass", annotationClass);
    }
    Class<?> markerInterface = mapperScan.getClass("markerInterface");
    if (!Class.class.equals(markerInterface)) {
        builder.addPropertyValue("markerInterface", markerInterface);
    }
    Class<? extends BeanNameGenerator> generatorClass = mapperScan.getClass("nameGenerator");
    if (!BeanNameGenerator.class.equals(generatorClass)) {
        builder.addPropertyValue("nameGenerator", BeanUtils.newInstance(generatorClass));
    }
    Class<? extends MapperFactoryBean<?>> mapperFactoryBeanClass = mapperScan.getClass("factoryBean");
    if (!MapperFactoryBean.class.equals(mapperFactoryBeanClass)) {
        builder.addPropertyValue("mapperFactoryBeanClass", mapperFactoryBeanClass);
    }
    String sqlSessionTemplateRef = mapperScan.getString("sqlSessionTemplateRef");
    if (StringUtils.hasText(sqlSessionTemplateRef)) {
        builder.addPropertyValue("sqlSessionTemplateBeanName", sqlSessionTemplateRef);
    }
    String sqlSessionFactoryRef = mapperScan.getString("sqlSessionFactoryRef");
    if (StringUtils.hasText(sqlSessionFactoryRef)) {
        builder.addPropertyValue("sqlSessionFactoryBeanName", sqlSessionFactoryRef);
    }
    ArrayList<String> basePackages = new ArrayList<>();
    basePackages.addAll(Arrays.stream(mapperScan.getStringArray("value")).filter(StringUtils::hasText).toList());
    basePackages.addAll(Arrays.stream(mapperScan.getStringArray("basePackages")).filter(StringUtils::hasText).toList());
    basePackages.addAll(Arrays.stream(mapperScan.getClassArray("basePackageClasses")).map(ClassUtils::getPackageName).toList());
    if (basePackages.isEmpty()) {
        basePackages.add(getDefaultBasePackage(annoMeta));
    }
    String lazyInitialization = mapperScan.getString("lazyInitialization");
    if (StringUtils.hasText(lazyInitialization)) {
        builder.addPropertyValue("lazyInitialization", lazyInitialization);
    }
    String defaultScope = mapperScan.getString("defaultScope");
    if (StringUtils.hasText(defaultScope)) {
        builder.addPropertyValue("defaultScope", defaultScope);
    }
    builder.addPropertyValue("basePackage", StringUtils.collectionToCommaDelimitedString(basePackages));
    builder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
    registry.registerBeanDefinition(beanName, builder.getRawBeanDefinition());
}
Also used : MapperFactoryBean(cn.taketoday.orm.mybatis.mapper.MapperFactoryBean) ArrayList(java.util.ArrayList) Annotation(java.lang.annotation.Annotation) MergedAnnotation(cn.taketoday.core.annotation.MergedAnnotation) ClassUtils(cn.taketoday.util.ClassUtils) BeanNameGenerator(cn.taketoday.beans.factory.support.BeanNameGenerator) BeanDefinitionBuilder(cn.taketoday.beans.factory.support.BeanDefinitionBuilder) StringUtils(cn.taketoday.util.StringUtils)

Example 4 with MergedAnnotation

use of cn.taketoday.core.annotation.MergedAnnotation in project today-framework by TAKETODAY.

the class AspectAutoProxyCreator method addCandidateAdvisors.

private void addCandidateAdvisors(BeanDefinition aspectDef, @Nullable Method aspectMethod, Stream<MergedAnnotation<Advice>> adviceAttributes) {
    adviceAttributes.forEach(advice -> {
        MethodInterceptor interceptor = getInterceptor(aspectDef, aspectMethod, advice);
        if (log.isTraceEnabled()) {
            log.trace("Found Interceptor: [{}]", interceptor);
        }
        // Annotations
        Class<? extends Annotation>[] annotations = advice.getClassArray(MergedAnnotation.VALUE);
        if (ObjectUtils.isNotEmpty(annotations)) {
            for (Class<? extends Annotation> annotation : annotations) {
                AnnotationMatchingPointcut matchingPointcut = AnnotationMatchingPointcut.forMethodAnnotation(annotation);
                DefaultPointcutAdvisor pointcutAdvisor = new DefaultPointcutAdvisor(matchingPointcut, interceptor);
                annotationAdvisors.add(pointcutAdvisor);
            }
        }
    });
}
Also used : AnnotationMatchingPointcut(cn.taketoday.aop.support.AnnotationMatchingPointcut) SuppliedMethodInterceptor(cn.taketoday.aop.support.SuppliedMethodInterceptor) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor) MergedAnnotation(cn.taketoday.core.annotation.MergedAnnotation) Annotation(java.lang.annotation.Annotation)

Aggregations

MergedAnnotation (cn.taketoday.core.annotation.MergedAnnotation)4 Annotation (java.lang.annotation.Annotation)3 BeanDefinitionBuilder (cn.taketoday.beans.factory.support.BeanDefinitionBuilder)2 BeanNameGenerator (cn.taketoday.beans.factory.support.BeanNameGenerator)2 MapperFactoryBean (cn.taketoday.orm.mybatis.mapper.MapperFactoryBean)2 ClassUtils (cn.taketoday.util.ClassUtils)2 StringUtils (cn.taketoday.util.StringUtils)2 ArrayList (java.util.ArrayList)2 AnnotationMatchingPointcut (cn.taketoday.aop.support.AnnotationMatchingPointcut)1 DefaultPointcutAdvisor (cn.taketoday.aop.support.DefaultPointcutAdvisor)1 SuppliedMethodInterceptor (cn.taketoday.aop.support.SuppliedMethodInterceptor)1 BeanFactory (cn.taketoday.beans.factory.BeanFactory)1 AnnotatedBeanDefinition (cn.taketoday.beans.factory.annotation.AnnotatedBeanDefinition)1 AutowireCapableBeanFactory (cn.taketoday.beans.factory.config.AutowireCapableBeanFactory)1 BeanDefinition (cn.taketoday.beans.factory.config.BeanDefinition)1 Method (java.lang.reflect.Method)1 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)1