springAOP(基于注解开发)

五.springAOP(基于注解开发)
1.导包和springAOP XML开发一样
2.前置通知,后置通知示例:
eg:
applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 配置扫描注解的位置 -->
    <context:component-scan base-package="com.gyf"></context:component-scan>
    <!-- 确定AOP注解生效 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

    <!-- aop切面配置 -->
    <aop:config>
        <!-- 引入切面类 -->
        <aop:aspect ref="myAspect"></aop:aspect>
    </aop:config>
</beans>

切面类MyAspect

// @Aspect注解告诉spring这是一个切面类
@Component("myAspect")
@Aspect
public class MyAspect {

    // 声明公共切点
    @Pointcut("execution(* com.gyf.service.*.*(..))")
    public void myPointcut(){

    }

    // 单独编写切点
    // @Before("execution(* com.gyf.service.*.*(..))")
    // 引用公共切点
    @Before("myPointcut()")
    public void myBefore(JoinPoint joinPoint){
        System.out.println("前置通知..." + joinPoint.getSignature().getName());
    }

    // 单独编写切点
    // @AfterReturning(pointcut = "execution(* com.gyf.service.*.*(..))", returning = "retValue")
    // 引用公共切点
    @AfterReturning(pointcut = "myPointcut()", returning = "retValue")
    public void myAfterReturning(JoinPoint joinPoint, Object retValue){
        System.out.println("后置通知..." + "=== 方法名为" + joinPoint.getSignature().getName() + "=== 返回值为" + retValue);
    }
}

UserServiceImpl

@Service("userService")
public class UserServiceImpl implements UserService {

    @Resource(name="userDao")
    private UserDao userDao;

    @Override
    public boolean add() {
        System.out.println("service 添加用户");
        return userDao.add();
    }
}

UserDaoImpl

@Repository("userDao")
public class UserDaoImpl implements UserDao {

    @Override
    public boolean add() {
        System.out.println("dao 添加用户");
        return true;
    }
}

测试类

@Test
public void test5(){
    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserService userService = (UserService) ac.getBean("userService");
    userService.add();
}
输出结果:
前置通知...add
service 添加用户
dao 添加用户
后置通知...=== 方法名为add=== 返回值为true

3.环绕通知示例:
applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 配置扫描注解的位置 -->
    <context:component-scan base-package="com.gyf"></context:component-scan>
    <!-- 确定AOP注解生效 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

    <!-- aop切面配置 -->
    <aop:config>
        <!-- 引入切面类 -->
        <aop:aspect ref="myAspect"></aop:aspect>
    </aop:config>
</beans>

切面类MyAspect

// @Aspect注解告诉spring这是一个切面类
@Component("myAspect")
@Aspect
public class MyAspect {

    // 声明公共切点
    @Pointcut("execution(* com.gyf.service.*.*(..))")
    public void myPointcut(){
    
    }

    @Around("myPointcut()")
    public Object myAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕通知...开启事物");
        Object retObj = proceedingJoinPoint.proceed();
        System.out.println("环绕通知...提交事物");
        return retObj;
    }
}

UserServiceImpl

@Service("userService")
public class UserServiceImpl implements UserService {

    @Resource(name="userDao")
    private UserDao userDao;

    @Override
    public boolean add() {
        System.out.println("service 添加用户");
        return userDao.add();
    }
}

UserDaoImpl

@Repository("userDao")
public class UserDaoImpl implements UserDao {

    @Override
    public boolean add() {
        System.out.println("dao 添加用户");
        return true;
    }
}

测试类

@Test
public void test5(){
    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserService userService = (UserService) ac.getBean("userService");
    boolean ret = userService.add();
    System.out.println(ret);
}

4.异常通知,最终通知示例:
applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 配置扫描注解的位置 -->
    <context:component-scan base-package="com.gyf"></context:component-scan>
    <!-- 确定AOP注解生效 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

    <!-- aop切面配置 -->
    <aop:config>
        <!-- 引入切面类 -->
        <aop:aspect ref="myAspect"></aop:aspect>
    </aop:config>
</beans>

MyAspect

// @Aspect注解告诉spring这是一个切面类
@Component("myAspect")
@Aspect
public class MyAspect {

    // 声明公共切点
    @Pointcut("execution(* com.gyf.service.*.*(..))")
    public void myPointcut(){

    }

    @AfterThrowing(pointcut="myPointcut()", throwing="e")
    public void myAfterThrowing(JoinPoint joinPoint, Throwable e){
        System.out.println("异常通知..." + joinPoint.getSignature().getName() + "===" + e.getMessage());
    }

    @After("myPointcut()")
    public void a(JoinPoint joinPoint){
        System.out.println("最终通知..." + joinPoint.getSignature().getName());
    }
}

UserServiceImpl

@Service("userService")
public class UserServiceImpl implements UserService {

    @Resource(name="userDao")
    private UserDao userDao;

    @Override
    public boolean add() {
        System.out.println("service 添加用户");
        int i = 10/0;
        return userDao.add();
    }
}

UserDaoImpl

@Repository("userDao")
public class UserDaoImpl implements UserDao {

    @Override
    public boolean add() {
        System.out.println("dao 添加用户");
        return true;
    }
}

测试类

@Test
public void test5(){
    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserService userService = (UserService) ac.getBean("userService");
    boolean ret = userService.add();
    System.out.println(ret);
}

输出结果:
service 添加用户
最终通知...add
异常通知...add===/ by zero
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值