一、使用情形
如果需要在现有业务类的基础上进行日志收集、安全管理、事务管理、缓存优化等功能时,一般会使用Spring的AOP实现功能的增强。如下现有简单业务:
package com.giser.aop;
public interface Performance {
void perform();
}
package com.giser.aoptest;
import org.springframework.stereotype.Service;
import com.giser.aop.Performance;
@Service
public class GISPerformance implements Performance {
@Override
public void perform() {
System.out.println("gis performance");
}
}
现在需要在perform之前或之后添加一些行为,如就座、鼓掌等,就需要使用切面。
二、使用方式
1、声明切面类
package com.giser.aop;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
/**
* 将POJO Audience使用@Aspect进行标注,表明此类为切面类
*/
@Aspect
public class Audience {
// 定义切点表达式
@Pointcut("execution(** com.giser.aop.Performance.*(..))")
public void pointcut() {}
// @Before("execution(** com.giser.aop.Performance.*(..))")
@Before("pointcut()") // 引用切点表达式
public void silenceCellPhones() {
System.out.println("Silencing cell phones");
}
// @Before("execution(** com.giser.aop.Performance.*(..))")
@Before("pointcut()")
public void takeSeats() {
System.out.println("takeSeats");
}
// @AfterReturning("execution(** com.giser.aop.Performance.*(..))")
@AfterReturning("pointcut()")
public void applause() {
System.out.println("applause");
}
// @AfterThrowing("execution(** com.giser.aop.Performance.*(..))")
@AfterThrowing("pointcut()")
public void demandRefund() {
System.out.println("demandRefund");
}
}
2、启用切面类
2.1 基于注解方式:
package com.giser.aop;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@EnableAspectJAutoProxy // 启用AspectJ自动代理,使切面类生效
public class ConcertConfig {
@Bean // 声明切面类的bean
public Audience audience() {
return new Audience();
}
}
测试:
package com.giser.aoptest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.giser.aop.ConcertConfig;
import com.giser.aop.Performance;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes= {ConcertConfig.class, GISPerformance.class})
public class SpringTest {
@Autowired
private Performance performance;
@Autowired
private ConcertConfig concertConfig;
@Test
public void testPerformance() {
performance.perform();
}
}
输出:
Silencing cell phones
takeSeats
gis performance
applause
2.2 基于xml
spring.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--
1、声明spring的aop命名空间
xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
-->
<!-- 配置扫描路径 -->
<context:component-scan base-package="com.giser" />
<!-- 声明Audience -->
<bean class="com.giser.aop.Audience" />
<!-- 启用AspectJ自动代理 -->
<aop:aspectj-autoproxy />
</beans>
测试:
package com.giser.aoptest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.giser.aop.Performance;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = {"classpath:spring.xml"})
public class SpringTest2 {
@Autowired
private Performance performance;
@Test
public void testPerformance() {
performance.perform();
}
}
输出:
Silencing cell phones
takeSeats
gis performance
applause
applause
三、pom配置
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring.version>4.3.9.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Springframework start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<!-- aop所需jar -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.5</version>
</dependency>
<!-- aop所需jar -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</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-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</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-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument-tomcat</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc-portlet</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</dependencyManagement>