AspectJ实现AOP(xml配置方式)

来源:互联网 发布:淘宝app图标双11 编辑:程序博客网 时间:2024/06/06 01:40

AOP是一种编程思想,相对于OOP来说,AOP更关注于某一种功能广泛应用于多个功能,比如说事务管理、日志记录等。举个例子,假如我要在用户登录前记录日志,还要在用户进行某种操作前也记录日志,还要在...前记录日志,那么是不是要在这些功能的实现里面都要加上一个记录日志的操作呢?那么,假如记录日志的操作发生了某些变动,那么是不是每个实现都要进行改动?太麻烦了。

所以,为了解耦,降低编程的重复性,这个时候使用AOP就非常合适了。接下来实现一个非常简单的例子来实现一个AOP,在用户登录前和登录后都进行一些操作,这次使用AspectJ来实现(个人认为使用Spring自带的太麻烦了)。

因为只是为了实现一个简单的AOP,所以功能尽可能精简,从某种程度来讲,这也是一种解耦的思想。哈哈。

国际惯例,先上pom:

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>Demo</groupId>  <artifactId>SpringByAspectJ</artifactId>  <version>0.0.1-SNAPSHOT</version>  <packaging>jar</packaging>  <name>SpringByAspectJ</name>  <url>http://maven.apache.org</url>  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    <spring.version>4.3.4.RELEASE</spring.version>  </properties>  <dependencies>    <!-- https://mvnrepository.com/artifact/junit/junit --><dependency>    <groupId>junit</groupId>    <artifactId>junit</artifactId>    <version>4.12</version>    <scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-context --><dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-context</artifactId>    <version>${spring.version}</version></dependency><dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-core</artifactId>    <version>${spring.version}</version></dependency>    <dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-beans</artifactId>    <version>${spring.version}</version></dependency>    <dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-aop</artifactId>    <version>${spring.version}</version></dependency><dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-test</artifactId>    <version>${spring.version}</version></dependency><dependency>    <groupId>aopalliance</groupId>    <artifactId>aopalliance</artifactId>    <version>1.0</version></dependency>    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --><dependency>    <groupId>org.aspectj</groupId>    <artifactId>aspectjweaver</artifactId>    <version>1.8.11</version></dependency>  </dependencies></project>

注意,除了加上junit包和spring全家桶以外,还要加上apoalliance(aop联盟)和aspectjweaver这两个包。另外

版本的问题也需要注意下,有时候明明代码没问题,但是就是报错,那么可以尝试下更换个版本的jar包,可能就

OK了。

再上项目结构:

userService包下有2个类,一个是用户登录类Loign,还有一个是记录用户登录的类LoginService。LoginService

就是一个切面,横切所有需要用这个类里面的某种方法的点,那么这个点就是连接点。看代码吧:

SpringContext-Config.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"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:tx="http://www.springframework.org/schema/tx"    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context-4.3.xsd"><context:component-scan base-package="userService"></context:component-scan>            <aop:config>    <aop:aspect id="userLogin" ref="loginService">    <aop:pointcut expression="execution(* userService.Login.*(..))" id="allService"/>    <aop:before method="before" pointcut-ref="allService"/>    <aop:after method="after" pointcut-ref="allService"/>    </aop:aspect>    </aop:config>    </beans>
这个也很简单,首先开启包的自动扫描,然后进行AOP的配置。

接下来就是测试类了:

MyTestCase.java

package test;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import userService.Login;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:SpringContext-Config.xml")public class MyTestCase {@Autowired private ApplicationContext applicationContext;@Testpublic void testAop(){Login login = (Login)applicationContext.getBean("login");login.login("TOM");}}
看输出: