首先在spring配置文件中加上

xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

然后在配置文件中开启aop

 <aop:aspectj-autoproxy />
<context:component-scan base-package="com.test"></context:component-scan>

定义bean

package com.test;

import org.springframework.stereotype.Component;

@Component("SpringTest")
public class SpringTest{ public void PrintHello(String name){
System.out.println("Hello," + name);
} public void save(String name){
System.out.println("我是save()方法...");
} public void update(String name){
System.out.println("我是update()方法...");
} public void search(String search){
System.out.println("我是search()方法...");
} }

然后定义切面

package com.test;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; /**
* 定义切面
* @author John
*
*/
@Aspect
@Component("Aspject")
public class Aspject { //定义切点
@Pointcut("execution(* com.test.SpringTest.update(..))")
private void anyMethod(){} //定义前置通知
@Before("anyMethod() && args(name)")
public void BeforeTongZhi(String name){
System.out.println("我是前置通知.....");
} @After("anyMethod()")
public void after(){
System.out.println("最终通知");
} }

测试类

package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) {
ApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext.xml");
SpringTest test = (SpringTest) cxt.getBean("SpringTest");
test.update("zhangsan");
}
}

运行结果

我是前置通知.....
我是update()方法...
最终通知

期间遇到一个错误

error at ::0 can't find referenced pointcut anyMethod

是aspectjrt jar包的版本太低了。

Spring 注解式Aop 入门的更多相关文章

  1. Spring注解式AOP面向切面编程.

    1.AOP指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式.aop底层是动态代理. package com.bie.config; import org.aspectj.lan ...

  2. Spring注解式事务解析

    #Spring注解式事务解析 增加一个Advisor 首先往Spring容器新增一个Advisor,BeanFactoryTransactionAttributeSourceAdvisor,它包含了T ...

  3. spring注解式参数校验

    很痛苦遇到大量的参数进行校验,在业务中还要抛出异常或者返回异常时的校验信息,在代码中相当冗长,今天我们就来学习spring注解式参数校验. 其实就是:hibernate的validator. 开始啦. ...

  4. Spring Boot数据访问之动态数据源切换之使用注解式AOP优化

    在Spring Boot数据访问之多数据源配置及数据源动态切换 - 池塘里洗澡的鸭子 - 博客园 (cnblogs.com)中详述了如何配置多数据源及多数据源之间的动态切换.但是需要读数据库的地方,就 ...

  5. spring注解开发-AOP(含原理)

    一:AOP基本使用 AOP指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式: 步骤一:导入aop模块:Spring AOP:(spring-aspects) <depen ...

  6. Spring注解(AOP)

    底层动态代理 程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式 导入aop的相关模块 <!-- https://mvnrepository.com/artifact/org. ...

  7. spring注解、aop(二)

    使用注解配置spring 1.导入 spring-aop-5.0.6.RELEASE.jar包 2.为主配置文件引入新的命名空间 xmlns:context="http://www.spri ...

  8. Spring 3.0 Aop 入门

    关于Aop的原理,动态代理,反射,之类的底层java技术网上搜一堆一堆的..我就不多说了,主要说在spring上使用aop的方法. 首先不得不说一下的就是,spring aop的支持需要外部依赖包: ...

  9. Spring注解式与配置文件式

    http://tom-seed.iteye.com/blog/1584632 Spring注解方式bean容器管理 1.通过在配置文件中配置spring组件注入 <context:compone ...

随机推荐

  1. Date的使用

    方法 说明 Date() 返回当日的日期和时间 getDate() 获取当天(1-31) getDay() 获取当天的星期(0-6) getMonth() 获取月份(0-11) getFullYear ...

  2. 你的flume-ng的第一篇博客

    我在flume-ng 1.1.0 孵化版的时候就开始接触了,自己也搞了一段时间,没事扯扯心得吧. 先说在前面,flume-ng 后面的版本,我没仔细读change log ,比较农民 , 不知道新添了 ...

  3. 快速创建InfoPath表单

    快速创建InfoPath表单 2010年已经过去了一半了,这时候再说初识InfoPath可能会被很多人笑话,但是又有多少人真正认识InfoPath呢?无论你是刚刚 听说这个东西还是它的老相好都请同我一 ...

  4. 包图Package

    [UML]UML系列——包图Package 系列文章 [UML]UML系列——用例图Use Case       [UML]UML系列——用例图中的各种关系(include.extend)       ...

  5. C#程序调用cmd.exe执行命令

    代码部分 using System.Diagnostics; public class CmdHelper { private static string CmdPath = @"C:\Wi ...

  6. jQuery实现鼠标移上弹出提示框,移出消失

    <TD>里有一行数据 "那片笑声让我想起......"  假设超出规定长度将用......代替, 而现在要通过鼠标移动到......上 显示全部内容,移出则消失.如下图 ...

  7. HashMap源码剖析

    HashMap源码剖析 无论是在平时的练习还是项目当中,HashMap用的是非常的广,真可谓无处不在.平时用的时候只知道HashMap是用来存储键值对的,却不知道它的底层是如何实现的. 一.HashM ...

  8. MongoDB的一些用法(转藏)

    MongoDB是目前工作中经常使用到的NoSQL数据库. 本博客只记录相关理论知识和技巧,涉及到实践的部分都会单开Blog来记录实践过程. ------------------------------ ...

  9. JavaEE:Servlet简介及ServletConfig、ServletContext

    Servlet简介 1.Servlet是sun公司提供的一门用于开发动态web资源的技术*静态web资源:固定数据文件*动态web资源:通过程序动态生成数据文件2.Servlet技术基于Request ...

  10. STM8S TIM4 初始化设置

    #define TIM4_DIV1 (unsigned char)0 #define TIM4_DIV2 (unsigned char)1 #define TIM4_DIV4 (unsigned ch ...