一、实体类:

Role

  1. public class Role {
  2. private int id;
  3. private String roleName;
  4. private String note;
  5.  
  6. @Override
  7. public String toString() {
  8. return "Role{" +
  9. "id=" + id +
  10. ", roleName='" + roleName + '\'' +
  11. ", note='" + note + '\'' +
  12. '}';
  13. }
  14.  
  15. public Role() {
  16. }
  17.  
  18. public Role(int id, String roleName, String note) {
  19. this.id = id;
  20. this.roleName = roleName;
  21. this.note = note;
  22. }
  23.  
  24. public int getId() {
  25. return id;
  26. }
  27.  
  28. public void setId(int id) {
  29. this.id = id;
  30. }
  31.  
  32. public String getRoleName() {
  33. return roleName;
  34. }
  35.  
  36. public void setRoleName(String roleName) {
  37. this.roleName = roleName;
  38. }
  39.  
  40. public String getNote() {
  41. return note;
  42. }
  43.  
  44. public void setNote(String note) {
  45. this.note = note;
  46. }
  47. }

二、连接点(join point)

1、接口:RoleService

  1. public interface RoleService {
  2. void printRoleInfo(Role role);
  3. }

2、实现类:RoleServiceImp

  1. @Component
  2. public class RoleServiceImpl implements RoleService {
  3. @Override
  4. public void printRoleInfo(Role role) {
  5. System.out.println("id = "+role.getId()+", roleName = '"+role.getId()+"', note = '"+role.getNote()+"'");
  6. }
  7.  
  8. }

三、创建切面(Aspect)

1、方式一:

  1. @Aspect
  2. public class RoleAspect {
  3.  
  4. @Before("execution(* com.wbg.springAOP.aop.service.impl.RoleServiceImpl.printRoleInfo(..))")
  5. public void before() {
  6. System.out.println("进入方法before...");
  7. }
  8.  
  9. @After("execution(* com.wbg.springAOP.aop.service.impl.RoleServiceImpl.printRoleInfo(..))")
  10. public void after() {
  11. System.out.println("进入方法after...");
  12. }
  13. @AfterReturning("execution(* com.wbg.springAOP.aop.service.impl.RoleServiceImpl.printRoleInfo(..))")
  14. public void afterReturning() {
  15. System.out.println("进入方法afterReturning...");
  16. }
  17. @AfterThrowing("execution(* com.wbg.springAOP.aop.service.impl.RoleServiceImpl.printRoleInfo(..))")
  18. public void afterThrowing() {
  19. System.out.println("afterThrowing...");
  20. }
  21. }

2、方式二:切点(Pointcut)

使用注解:@Pointcut

  1. @Aspect
  2. public class RoleAspect {
  3. @Pointcut("execution(* com.wbg.springAOP.aop.service.impl.RoleServiceImpl.printRoleInfo(..))")
  4. public void print() {
  5.  
  6. }
  7.  
  8. @Before("print()")
  9. public void before() {
  10. System.out.println("进入方法before...");
  11. }
  12.  
  13. @After("print()")
  14. public void after() {
  15. System.out.println("进入方法after...");
  16. }
  17.  
  18. @AfterReturning("print()")
  19. public void afterReturning() {
  20. System.out.println("进入方法afterReturning...");
  21. }
  22.  
  23. @AfterThrowing("print()")
  24. public void afterThrowing() {
  25. System.out.println("afterThrowing...");
  26. }
  27. }

四:配置Spring bean

方式一:采用注解java配置

  1. @Configuration
  2. @EnableAspectJAutoProxy//自动代理
  3. @ComponentScan("com.wbg.springAOP.aop")
  4. public class AopConfig {
  5. @Bean
  6. public RoleAspect getRoleAspct(){
  7. return new RoleAspect();
  8. }
  9. }

方式二:使用XML配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
  5. <!--如同注解@EnableAspectJautoProxy-->
  6. <aop:aspectj-autoproxy/>
  7. <bean id="roleAspect" class="com.wbg.springAOP.aop.aspect.RoleAspect"/>
  8.  
  9. <bean id="roleService" class="com.wbg.springAOP.aop.service.impl.RoleServiceImpl"/>
  10. </beans>

五、测试:

  1. public static void main(String[] args) {
  2.  
  3. ApplicationContext ctx = new AnnotationConfigApplicationContext(AopConfig.class);
  4. //ApplicationContext ctx = new ClassPathXmlApplicationContext("aspectj.xml");
  5. RoleService roleService = ctx.getBean(RoleService.class);
  6. Role role = new Role(1,"123","55");
  7. roleService.printRoleInfo(role);
  8. System.out.println("--------------");
  9. //role=null;
  10. roleService.printRoleInfo(role);
  11. }

1、不为空:

2、为空:

六、加入环绕通知@Around

直接在切面加入:

  1. @Around("print()")
  2. public void around(ProceedingJoinPoint joinPoint){
  3. System.out.println("around======before============");
  4. try {
  5. joinPoint.proceed();
  6. } catch (Throwable throwable) {
  7. throwable.printStackTrace();
  8. }
  9. System.out.println("around======after============");
  10. }

测试:

七、给通知传递参数:&& args(..)

  1. @Before("execution(* com.wbg.springAOP.aop.service.impl.RoleServiceImpl.printRoleInfo(..)) && args(role, sort)")

