public interface IHello {
public void sayHello(String str);
}
public class Hello implements IHello { @Override
public void sayHello(String str) {
// TODO Auto-generated method stub
System.out.println("你好"+str);
} }

  

aspectBean.java

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut; //引入Aspect注解,声明切面
@Aspect
public class aspectBean {
//定义为切入点
@Pointcut("execution(* hello.*(..))")
public void log() { }
@Before(value="log()") //在切入点之前执行
public void startLog()
{
System.out.println("开始记录");
}
@After(value="log()") //在切入点之后执行
public void endLog()
{
System.out.println("结束记录");
}
}

  applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> <!-- 启动aspectj -->
<aop:aspectj-autoproxy/>
<bean id="apbean" class="com.pb.aspectBean"></bean>
<bean id="he" class="com.pb.Hello"></bean>
</beans>

  Maintest文件:

public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); IHello hello=(IHello)context.getBean("he");
hello.sayHello("访客");
}

  执行:

Spring AOP 创建Advice 基于Annotation的更多相关文章

  1. Spring AOP 创建Advice 定义pointcut、advisor

    前面定义的advice都是直接植入到代理接口的执行之前和之后,或者在异常发生时,事实上,还可以对植入的时机定义的更细. Pointcut定义了advice的应用时机,在Spring中pointcutA ...

  2. Spring学习笔记之四----基于Annotation的Spring AOP编程

    你能使用@Aspect annotation将某个Java类标注为Aspect,这个Aspect类里的所有公有方法都可以成为一个Advice,Spring提供了5个Annotation去将某个方法标注 ...

  3. Spring AOP 介绍与基于接口的实现

    热烈推荐:超多IT资源,尽在798资源网 声明:转载文章,为防止丢失所以做此备份. 本文来自公众号:程序之心 原文地址:https://mp.weixin.qq.com/s/vo94gVyTss0LY ...

  4. Spring AOP 中 advice 的四种类型 before after throwing advice around

    spring  AOP(Aspect-oriented programming) 是用于切面编程,简单的来说:AOP相当于一个拦截器,去拦截一些处理,例如:当一个方法执行的时候,Spring 能够拦截 ...

  5. Spring AOP Example – Advice

    Spring AOP + AspectJ Using AspectJ is more flexible and powerful. Spring AOP (Aspect-oriented progra ...

  6. 阿里四面:你知道Spring AOP创建Proxy的过程吗?

    Spring在程序运行期,就能帮助我们把切面中的代码织入Bean的方法内,让开发者能无感知地在容器对象方法前后随心添加相应处理逻辑,所以AOP其实就是个代理模式. 但凡是代理,由于代码不可直接阅读,也 ...

  7. Spring AOP 创建切面

        增强被织入到目标类的所有方法中,但是如果需要有选择性的织入到目标类某些特定的方法中时,就需要使用切点进行目标连接点的定位.增强提供了连接点方位信息:如织入到方法前面.后面等,而切点进一步描述织 ...

  8. Spring AOP 创建增强类

    AOP联盟为增强定义了org.aopalliance.aop.Advice接口,Spring支持5种类型的增强:     1)前置增强:org.springframework.aop.BeforeAd ...

  9. Spring AOP创建Throwdvice实例

    1.异常发生的时候,通知某个服务对象做处理 2.实现throwsAdvice接口 接口实现: public interface IHello { public void sayHello(String ...

随机推荐

  1. 原创:logistic regression实战(一):SGD Without lasso

    logistic regression是分类算法中非常重要的算法,也是非常基础的算法.logistic regression从整体上考虑样本预测的精度,用判别学习模型的条件似然进行参数估计,假设样本遵 ...

  2. kings(骑士)解题报告

    kings(骑士) Time Limit5000 ms    Memory Limit131072 KBytes Description 用字符矩阵来表示一个8x8的棋盘,'.'表示是空格,'P'表示 ...

  3. eclipse juno 怎么安装maven

    步骤如下: 1.下载maven的bin,在apache官方网站可以下载. 2.下载下来之后,解压,找个路径放进去, 把bin的位置设在环境变量里,新建环境变量MAVEN_HOME. 3.在PATH里加 ...

  4. 用Visual Studio编写UDF的一点小技巧(自动补全宏函数、变量)

    下载Visual Studio,安装VS 下载番茄助手(Visual Assist X),链接:www.wholetomato.com,然后安装番茄助手 打开VS

  5. hdoj - 1864 最大报销额

    Problem Description 现有一笔经费可以报销一定额度的发票.允许报销的发票类型包括买图书(A类).文具(B类).差旅(C类),要求每张发票的总额不得超过1000元,每张发票上,单项物品 ...

  6. vscode插件开发之如何玩转vscode命令

    这里以插件开发为例,VsCode之所以那么强大是因为它背后有千千万万的开发者们为其开发大量功能插件,WordPress同理. 那么如何玩转VsCode命令呢(以插件开发为例)? 官方文档必不可少 ht ...

  7. python 操作redis,存取为字节格式,避免转码加

    这种情况连接数据库,对数据的存取都是字节类型,存取时还得转码一下 from redis import Redis # 实例化redis对象 rdb = Redis(host='localhost', ...

  8. LDAP -- ldap 的cn, ou, dc的含义

    1. LDAP的存储规则 区分名(DN,Distinguished Name) 和自然界中的树不同,文件系统/LDAP/电话号码簿目录的每一片枝叶都至少有一个独一无二的属性,这一属性可以帮助我们来区别 ...

  9. jdbc批量写入

    jdbc加了rewriteBatchedStatements=true就可以提升很多倍,

  10. 文件分片 浏览器文件大小限制 自定义请求头 在一个资源的加载进度停止之后被触发 arrayBuffer 异步 二进制数据缓存区

    js 整数限制 浏览器文件大小限制 https://w3c.github.io/FileAPI/#dom-blob-arraybuffer https://developer.mozilla.org/ ...