实现步骤:

  1:导入类扫描的注解解析器
    命名空间:xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-2.5.xsd"

    xml配置文件如下配置:<context:component-scan base-package="com.lxk.spring.aop.annotation"/>

  2:导入springAOP的注解解析器
    命名空间:xmlns:aop="http://www.springframework.org/schema/aop"
      xsi:schemaLocation="http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"

    xml配置文件如下配置:<aop:aspectj-autoproxy/>          //开启aop注释自动代理

  3:首先定义我们的类,实现项目中的某个功能。 

 import org.springframework.stereotype.Component;

 @Component("aopFun")                  //这里将此类加载到bean里面,这里是使用的注解形式,也可以通过spring.xml文件中配置<bean id="aopFun" class="com.zs.aop"></bean>进和依赖注入
public class AopFun{
public void printfStr(){
System.out.println("执行切点方法");
}
}

  4:配置切面类,也就是配置在什么情况下我们去执行切点:

 /**
* 注解方式声明aop
* 1.用@Aspect注解将类声明为切面(如果用@Component("")注解注释为一个bean对象,那么就要在spring配置文件中开启注解扫描,<context:component-scan base-package="com.cjh.aop2"/>
* 否则要在spring配置文件中声明一个bean对象)
* 2.在切面需要实现相应方法的前面加上相应的注释,也就是通知类型。
* 3.此处有环绕通知,环绕通知方法一定要有ProceedingJoinPoint类型的参数传入,然后执行对应的proceed()方法,环绕才能实现。
*/
@Component("aopTest")
@Aspect
public class AopTest{
//定义切点
@Pointcut("execution(* *.printfStr(..))")
public void printfString(){}
/**
* 前置通知(注解中的printfString()方法,其实就是上面定义pointcut切点注解所修饰的方法名,那只是个代理对象,不需要写具体方法,
* 相当于xml声明切面的id名,如下,相当于id="embark",用于供其他通知类型引用)
* <aop:config>
<aop:aspect ref="mistrel">
<!-- 定义切点 -->
<aop:pointcut expression="execution(* *.printfStr(..))" id="embark"/>
<!-- 声明前置通知 (在切点方法被执行前调用) -->
<aop:before method="beforSay" pointcut-ref="embark"/>
<!-- 声明后置通知 (在切点方法被执行后调用) -->
<aop:after method="afterSay" pointcut-ref="embark"/>
</aop:aspect>
</aop:config>
*/
@Before("printfString()")            //before:在切点执行之前执行
public void sayHello(){
System.out.println("注解类型前置通知");
}
//后置通知
@After("printfString()")            //After:在切点执行之后执行
public void sayGoodbey(){
System.out.println("注解类型后置通知");
}
//环绕通知。注意要有ProceedingJoinPoint参数传入。
@Around("printfString()")
public void sayAround(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("注解类型环绕通知..环绕前");      
pjp.proceed();//执行方法
System.out.println("注解类型环绕通知..环绕后");
}
}

  5:spring.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/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 开启注解扫描 -->
<context:component-scan base-package="com.zs.aop"/>
<!-- 开启aop注解方式,此步骤s不能少,这样java类中的aop注解才会生效 -->
<aop:aspectj-autoproxy/>
</beans>

  6:测试类

 package com.cjh.aop2;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
*
* @author Caijh
* email:codecjh@163.com
* 2017年7月11日 下午6:27:06
*/
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("com/zs/aop/spring.xml");      //获取applicationContext对象
AopFun bean1= (AopFun) ac.getBean("aopFun");          //通过spring的bean对象获取AopFun对象,并对对象进行依赖注入
bean1.printfStr();                          //执行功能方法
}
}

  7:最后打印的结果为

    注解类型环绕通知..环绕前
    注解类型前置通知
    执行切点方法
    注解类型环绕通知..环绕后
    注解类型后置通知

通知类型有:

  1:@Before:通知这个方法会在功能之前执行

  2:@After:通知这个方法会在功能之后执行

  3:@AfterThrow:通知这个方法会在功能抛出异常时执行

  4:@AfterReturning:通知这个方法会在功能return后执行

  5:@Around:通知这个方法会将功能封装起来

在最后也简单说一下通过spring.xml文件配置aop

     <!-- 使用xml配置aop -->
<!-- 强制使用cglib代理,如果不设置,将默认使用jdk的代理,但是jdk的代理是基于接口的 -->
<aop:config proxy-target-class="true" />
<aop:config>
   <!--定义切面-->
  <aop:aspect id="logAspect" ref="logInterceptor">
  <!-- 定义切入点 (配置的的有printfStr方法在调用之前都会被拦截)-->
