Spring高级话题[email protected]***注解的工作原理

出自:http://blog.csdn.net/qq_26525215

@EnableAspectJAutoProxy

@EnableAspectJAutoProxy注解 激活Aspect自动代理

<aop:aspectj-autoproxy/>

开启对AspectJ自动代理的支持。

在用到AOP的自动代理的时候用,如果你理解了Java的动态代理,很容易的就会熟悉AOP的自动代理的。

@EnableAsync

@EnableAsync注解开启异步方法的支持。 
这个相信大家都比较熟悉的。对于异步应该都理解的。 
不太熟悉的,可以看这篇博客:-有示例 
【Spring】Spring高级话题-多线程-TaskExecutor

@EnableScheduling

@EnableScheduling注解开启计划任务的支持。

也就是字面上的意思,开启计划任务的支持! 
一般都需要@Scheduled注解的配合。

详情见此博客: 
【Spring】Spring高级话题-计划任务[email protected]

@EnableWebMVC

@EnableWebMVC注解用来开启Web MVC的配置支持。

也就是写Spring MVC时的时候会用到。

@EnableConfigurationProperties

@EnableConfigurationProperties注解是用来开启对@ConfigurationProperties注解配置Bean的支持。

也就是@EnableConfigurationProperties注解告诉Spring Boot 使能支持@ConfigurationProperties

@EnableJpaRepositories

@EnableJpaRepositories注解开启对Spring Data JPA Repostory的支持。

Spring Data JPA 框架,主要针对的就是 Spring 唯一没有简化到的业务逻辑代码,至此,开发者连仅剩的实现持久层业务逻辑的工作都省了,唯一要做的,就只是声明持久层的接口,其他都交给 Spring Data JPA 来帮你完成!

简单的说,Spring Data JPA是用来持久化数据的框架。

@EnableTransactionManagement

@EnableTransactionManagement注解开启注解式事务的支持。

注解@EnableTransactionManagement通知Spring,@Transactional注解的类被事务的切面包围。这样@Transactional就可以使用了。

@EnableCaching

@EnableCaching注解开启注解式的缓存支持

通过这些简单的@Enable*可以开启一项功能的支持,从而避免自己配置大量的代码,很大程度上降低了使用难度。

我们一起来观察下这些@Enable*注解的源码,可以发现所有的注解都有一个@Import注解。

@Import注解是用来导入配置类的,这也就是说这些自动开启的实现其实是导入了一些自动配置的Bean。

这些导入配置方式主要分为以下三种类型。

@Import注解导入配置方式的三种类型

第一类:直接导入配置类

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.springframework.scheduling.annotation;

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;
import org.springframework.context.annotation.Import;
import org.springframework.scheduling.annotation.SchedulingConfiguration;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Import({SchedulingConfiguration.class})
@Documented
public @interface EnableScheduling {
}

直接导入配置类SchedulingConfiguration,这个类注解了@Configuration,且注册了一个scheduledAnnotationProcessor的Bean,源码如下:

/*
 * Copyright 2002-2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.scheduling.annotation;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Role;
import org.springframework.scheduling.config.TaskManagementConfigUtils;

/**
 * {@code @Configuration} class that registers a {@link ScheduledAnnotationBeanPostProcessor}
 * bean capable of processing Spring‘s @{@link Scheduled} annotation.
 *
 * <p>This configuration class is automatically imported when using the
 * @{@link EnableScheduling} annotation. See {@code @EnableScheduling}‘s javadoc
 * for complete usage details.
 *
 * @author Chris Beams
 * @since 3.1
 * @see EnableScheduling
 * @see ScheduledAnnotationBeanPostProcessor
 */
@Configuration
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public class SchedulingConfiguration {

    @Bean(name = TaskManagementConfigUtils.SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME)
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    public ScheduledAnnotationBeanPostProcessor scheduledAnnotationProcessor() {
        return new ScheduledAnnotationBeanPostProcessor();
    }

}

第二类:依据条件选择配置类

EnableAsync 注解核心代码:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AsyncConfigurationSelector.class)
public @interface EnableAsync {
    Class<? extends Annotation> annotation() default Annotation.class;
    boolean proxyTargetClass() default false;
    AdviceMode mode() default AdviceMode.PROXY;
    int order() default Ordered.LOWEST_PRECEDENCE;

}

AsyncConfigurationSelector通过条件来选择需要导入的配置类, 
AsyncConfigurationSelector的根接口为ImportSelector,这个接口需要重写selectImports方法,在此方法内进行事先条件判断。

在下面的源码中,若adviceMode为PORXY,则返回ProxyAsyncConfiguration这个配置类。 
若activeMode为ASPECTJ,则返回AspectJAsyncConfiguration配置类。 
源码如下:

public class AsyncConfigurationSelector extends AdviceModeImportSelector<EnableAsync> {

    private static final String ASYNC_EXECUTION_ASPECT_CONFIGURATION_CLASS_NAME =
            "org.springframework.scheduling.aspectj.AspectJAsyncConfiguration";

    /**
     * {@inheritDoc}
     * @return {@link ProxyAsyncConfiguration} or {@code AspectJAsyncConfiguration} for
     * {@code PROXY} and {@code ASPECTJ} values of {@link EnableAsync#mode()}, respectively
     */
    @Override
    public String[] selectImports(AdviceMode adviceMode) {
        switch (adviceMode) {
            case PROXY:
                return new String[] { ProxyAsyncConfiguration.class.getName() };
            case ASPECTJ:
                return new String[] { ASYNC_EXECUTION_ASPECT_CONFIGURATION_CLASS_NAME };
            default:
                return null;
        }
    }

}

