aop 思想: 横向重复 纵向抽取
连接点: 给事务提交的方法
通知 / 增强: 方法执行以前调的方法
目标对象: 被代理对象
先导包
新建 接口 Service 并封装
- package com.oracle.service;
- public interface UserService {
- public abstract void get();
- public abstract void update();
- public abstract void insert();
- public abstract void delete();
- }
新建个实现类 UserServiceImp
- package com.oracle.service;
- // 定义一个接口并实现
- public class UserServiceImp implements UserService{
- @Override
- public void get() {
- System.out.println("查询用户");
- }
- @Override
- public void update() {
- System.out.println("修改用户");
- }
- @Override
- public void insert() {
- System.out.println("新增用户");
- }
- @Override
- public void delete() {
- System.out.println("删除用户");
- }
- }
new 一个 通知类 advice , 定义为 MyAdvice
- package com.oracle.advice;
- import org.aspectj.lang.ProceedingJoinPoint;
- // 新建通知类
- public class MyAdvice {
- // 前置通知
- public void before(){
- System.out.println("这是前置通知");
- }
- // 后置通知
- public void afterReturning(){
- System.out.println("这是后置通知");
- }
- // 环绕通知 返回值是个 obj 类型
- public Object around(ProceedingJoinPoint pjp) throws Throwable{
- System.out.println("环绕通知之前调用");
- Object obj=pjp.proceed();
- System.out.println("环绕之后调用");
- return pjp;
- }
- // 异常通知
- public void afterException(){
- System.out.println("异常后调用");
- }
- // 后置通知 (不管是否异常都调用 )
- public void after(){
- System.out.println("后置通知目标方法执行后调用");
- }
- }
然后将它们联系起来 配置一下 applicationContext.xml
- <?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" xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
- <!-- 配置注解扫描 -->
- <context:component-scan base-package="com.oracle.pojo"></context:component-scan>
- <bean name="car2" class="com.oracle.pojo.Car"></bean>
- <!-- 配置目标对象 -->
- <bean name="userService" class="com.oracle.service.UserServiceImp"></bean>
- <!-- 配置通知对象 -->
- <bean name="myadvice" class="com.oracle.advice.MyAdvice"></bean>
- <aop:config>
- <!-- 配置切入点
- public void com.oracle.service.UserServiceImp.save()
- void com.oracle.service.UserServiceImp.save()
- * com.oracle.service.UserServiceImp.save()
- * com.oracle.service.UserServiceImp.*()
- * com.oracle.service.*ServiceImp.*(..)
- -->
- <aop:pointcut expression="execution(* com.oracle.service.*ServiceImp.*(..))" 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>
最后建个 demo 测试
- package com.oracle.test;
- 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.oracle.service.UserService;
- //@表示注解, 将约束注解到容器
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration("classpath:applicationContext.xml")
- public class Demo01 {
- @Autowired
- private UserService userService;
- @Test
- public void get(){
- userService.get();
- }
- }
运行结果
.
来源: http://www.bubuko.com/infodetail-3149340.html