Spring AOP 创建Advice 基于Annotation

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的更多相关文章
- Spring AOP 创建Advice 定义pointcut、advisor
前面定义的advice都是直接植入到代理接口的执行之前和之后,或者在异常发生时,事实上,还可以对植入的时机定义的更细. Pointcut定义了advice的应用时机,在Spring中pointcutA ...
- Spring学习笔记之四----基于Annotation的Spring AOP编程
你能使用@Aspect annotation将某个Java类标注为Aspect,这个Aspect类里的所有公有方法都可以成为一个Advice,Spring提供了5个Annotation去将某个方法标注 ...
- Spring AOP 介绍与基于接口的实现
热烈推荐:超多IT资源,尽在798资源网 声明:转载文章,为防止丢失所以做此备份. 本文来自公众号:程序之心 原文地址:https://mp.weixin.qq.com/s/vo94gVyTss0LY ...
- Spring AOP 中 advice 的四种类型 before after throwing advice around
spring AOP(Aspect-oriented programming) 是用于切面编程,简单的来说:AOP相当于一个拦截器,去拦截一些处理,例如:当一个方法执行的时候,Spring 能够拦截 ...
- Spring AOP Example – Advice
Spring AOP + AspectJ Using AspectJ is more flexible and powerful. Spring AOP (Aspect-oriented progra ...
- 阿里四面:你知道Spring AOP创建Proxy的过程吗?
Spring在程序运行期,就能帮助我们把切面中的代码织入Bean的方法内,让开发者能无感知地在容器对象方法前后随心添加相应处理逻辑,所以AOP其实就是个代理模式. 但凡是代理,由于代码不可直接阅读,也 ...
- Spring AOP 创建切面
增强被织入到目标类的所有方法中,但是如果需要有选择性的织入到目标类某些特定的方法中时,就需要使用切点进行目标连接点的定位.增强提供了连接点方位信息:如织入到方法前面.后面等,而切点进一步描述织 ...
- Spring AOP 创建增强类
AOP联盟为增强定义了org.aopalliance.aop.Advice接口,Spring支持5种类型的增强: 1)前置增强:org.springframework.aop.BeforeAd ...
- Spring AOP创建Throwdvice实例
1.异常发生的时候,通知某个服务对象做处理 2.实现throwsAdvice接口 接口实现: public interface IHello { public void sayHello(String ...
随机推荐
- Jmeter(十七)_jmeter与java_selenium自动化
Jmeter可以通过WebDriver来完成UI自动化测试,也可以测试浏览器端对系统的压力,需要以下jiar包 基本配置 1:下载JMeterPlugins-WebDriver-1.3.1.zip,解 ...
- MyBatis你只写了接口为啥就能执行SQL啊?
一.静态代理 又是一年秋招季,很多小伙伴开始去大城市打拼.来大城市第一件事就是租房,免不了和中介打交道,因为很多房东很忙,你根本找不到他.从这个场景中就可以抽象出来代理模式: ISubject:被访问 ...
- Alpha3
队名:福大帮 组长博客链接:https://www.cnblogs.com/mhq-mhq/p/11899921.html 作业博客 :https://edu.cnblogs.com/campus/f ...
- QString 中文编码转换
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/xxm524/article/det ...
- 搭建阿里云服务 FTP 折中方案
该配置的服务都配置了,端口也都打开了 ,但是ftp 就是连接不上 就是打不开目录 8uftp 出现以下情况 配置文件逐条检查,端口逐个检查 都没有问题,还是出现这种情况,实在没辙,蛋疼...... ...
- C# 获取文件扩展信息-应用名称/作者等
方案一:使用微乳封装的Shell包 添加nuget包:Microsoft.WindowsAPICodePack.Shell using Microsoft.WindowsAPICodePack.She ...
- 【E2EL5】A Year in Computer Vision中关于图像增强系列部分
http://www.themtank.org/a-year-in-computer-vision 部分中文翻译汇总:https://blog.csdn.net/chengyq116/article/ ...
- pytorch visdom可视化工具学习—3-命令行操作使用经验
在使用过程中一直以为要在哪个指定的environment下(即参数env)绘制内容,就必须在使用时声明 比如如果不声明,默认的就是在'main'环境下,端口为8097: viz = visdom.Vi ...
- ios label根据内容自适应高度
label自适应高度,想必大家也都很熟悉怎么去做,上代码: UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(150, 50, 15 ...
- django安装使用xadmin
Xadmin介绍 直接替换掉Django自带的admin系统,并提供了很多有用的东西:完全的可扩展的插件支持,基于Twitter Bootstrap的漂亮UI. 完全替代Django admin 支持 ...