第三类:动态注册Bean

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AspectJAutoProxyRegistrar.class)
public @interface EnableAspectJAutoProxy {
    boolean proxyTargetClass() default false;
}

AspectJAutoProxyRegistrar 事先了ImportBeanDefinitionRegistrar接口,ImportBeanDefinitionRegistrar的作用是在运行时自动添加Bean到已有的配置类,通过重写方法:

@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry)

其中,AnnotationMetadata参数用来获得当前配置类上的注解; BeanDefinittionRegistry参数用来注册Bean。 
源码如下:

class AspectJAutoProxyRegistrar implements ImportBeanDefinitionRegistrar {

    /**
     * Register, escalate, and configure the AspectJ auto proxy creator based on the value
     * of the @{@link EnableAspectJAutoProxy#proxyTargetClass()} attribute on the importing
     * {@code @Configuration} class.
     */
    @Override
    public void registerBeanDefinitions(
            AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

        AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry);

        AnnotationAttributes enableAJAutoProxy =
                AnnotationConfigUtils.attributesFor(importingClassMetadata, EnableAspectJAutoProxy.class);
        if (enableAJAutoProxy.getBoolean("proxyTargetClass")) {
            AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
        }
    }

}
 
时间: 2024-08-28 00:50:43

Spring高级话题[email protected]***注解的工作原理的相关文章

[email&#160;protected]注解与自动装配

1   配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss 拥有 Office 和 Car 类型的两个属性:       清单 3. Boss.java [java] view plaincopy package com.baobaotao; public class Boss { private Car car; private Office office

springboot情操陶冶[email&#160;protected]注解解析

承接前文springboot情操陶冶[email protected]注解解析,本文将在前文的基础上对@SpringBootApplication注解作下简单的分析 @SpringBootApplication 该注解是springboot最集中的一个注解,也是应用最广泛的注解.官方也多用此注解以启动spring服务,我们看下其中的源码 @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inher

springboot自动装配(1)[email&#160;protected]注解怎么自动装配各种组件

1.对于springboot个人认为它就是整合了各种组件,然后提供对应的自动装配和启动器(starter) [email protected]注解其实就是组合注解,通过它找到自动装配的注解@EnableAutoConfiguration,再由@EnableAutoConfiguration导入自动装配选择类AutoConfigurationImportSelector的selectImports方法去MATA-INF/spring.factories下面找到需要自动装配的组件的对应配置(各种Au

s[email&#160;protected]注解

自动将数据封装成json格式的数据返回回去 Maven <!-- Json Begin --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackso

spring事务(4)-----复习[email&#160;protected]注解、@Resource注解和@Service注解(为手写做准备)

什么是注解 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点: 1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件,那么.xml文件又会非常多.总之这将导致配置文件的可读性与可维护性变得很低 2.在开发中在.java文件和.xml文件之间不断切换,是一件麻烦的事,同时这种思维上的不连贯也会降低开发的效率 为了解决这两个问题,Spring引入了注解,通过"@XXX"的方式,让注解与Java

开始学习Spring Boot,启动类的注解(一)(工作日记)

这个博客的目的只是为了给懒惰的自己学习知识的时候总结用 喜欢高深的知识的人请到别的空间,谢谢!!! 虽然已经经历了两个Spring Boot项目了,但是仍然不知道自己在干什么,它是怎么实现的,完全不清楚. 因此在网上看了一些人的资料,决定根据自己的理解,写下这一篇博客,就当作工作日记吧!!! 说说Spring Boot的感受,感觉很方便,部署环境很快,在工作中我基本都是复制一个已经有的模块,删减掉我不用的文件,改一下pom文件,属于自己的模块就出来了. 之后maven加载一下需要的jar包,之后

[email&#160;protected] 注解原理与使用

Java反射 java反射机制的定义: 在运行转态时(动态的)时. 对于任意一个类,都能够知道这个类的所有属性和方法 对于任意一个对象,都能够知道调用它的任意属性和方法 Class对象 java中用对象来对现实生活中的事物进行抽象,如人(现实生活)抽象到一个person类(java对象).但有没有想过,java中的类(现实生活)其实也是一个Class对象(对象).因此,这个Class类就包含了所有你定义的Class信息,包括所有的方法(私有,公有).构造器.实现了那些方法.哪些注解信息.所有的属

工作积累(五)——使用[email&#160;protected]注解实现常量功能

之前的博客中提到过如何通过 java.util.ResourceBundle 和 java.util.Properties类通过读取 key-value 文件的形式实现常量功能.其实 spring 已经通过@Value 注解实现,下面看看如何使用. 1.创建.properties文件: 在如下目录创建 keyvalue.properties文件src/main/resources/META-INF/spring/keyvalue.properties ,写入如下内容: test.value=il

Spring In [email&#160;protected]注解

//@Component注解会告诉Spring创建这个类的实例bean(注意,启动Component注解功能需要在xml里面配置,下面会将)@Component package com.zte.springinaction.soundsystem.imp; import org.springframework.stereotype.Component; import com.zte.springinaction.soundsystem.CompactDisc; //@Component注解会告诉