AOP面向切面编程思想:横向重复代码,纵向抽取
动态代理:1.通过动态代理可以提现 aop思想
2.对目标对象中的方法进行增强
Spring Aop开发:Spring 封装了动态代理代码,不需要手动写代理代码,并且还封装了cglib代理
1、xml配置
1.1 导包
springsource.org.aopalliance-1.0.0.jar
springsource.org.apache.commons.logging-1.1.1.jar
springsource.org.apache.log4j-1.2.15.jar
springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
spring-aop-4.2.4.RELEASE.jar
spring-aspects-4.2.4.RELEASE.jar
spring-beans-4.2.4.RELEASE.jar
spring-context-4.2.4.RELEASE.jar
spring-core-4.2.4.RELEASE.jar
spring-expression-4.2.4.RELEASE.jar
spring-test-4.2.4.RELEASE.jar
1.2 准备目标对象
UserService接口
package com.hciot.service;
public interface UserService {
void save();
void delete();
void update();
void find();
}
UserSeviceImpl实现UseService接口
package com.hciot.service;
public class UserServiceImpl implements UserService {
@Override
public void save() {
System.out.println("保存用户");
}
@Override
public void delete() {
System.out.println("删除用户");
}
@Override
public void update() {
System.out.println("更新用户");
}
@Override
public void find() {
System.out.println("查找用户");
}
}
1.3 准备通知
通知类型:
a.前置通知—目标方法运行之前调用
b.后置通知(出现异常不会调用)—目标方法运行之后调用
c.环绕通知—目标方法运行之前和运行之后都调用
d. 异常拦截通知—如果出现异常就会调用
e.后置通知(无论是否出现异常都会调用)—目标方法运行之后调用
package com.hciot.d_springaop;
import org.aspectj.lang.ProceedingJoinPoint;
public class MyAdvice {
public void before(){
System.out.println("这是前置通知");
}
public void afterReturning(){
System.out.println("这是后置通知(出现异常不会调用)");
}
public Object around(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("这是环绕通知之前部分");
Object proceed = pjp.proceed();
System.out.println("这是环绕通知之后部分");
return proceed;
}
public void afterException(){
System.out.println("出现异常");
}
public void after(){
System.out.println("这是后置通知,出现异常也会调用");
}
}
1.4 配置进行织入,将通知织入目标对象中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">
<!--准备工作:导入aop命名空间 -->
<!-- 1.配置目标对象 -->
<bean name="userService" class="com.hciot.service.UserServiceImpl"></bean>
<!-- 2.配置通知对象 -->
<bean name="myAdvice" class="com.hciot.d_springaop.MyAdvice"></bean>
<!-- 3.配置将通知织入目标对象 -->
<aop:config>
<!-- 1.配置切入点
public void com.hciot.service.UserServiceImp.save()
void com.hciot.service.UserServiceImp.save()
* com.hciot.service.UserServiceImp.save()
* com.hciot.service.UserServiceImp.*()
* com.hciot.service.UserServiceImp.*(..)
* com.hciot.service.*ServiceImp.*(..)
-->
<aop:pointcut expression="execution(* com.hciot.service.*ServiceImpl.*(..))" id="pc"/>
<aop:aspect ref="myAdvice">
<!-- 指定名为before方法作为前置 通知-->
<aop:before method="before" pointcut-ref="pc"/>
<!-- 后置 -->
<aop:after-returning method="afterReturning" pointcut-ref="pc"/>
<!-- 环绕 通知-->
<aop:around method="around" pointcut-ref="pc"/>
<!-- 异常拦截通知-->
<aop:after-throwing method="afterException" pointcut-ref="pc"/>
<!-- 后置-->
<aop:after method="after" pointcut-ref="pc"/>
</aop:aspect>
</aop:config>
</beans>
1.5 测试代码
package com.hciot.d_springaop;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.hciot.service.UserService;
@RunWith(SpringJUnit4ClassRunner.class) //帮我们创建容器
@ContextConfiguration("classpath:com/hciot/d_springaop/applicationContext.xml")
public class Demo {
@Resource(name="userService")
private UserService us;
@Test
public void fun(){
us.save();
}
}
2、注解配置package com.hciot.e_annotationaop;
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;
@Aspect //表示该类是通知类
public class MyAdvice {
@Pointcut("execution(* com.hciot.service.*ServiceImpl.*(..))")
public void pc(){}
//指定该方法是前置通知,并指定切入点
@Before("MyAdvice.pc()")
public void before(){
System.out.println("这是前置通知");
}
@AfterReturning("MyAdvice.pc()")
public void afterReturning(){
System.out.println("这是后置通知(出现异常不会调用)");
}
@Around("MyAdvice.pc()")
public Object around(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("这是环绕通知之前部分");
Object proceed = pjp.proceed();
System.out.println("这是环绕通知之后部分");
return proceed;
}
@AfterThrowing("MyAdvice.pc()")
public void afterException(){
System.out.println("出现异常");
}
@After("MyAdvice.pc()")
public void after(){
System.out.println("这是后置通知,出现异常也会调用");
}
}