<aop:pointcut expression="execution(execution(* *.printfStr(..)))" id="logPointCut"/>
9    <!--方法执行之前被调用执行的-->
   <aop:before method="before" pointcut-ref="logPointCut"/><!--一个切入点的引用-->
   <aop:after method="after" pointcut-ref="logPointCut"/><!--一个切入点的引用-->
   </aop:aspect>
  </aop:config>

Spring AOP注解形式简单实现的更多相关文章

  1. Spring AOP注解为什么失效?90%Java程序员不知道

    使用Spring Aop注解的时候,如@Transactional, @Cacheable等注解一般需要在类方法第一个入口的地方加,不然不会生效. 如下面几种场景 1.Controller直接调用Se ...

  2. Spring Boot -- Spring AOP原理及简单实现

    一.AOP基本概念 什么是AOP,AOP英语全名就是Aspect oriented programming,字面意思就是面向切面编程.面向切面的编程是对面向对象编程的补充,面向对象的编程核心模块是类, ...

  3. spring的注解形式:@Repository、@Service、@Controller,

    Spring的注解形式:@Repository.@Service.@Controller,它们分别对应存储层Bean,业务层Bean,和展示层Bean. @Repository.@Service.@C ...

  4. Spring aop注解失效

    问题 在spring 中使用 @Transactional . @Cacheable 或 自定义 AOP 注解时,对象内部方法中调用该对象的其他使用aop机制的方法会失效. @Transactiona ...

  5. Spring AOP Aspect的简单实现(基于注解)

    第1步:声明使用注解 <!-- 配置扫描注解--> 扫描包的位置<context:component-scan base-package="com.zz"/> ...

  6. Spring AOP就是这么简单啦

    前言 只有光头才能变强 上一篇已经讲解了Spring IOC知识点一网打尽!,这篇主要是讲解Spring的AOP模块~ 之前我已经写过一篇关于AOP的文章了,那篇把比较重要的知识点都讲解过了一篇啦:S ...

  7. Spring AOP 注解和xml实现 --转载

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

  8. 【学习笔记】Spring AOP注解使用总结

    Spring AOP基本概念 是一种动态编译期增强性AOP的实现 与IOC进行整合,不是全面的切面框架 与动态代理相辅相成 有两种实现:基于jdk动态代理.cglib Spring AOP与Aspec ...

  9. 死磕Spring之AOP篇 - Spring AOP注解驱动与XML配置

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

随机推荐

  1. shell学习(9)- du和df区别及详解

    清明小长假来加班,总得干点啥吧,今天就说说du 和df的区别. 1.区别 du,disk usage,是通过搜索文件来计算每个文件的大小然后累加,du能看到的文件只是一些当前存在的,没有删除的.他计算 ...

  2. C# 数据库连接字符串拼接

    string connectionString = string.Format(@"Data Source={0};User ID={1};Password={2};Initial Cata ...

  3. Codeforces 1105D(双层广搜)

    要点 题意:可以拐弯,即哈密顿距离 注意不可以直接一个一个搜,这过程中会把下一轮的标记上,导致同一轮的其它点没能正常完成应有的搜索 因此采用双层广搜,把同一轮先都出队列再的一起搜 #include & ...

  4. jQuery基础(2)

    jQuery的属性操作,使用jQuery操作input的value值,jQuery的文档操作 零.昨日内容回顾 jQuery 宗旨:write less do more 就是js的库,它是javasc ...

  5. PHP全国省市区地址分割提取脚本程序

    github地址: https://github.com/zmxfree/addressapart 比如将 浙江省杭州市江干区XX路X号 分割成 浙江省 杭州市 江干区 XX路X号,方便excel操作 ...

  6. MyIfmHttpClient

    package com.yd.ifm.client.caller.util.http; import java.util.Map; import com.yd.ifm.client.caller.mo ...

  7. MapReduce基本流程与设计思想初步

    1.MapReduce是什么? MapReduce是一种编程模型,用于大规模数据集的并行运算.它借用了函数式的编程概念,是Google发明的一种数据处理模型. 主要思想为:Map(映射)和Reduce ...

  8. Jsp动态生成表格

    输入行列: <body> <form action="Train2ResultJsp.jsp"> row:<input type="text ...

  9. js的加密和解密

    最近在研究js的加密和解密的问题,上网上搜出来很多方法,不过不知道到底哪一个会比较管用.这里是今天找到的一些关于base64加密解密的js代码,已经经过试验,可以使用,不过网上很多加密解密的工具,这种 ...

  10. 零基础逆向工程18_PE结构02_联合体_节表_PE加载过程

    联合体 特点 1.联合体的成员是共享内存空间的 2.联合体的内存空间大小是联合体成员中对内存空间大小要求最大的空间大小 3.联合体最多只有一个成员有效 节表数据结构说明 PE 加载 过程 FileBu ...