http://blog.csdn.net/yerenyuan_pku/article/details/52865330

首先在Eclipse中新建一个普通的Java Project,名称为springAOP。为了使用Spring的注解方式进行面向切面编程,需要在springAOP项目中加入与AOP相关的jar包,spring aop需要额外的jar包有:

  • com.springsource.org.aopalliance-1.0.0.jar
  • com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
  • spring-aop-4.2.5.RELEASE.jar
  • spring-aspects-4.2.5.RELEASE.jar

这样,springAOP项目共须jar包如下: 
 
要进行AOP编程,我们接着要在Spring的配置文件——beans.xml中引入aop命名空间:

<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" 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-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> </beans>

Spring提供了两种切面声明方式,实际工作中我们可以选用其中一种:

  • 基于XML配置方式声明切面。
  • 基于注解方式声明切面。

本文选用第二种方式进行面向切面编程,即基于Spring注解的方式声明切面。 
接下来我们在src目录下新建一个it.cast.service包,并在该包下创建PersonService接口,其代码为:

public interface PersonService {public void save(String name); public void update(String name, Integer id); public String getPersonName(Integer id); }
  • 1

紧接着在src目录下新建一个it.cast.service.impl包,并在该包下创建PersonService接口的实现类——PersonServiceBean.java,其代码为:

public class PersonServiceImpl implements PersonService { @Override public void save(String name) { System.out.println("我是save()方法"); } @Override public void update(String name, Integer id) { System.out.println("我是update()方法"); } @Override public String getPersonName(Integer id) { System.out.println("我是getPersonName()方法"); return "xxx"; } }
  • 1
  • 2

