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");}}看输出:
阅读全文
0 0
- AspectJ实现AOP(xml配置方式)
- AspectJ对AOP的实现(xml)
- AspectJ实现AOP(注解方式)
- AspectJ注解方式实现AOP
- spring(五)---aspectj aop编程(xml配置)
- Spring AOP+AspectJ在XML配置实例
- 配置xml方式实现Spring的aop
- Spring通过@AspectJ方式实现AOP
- Spring学习-22:Spring的AOP:基于AspectJ的XML配置方式开发
- AOP注解实现(AspectJ)
- 采用AOP配置方式的AOP实现及XML解析
- AOP XML方式配置
- SSH(1) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP
- 面向切面(AOP)之Spring接口方式 schema配置方式 aspectj注解方式
- Spring AOP AspectJ注解和XML配置两种实现(Maven构建)
- Spring AOP 之AspectJ注解和XML配置两种实现(Maven构建)
- XML方式实现aop
- Spring AOP (三) XML配置方式
- vscode: Visual Studio Code 常用快捷键
- 实现容器的底层技术
- Linux下切换python2和python3
- C++ 结构体可以直接赋值
- Python基础4
- AspectJ实现AOP(xml配置方式)
- DruidDataSource的参考配置
- Ubuntu下安装软件提示软件包 apport 需要重新安装解决方法
- python3安装
- 指针运算、空指针、函数指针、staic与const的区别
- Codeforces Round #446 (Div. 2) A. Greed
- CORBA 学习之实现例子
- Maven 生产环境 测试环境 开发环境 打包配置
- iOS 自动化单元测试--XCTest