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. Android - Button(按钮)的响应点击事件的4种写法

    Button控件setOnclickListener(View.OnClickListener listener)来接收一个点击事件的监听器 自定义一个点击事件监听器类让其实现View.OnClick ...

  2. JavaScript工具类(三):localStorage本地储存

    localStorage Web 存储 API 提供了 sessionStorage (会话存储) 和 localStorage(本地存储)两个存储对象来对网页的数据进行添加.删除.修改.查询操作. ...

  3. pandas filter数据筛选

    https://study.163.com/course/courseMain.htm?courseId=1006383008&share=2&shareId=400000000398 ...

  4. http使用formData方式传输文件请求

    转载请注明出处: 项目中有遇到http使用formData请求传输文件,在此记录一下 1.依赖jar包: <dependency> <groupId>org.apache.ht ...

  5. Python实现PIL将图片转成字符串

    # -*- coding: utf-8 -*- # author:baoshan from PIL import Image, ImageFilter codeLib = '''@#$%&?* ...

  6. (转)Python3的四舍五入round()函数坑爹?不,更科学!

    原文:https://blog.csdn.net/lly1122334/article/details/80596026 Python3的四舍五入round()函数坑爹?不,更科学!Python2中, ...

  7. windows下用navicat远程链接虚拟机Linux下MySQL数据库(转)

    原文地址:https://www.cnblogs.com/blogforly/p/5997553.html 今天想用navicat远程连接虚拟机中的MySQL数据库,一直连不上,在网上搜索了一下,发现 ...

  8. 全基因组关联分析(GWAS)的计算原理

    前言 关于全基因组关联分析(GWAS)原理的资料,网上有很多. 这也是我写了这么多GWAS的软件教程,却从来没有写过GWAS计算原理的原因. 恰巧之前微博上某位小可爱提问能否写一下GWAS的计算原理. ...

  9. jwplayer :若请求不到流,则页面一直转圈请求效果

    思路: 利用jwplayer onPlay(播放) .onError(出错)事件. 页面:背景图为黑色,嵌入一张背景为黑色的 git 动态图,加载页面时隐藏. 流程:若进入到onPlay 方法,则说明 ...

  10. VisualStudio开发UE4工程设置

    转自:http://wangjie.rocks/2016/06/24/ue4-vs-setup/ 推荐插件 Visual Assist X C++ 代码高亮 UnrealVS Extension UE ...