spring(六)---aspectj aop编程(注解配置)
来源:互联网 发布:淘宝退货运费险怎么退 编辑:程序博客网 时间:2024/06/05 09:51
一、简单应用
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千万不能忘记!这个是最重要的一部!
如果有疑问或者对该博文有何看法或建议或有问题的,欢迎评论,恳请指正!
- spring(六)---aspectj aop编程(注解配置)
- Spring AOP实例(schema配置,aspectJ注解)
- Spring AOP Aspectj 注解配置
- Spring AOP与AspectJ 面向切面编程配置与注解
- (15)Spring AOP 注解 AspectJ
- spring(五)---aspectj aop编程(xml配置)
- SSH(1) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP
- Spring AOP使用配置介绍(四):基于@AspectJ注解的aop
- spring-aop---AspectJ注解式切面编程。
- Spring Aop实例之AspectJ注解配置
- Spring Aop实例之AspectJ注解配置
- spring的AOP(五)----Spring AOP 编程(AspectJ )
- AOP(AspectJ、Spring AOP)
- Spring——AOP(面向切面编程)@AspectJ注解方式
- 面向切面(AOP)之Spring接口方式 schema配置方式 aspectj注解方式
- AOP注解实现(AspectJ)
- Spring框架 AspectJ注解配置(四)
- Spring AOP(三)——通过@AspectJ注解切面
- python 核心编程 Chapter4 Exercise
- jQuery中的选择器
- Java中继承、final、抽象类
- 以人民的名义确认一下中企动力与客户的关系
- 欢迎使用CSDN-markdown编辑器
- spring(六)---aspectj aop编程(注解配置)
- Linux学习篇第二章~unit4
- MyBatis动态SQL语句
- Html--框架中画中画的用法
- [NLP论文阅读]LightRNN: Memory and Computation-Efficient Recurrent Neural Networks
- 用ajax验证用户名是否可以注册 POST方式
- meteor的安装的问题及unable to verify the first certificate
- 前端开发-移动端(3)- 响应式布局
- BloomFilter在DataDomain中的应用