Spring AOP入门

大三初学SpringAOP,记录我的学习情况以及心得。

 一、首先是了解SpringAOP。

   1、什么是AOP呢?

   AOP是英文Aspect-Oriented Programming 的简写,翻译过来的意思就是面向切面编程,面向方向编程。在SpringAOP中,我们可以理解为:面向方法编程。

    2、AOP在应用场景中的的作用是什么?

    在实际应用场景中,用户的命令会根据Web传入Service再传入Dao,最后由Dao传入数据库,然后数据会再从数据库从来的路径再返回给用户,但在这期间可能会出现数据返回延迟很久的情况。为了保护目标方法源代码不被修改,于是就使用SpringAOP对请求链接路上的目标方法进行耗时统计。

   3、SpringAOP是通过什么什么来实现呢?

    在SpringAOP实现的过程中,使用的技术是动态代理机制来实现对目标方法的编程,而动态代理是目前面向方法编程最主流的实现技术。

   4、AOP的其他应用场景----事务管理

   基于注解的AOP事务管理:

<tx:annotation-driven transaction-manager="transactionManager"/> 
<aop:aspectj-autoproxy /> 

   注解驱动的事务管理支持的核心标签:

<tx:annotation-driven/>

二、SpringAOP的基本配置

1、如果使用的是IDEA,我们首先要新建一个项目,然后在pom.xml中配置后续项目需要的基本依赖。

<dependencies>
    <!--        spring相关坐标-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.8</version>
    </dependency>
    <!--        mysql驱动-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-mxj-db-files</artifactId>
        <version>5.0.12</version>
    </dependency>
    <!--        spring整合jdbc相关坐标-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.3.8</version>
    </dependency>
    <!--        druid连接池-->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.2.15</version>
    </dependency>
    <!--        junit测试-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>RELEASE</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.6</version>
    </dependency>
</dependencies>

如果是使用SpringMVC的项目,则需要引入的依赖有:

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.2</version>
        <scope>test</scope>
    </dependency>
    <!--引入SpringMvc-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.1</version>
    </dependency>
</dependencies>
<!--引入Tomcat服务器配置-->
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat9-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <port>8080</port>
                <path>/</path>
            </configuration>
        </plugin>
    </plugins>
</build>

在配置过程中,如果出现报红的状况,可以刷新一下,这样就正常了。

2、然后就是在项目的src/main/resources中创建一个xml文件,其中的基本配置有:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
    <bean id="logManager" class="com.gcxy.aop.LogManager"></bean>
    <bean id="phoneServiceImpl" class="com.gcxy.service.impl.PhoneServiceImpl"></bean>
</beans>

在基础配置完成过后就可以尝试在java包中创建类来测试一下环境配置是否成功。

三、添加AOP相关的注释

@Aspect:把当前类声明为切面类。

@Around:把当前方法看成是环绕通知。属性value用于指定切入点表达式,还可以制定切入点表达式的引用。

@component@Service@Controller@Repository:这四个注解的功能相同,都是可以实现对象的创建,不过要把每个类作用在对应的层中,使得开发更加清晰明了。

@Configuration:声明一个类为配置类,用于取代bean.xml配置文件中注册的bean对象。

@ComponentScan:用于批量生产bean。

@EnableAspectJAutoProxy:用于开启注解版的SpringAOP功能,类似于使用xml方式的<aop:aspectj-autoproxy>。

@Override:可以是伪代码,表示重写;也可以当做注释,提高可读性。

@Bean:用于将对象存入Spring的ioc容器中,在使用过程中@Bean是将当前方法的返回值放到容器中。

@RequestMapping:是一个用来处理请求地址映射的注解,可用于映射一个请求或一个方法,用在类或方法上。

@ResponseBody:是将Controller的方法返回的对象,通过适当的转换器转换为指定的格式后,写入到response对像的body区,通常返回JSON或者是xml数据。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值