AOP面向切面编程
首先新建一个maven项目,在项目的pom.xml中添加spring aop相关的依赖项:
如下是完整的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>cn.outofmemory</groupId>
<artifactId>spring-aop-aspect</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-aop-aspect</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>3.1.1.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</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-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.12</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.12</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
</project>
在maven中我们引入了spring aop相关的依赖,aspectj相关的包有两个
下面我们看Aspect类:
package arch.pub.aop;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import arch.pub.util.UtilOut;
@Component
@Aspect
public class SimpleAspect {
private static final Logger logger = Logger.getLogger(SimpleAspect.class.getName());
@Pointcut("execution(* arch.demo.controller.*Ctrl*.*(..))")
public void pointCut() {
}
@After("pointCut()")
public void after(JoinPoint joinPoint) {
logger.error("after aspect executed");
}
@Before("pointCut()")
public void before(JoinPoint joinPoint) {
//如果需要这里可以取出参数进行处理
//Object[] args = joinPoint.getArgs();
logger.error("before aspect executing");
}
@AfterReturning(pointcut = "pointCut()", returning = "returnVal")
public void afterReturning(JoinPoint joinPoint, Object returnVal) {
logger.error("afterReturning executed, return result is "
+ returnVal);
}
@Around("pointCut()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
logger.error("around start..");
Object object = null;
try {
object = pjp.proceed();
} catch (Throwable ex) {
logger.error("ex:------------------------------------");
logger.error(ex.toString(), ex);
logger.error("ex:====================================");
object = new String("ex"); //可修改返回结果
}
logger.error("around end");
return object;
}
@AfterThrowing(pointcut = "pointCut()", throwing = "error")
public void afterThrowing(JoinPoint jp, Throwable error) {
logger.error("error:------------------------------------");
logger.error(error.toString(), error);
logger.error("error:====================================");
}
}
SimpleAspect类中的第一个方法是pointCut()方法,此方法无任何执行内容,只有一个@Pointcut的注解,其他方法都会引用这个注解中指定的pointcut表达式。
其他几个方法是各种Advice类型的方法实现,在这些方法中都可以通过JoinPoint实例来获得方法执行的上下文信息,参数信息。需要注意AfterReturning和AfterThrowing,After三种不同Advice的执行顺序。
有了spring框架和aspectj以及cglib的支持,我们只需要实现上面两个类就可以使用spring aop的功能了,下面我们看下如何在spring的配置文件中配置spring aop。
<?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"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<aop:aspectj-autoproxy />
<context:component-scan base-package="cn.outofmemory" />
</beans>
package cn.outofmemory.spring_aop_aspect;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
ApplicationContext appContext = new ClassPathXmlApplicationContext("/appContext.xml");
PersonService personService = appContext.getBean(PersonService.class);
String personName = "Jim";
personService.addPerson(personName);
personService.deletePerson(personName);
personService.editPerson(personName);
((ClassPathXmlApplicationContext)appContext).close();
}
}
浙公网安备 33010602011771号