spring(六)---aspectj aop编程(注解配置)

一、简单应用

1、建工程,导包

2、写目标对象

package top.einino.service;

public interface PersonService {
void eat();
}

package top.einino.service.impl;

import org.springframework.stereotype.Service;

import top.einino.service.PersonService;

@Service("personServiceImpl")
public class PersonServiceImpl implements PersonService{

@Override
public void eat(){
System.out.println("吃饭!!");
//int i = 1/0;
}

}

3、写切面

package top.einino.aspect;

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.springframework.stereotype.Component;

@Aspect
@Component("personAspect")
public class PersonAspect {

//前置通知 参数为连接点对象,如果抛出异常,默认方法不能执行
@Before("execution(* top.einino.service.impl.PersonServiceImpl.*(..))")
public void before(JoinPoint joinPoint){
//连接点信息
System.out.println(joinPoint.toLongString());
System.out.println("前置通知:买菜");
}

//后置通知,第一个参数是连接点对象,第二个参数是方法返回值,参数名必须与配置文件的名字一样
@AfterReturning(value="execution(* top.einino.service.impl.PersonServiceImpl.*(..))",returning="result")
public void afterReturning(JoinPoint joinPoint, Object result){
System.out.println("后置通知:目标返回值"+result);
}

//环绕通知,参数为可执行的连接点对象,可以实现任何通知效果
@Around("execution(* top.einino.service.impl.PersonServiceImpl.*(..))")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
System.out.println("环绕通知执行前:做饭");
Object result = proceedingJoinPoint.proceed();
System.out.println("环绕通知执行后:清洗");
}

//异常通知,第一个参数为连接点对象,第二个参数为异常对象
@AfterThrowing(value="execution(* top.einino.service.impl.PersonServiceImpl.*(..))",throwing="e")
public void afterThrowing(JoinPoint joinPoint, Exception e){
//得到方法名
String methodName = joinPoint.getSignature().getName();
System.out.println(methodName+"方法发生异常"+e);
}

//最终final通知,无论该方法是否出现异常,该方法总是会执行,被运用于释放资源
@After("execution(* top.einino.service.impl.PersonServiceImpl.*(..))")
public void after(JoinPoint joinPoint){
System.out.println("结束整个流程");
}

}

4、写配置文件

<?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/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">

<!-- bean实例化,目标对象和切面 -->
<context:component-scan base-package="top.einino"></context:component-scan>
<!-- 启动AspectJ 注解配置自动代理 -->
<aop:aspectj-autoproxy/>

</beans>

5、测试

package top.einino.junit;

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 top.einino.service.PersonService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class TestPersonAspect {

@Autowired
PersonService personService;

@Test
public void testEat(){
personService.eat();
}
}

二、总结

本博文主要在于aspectj aop的注解配置实现的简单应用,需要注意的一点是注解@Aspect千万不能忘记!这个是最重要的一部!

如果有疑问或者对该博文有何看法或建议或有问题的,欢迎评论,恳请指正!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值