然后,我们就要在cn.itcast.service包下创建一个切面类——MyInterceptor.java,下面我们来按照以下步骤将其写出来。

  • 首先用@Aspect注解声明整个类是一个切面:

    @Aspect
    public class MyInterceptor {...
    }
  • 接着用@Pointcut注解声明一个切入点。

    @Aspect
    public class MyInterceptor { @Pointcut("execution (* cn.itcast.service.impl.PersonServiceImpl.*(..))") private void anyMethod() {} // 声明一个切入点,anyMethod为切入点名称 ... }

    我们可利用方法签名来编写切入点表达式。最典型的切入点表达式是根据方法的签名来匹配各种方法:

    • execution (* cn.itcast.service.impl.PersonServiceImpl.*(..)):匹配PersonServiceImpl类中声明的所有方法。第一个*代表任意修饰符及任意返回值类型,第二个*代表任意方法,..匹配任意数量任意类型的参数,若目标类与该切面在同一个包中,可以省略包名。
    • execution public * cn.itcast.service.impl.PersonServiceImpl.*(..):匹配PersonServiceImpl类中的所有公有方法。
    • execution public double cn.itcast.service.impl.PersonServiceImpl.*(..):匹配PersonServiceImpl类中返回值类型为double类型的所有公有方法。
    • execution public double cn.itcast.service.impl.PersonServiceImpl.*(double, ..):匹配PersonServiceImpl类中第一个参数为double类型,后面不管有无参数的所有公有方法,并且该方法的返回值类型为double类型。
    • execution public double cn.itcast.service.impl.PersonServiceImpl.*(double, double):匹配PersonServiceImpl类中参数类型为double,double类型的,并且返回值类型也为double类型的所有公有方法。
  • 然后声明前置通知方法。 
    前置通知方法在目标方法开始之前执行。

    @Aspect
    public class MyInterceptor { @Pointcut("execution (* cn.itcast.service.impl.PersonServiceImpl.*(..))") private void anyMethod() {} // 声明一个切入点,anyMethod为切入点名称 // 声明该方法是一个前置通知:在目标方法开始之前执行 @Before("anyMethod()") public void doAccessCheck() { System.out.println("前置通知"); } }
    • 1

    注意:若是将一个类声明为一个切面,那么需要把该类放到IOC容器管理

接下来,我们理应要修改Spring的配置文件——beans.xml了,将其改为:

<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" 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-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> <aop:aspectj-autoproxy /> <bean id="myInterceptor" class="cn.itcast.service.MyInterceptor" /> <bean id="personService" class=" cn.itcast.service.impl.PersonServiceImpl"></bean> </beans>
  • 1

从上面可看出我们并没有让Spring自动扫描和管理Bean。 
最后,在src目录下新建一个junit.test包,并在该包中新建一个单元测试类——SpringAOPTest.java,其代码为:

public class SpringAOPTest {@Testpublic void interceptorTest() { ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml"); PersonService personService = (PersonService) cxt.getBean("personService"); personService.save("xxx"); } }
  • 1

测试interceptorTest()方法,会发现Eclipse控制台打印: 

如要查看源码,可点击使用Spring的注解方式实现AOP入门进行下载。

转载于:https://www.cnblogs.com/telwanggs/p/6913354.html

(转)使用Spring的注解方式实现AOP入门相关推荐

  1. (转)使用Spring的注解方式实现AOP的细节

    http://blog.csdn.net/yerenyuan_pku/article/details/52879669 前面我们已经入门使用Spring的注解方式实现AOP了,现在我们再来学习使用Sp ...

  2. 第五章 Spring进阶-注解方式实现AOP(1)

    <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /> 徒弟:师傅,我 ...

  3. Spring5学习(七):注解方式进行AOP操作 及 多种通知类型的测试

    先来介绍一下AOP操作中的几个术语: 1.连接点:指类里面可以被增强的方法 2.切入点:指实际被增强的方法 3.通知:指实际增强的逻辑部分 4.切面:把通知应用到切入点的过程 Spring框架一般都是 ...

  4. Spring 使用注解方式进行事物管理

    Spring 使用注解方式进行事物管理 大家在使用spring的注解式事务管理时,对事务的传播行为和隔离级别可能有点不知所措,下边就详细的介绍下以备方便查阅. 事物注解方式: @Transaction ...

  5. springboot注解方式实现aop及常规方式

    本文介绍springboot实现aop的两种方式 首先需要引入对应依赖: <dependency><groupId>org.springframework.boot</g ...

  6. Spring 使用注解方式进行事务管理

    2019独角兽企业重金招聘Python工程师标准>>> 大家在使用spring的注解式事务管理时,对事务的传播行为和隔离级别可能有点不知所措,下边就详细的介绍下以备方便查阅. 事物注 ...

  7. spring定时注解方式定时写到xml里面融合

    把spring注解方式的定时写到xml里面,因为定时常常修改在class里面很不方便代码如下 在xlm <beans  里面加入 xmlns:task="http://www.spri ...

  8. spring学习--基于注解方式创建对象AOP

    概念 下面四个注解功能是一样的,都可以用来创建 bean 实例 ​ (1)@Component ​ (2)@Service ​ (3)@Controller ​ (4)@Repository 1.引入 ...

  9. Spring之注解方式实例化Java类

    我们知道一个<bean></bean>就代表一个对象,如果想创建多个对象,就要使用多个<bean></bean>,所以这里有个简便的方法: <co ...

最新文章

  1. 这些建议需要竞赛组委会酌情考虑
  2. 布线须知:机柜在数据中心机房的三个新用途
  3. 初识react中高阶组件
  4. Node.js 应用故障排查手册 —— 类死循环导致进程阻塞
  5. 联想微型计算机电脑黑屏怎么做系统,联想电脑黑屏怎么办,5种方法轻松排除黑屏故障...
  6. oracle19c监听服务启动失败,Oracle19c安装(有失败成功记录)
  7. c语言代码re通常什么错误,C语言,realloc动态内存申请,出现报错double free or corruption (!prev)...
  8. [leetcode] Power of Two 判断一个数是否是2的平方
  9. [数据结构] 图 ,邻接矩阵法,邻接表法
  10. 東京タワー初めてphoto
  11. iOS项目之wifi局域网传输文件到iPhone的简单实现
  12. 黑苹果快捷键修改_小米air13.3安装黑苹果教程
  13. 正大国际琪貨:为什么资深交易者更倾向于裸K?
  14. centos6.3安装bluefish
  15. linux c 开发 英文简历,软件工程师英文简历范文
  16. 《Gartner2016年度新兴技术成熟度曲线》全解读
  17. Gom传奇引擎的微端连不上的原因是什么?附:微端配置教程
  18. 计算机毕业设计Java后勤管理系统(源码+系统+mysql数据库+lw文档)
  19. Hive desc详解
  20. 【Lintcode】562. Backpack IV

热门文章

  1. (12)ISE14.7仿真流程(FPGA不积跬步101)
  2. (3)zynq FPGA AXI4_Stream总线介绍
  3. 下划线_Python中下划线的5种含义
  4. MQTT 连接 阿里云物联网十六进制数据分析笔记
  5. python程序设计实验报告实验程序流程序列化_Python程序设计_教学大纲_段震.doc
  6. MySQL:给表建立索引及索引的显示
  7. java程序n体问题_2n皇后问题 (Java代码)详解
  8. java 鸡尾酒排序_Java实现几种常见排序方法
  9. 如何给页面加上loding_excel打印区域页面设置
  10. php打印预览jquery,JS实现浏览器打印、打印预览示例