Spring基础三—AOP
一.概述
AOP又称为面向切面编程,是对面向对象编程(OOP)的补充。一些常见的横切关注点如:日志记录,事务管理,数据验证等。在面向对象编程中,应用程序的模块化是通过类实现的,而在面向方面编程应用程序中,模块化由Aspects实现,并且它们被配置为跨越不同的类。Spring AOP从通过普通面向对象编程模型无法实现的类中获取横切任务的直接依赖性。例如,我们可以有一个单独的日志类,但功能类必须再次调用这些方法来实现跨应用程序的日志记录。
二.AOP的实现
1.引入AOP需要的包:
<dependencies>
<!-- Spring and Transactions -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<!-- Logging with SLF4J & LogBack -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.13</version>
<scope>runtime</scope>
</dependency>
<!-- AspectJ dependencies -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.7.4</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.7.4</version>
</dependency>
</dependencies>
2.编写HelloWorld接口
public interface HelloWorld {
public void helloWorld(String msg);
}
3.编写HelloWorld实现类
public class HelloWorldImpl implements HelloWorld{
public void helloWorld(String msg) {
System.out.println(msg);
}
}
public class HelloWorldImpl2 implements HelloWorld{
public void helloWorld(String msg) {
System.out.println(msg);
}
}
4.编写切面类
@Aspect//声明切面
public class AspectIntercepter {
@Pointcut(value="execution(* com.spring.HelloWorld.*(..))")
private void pointCut(){
}
@Around(value="pointCut()")
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("@Around开始");
Object object = pjp.proceed();
System.out.println(pjp.getThis()+"@Around结束!...");
return object;
}
@Before(value="pointCut()")
public void doAccessCheck(JoinPoint joinPoint){
System.out.println("@Before:"+Arrays.toString(joinPoint.getArgs()));
}
@AfterThrowing(value="pointCut()",throwing="ex")
public void doAfterThrow(JoinPoint joinPoint,Throwable ex){
System.out.println("@AfterThrowing异常信息:"+ex);
}
@After(value="pointCut()")
public void after(){
System.out.println("@After");
}
@AfterReturning(value="pointCut()")
public void doAfter(){
System.out.println("@AfterReturning");
}
}
5.编写配置信息
<?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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 使 AspectJ 的注解起作用 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<bean id="helloWorld1" class="com.spring.HelloWorldImpl"/>
<bean id="helloWorld2" class="com.spring.HelloWorldImpl2"/>
<bean id="myInterceptor" class="com.spring.AspectIntercepter"/>
</beans>
6.编写测试类
public class SpringAopTest {
private static ApplicationContext ctx;
public static void main(String[] args) {
ctx = new ClassPathXmlApplicationContext("spring.xml");
HelloWorld bean1 = (HelloWorld)ctx.getBean("helloWorld1");
bean1.helloWorld("helloWorld1");
HelloWorld bean2 = (HelloWorld)ctx.getBean("helloWorld2");
bean2.helloWorld("helloWorld2");
HelloWorld bean3 = new HelloWorldImpl();//通过new对象是不会触发aop的
bean3.helloWorld("bean3");
}
}
7.执行结果
@Around开始
@Before:[helloWorld1]
helloWorld1
com.spring.HelloWorldImpl@290d210d@Around结束!...
@After
@AfterReturning
@Around开始
@Before:[helloWorld2]
helloWorld2
com.spring.HelloWorldImpl2@1d76aeea@Around结束!...
@After
@AfterReturning
bean3