八、引入(Introduction)

目的:让Role为空时不打印

1、定义接口:RoleVerifer

2、实现接口类:RoleVeriferImpl

3、把RoleVerifier加入到切面中

com.wbg.springAOP.aop.service.impl.RoleServiceImpl+ :表示对RoleServiceImpl类进行增强
defaultImpl :代码其默认实现类,这里是RoleVerifierImpl
  1.   @DeclareParents(value = "com.wbg.springAOP.aop.service.impl.RoleServiceImpl+",defaultImpl = RoleVerifierImpl.class)
  2. public RoleVerifier roleVerifier;

4、引入增强检查是否为空

demo:https://github.com/weibanggang/springaopstaticanddynamic

使用@AspectJ注解开发Spring AOP的更多相关文章

  1. spring 使用@AspectJ注解开发Spring AOP

    选择切点 Spring是方法级别的AOP框架,而我们主要也是以某个类的某个方法作为切点,用动态代理的理论来说,就是要拦截哪个方法织入对应AOP通知. 代码清单:打印角色接口 package com.s ...

  2. Spring Aop(二)——基于Aspectj注解的Spring Aop简单实现

    转发地址:https://www.iteye.com/blog/elim-2394762 2 基于Aspectj注解的Spring Aop简单实现 Spring Aop是基于Aop框架Aspectj实 ...

  3. 基于Aspectj 注解实现 spring AOP

    AOP 面向切面编程,是 OOP (面向对象编程)的补充 术语 横切关注点:方法中非主要业务逻辑部分 比如运算的模块:有验证参数.执行方法前的操作.执行方法.执行方法后的操作,验证参数.执行方法前后的 ...

  4. Spring学习之旅(八)Spring 基于AspectJ注解配置的AOP编程工作原理初探

    由小编的上篇博文可以一窥基于AspectJ注解配置的AOP编程实现. 本文一下未贴出的相关代码示例请关注小编的上篇博文<Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AO ...

  5. Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AOP编程比较

    本篇博文用一个稍复杂点的案例来对比一下基于XML配置与基于AspectJ注解配置的AOP编程的不同. 相关引入包等Spring  AOP编程准备,请参考小编的其他博文,这里不再赘述. 案例要求: 写一 ...

  6. 基于注解的Spring AOP的配置和使用

    摘要: 基于注解的Spring AOP的配置和使用 AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不 ...

  7. 基于注解的Spring AOP示例

    基于注解的Spring AOP示例 目录 在XML配置文件中开启 @AspectJ 支持 声明切面及切入点 声明通知 测试 结语 在XML配置文件中开启 @AspectJ 支持 要使用Spring的A ...

  8. 基于注解的Spring AOP的配置和使用--转载

    AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. ...

  9. Spring注解开发系列Ⅵ --- AOP&事务

    注解开发 --- AOP AOP称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等待,Struts2的拦截器设计就是基于AOP的思想,横向重复,纵向抽取.详细的AO ...

随机推荐

  1. Boxlayout中button改变大小

    需要先设置maximunsize neuStart.setBorder(BorderFactory.createRaisedBevelBorder()); neuStart.setMaximumSiz ...

  2. cocos creator Touch事件应用(触控选择多个子节点)

    最近参与了cocos creator的研究,开发小游戏,结果被一个事件坑得不行不行的.现在终于解决了,分享给大家. 原理 1.触控事件是针对节点的 2.触控事件的冒泡,是一级一级往上冒泡,中间可以阻止 ...

  3. Struts 2 框架相比Struts1的新特性

    POJO形式和POJO动作 - 已经摆脱了Struts2的动作表单的Struts框架的一个组成部分.Struts2可以使用任何的POJO接收的形式输入.同样的,你现在可以看到任何POJO的Action ...

  4. img图片加载失败默认图片

    <img :src="item.goods_pic" onerror="javascript:this.src='../static/images/default. ...

  5. CSS单行、多行文本溢出显示省略号(……)解决方案

    单行文本溢出显示省略号(-) text-overflow:ellipsis-----部分浏览器还需要加宽度width属性 .ellipsis{ overflow: hidden; text-overf ...

  6. 01.php面向对象

    下面是php_oop的一些基本知识 <?php //echo "<meta charset='utf-8'>" //设置中文输出 //1.面向对象类的建立: cl ...

  7. <Android Framework 之路>BootAnimation(2)

    前言 上一篇主要讲解了BootAnimation是从何而来,如何启动,从开机,到SurfaceFlinger服务起来,然后到执行开机动画,如果要深入的看里面的代码,是需要花一定的时间的,我们旨在了解大 ...

  8. ASP.NET 页面之间传递值的几种方式

    1.使用QueryString,  如....?id=1; response. Redirect().... 2.使用Session变量 3.使用Server.Transfer4.Applicatio ...

  9. 深入理解java的形参和实参

    转载声明:本文转载自公众号「码匠笔记」. 前几天在头条上看到一道经典面试题,引发了一些思考.也是写这篇文章的导火索. 背景 请看题: public    classMain{    publicsta ...

  10. pt-mysql-summary

    pt-mysql-summary主要用来输出MySQL的基本信息,可以作为数据库巡检以及刚开始熟悉数据库环境时候进行使用: [root@mxqmongodb2 bin]# ./pt-mysql-sum ...