- 浏览: 646946 次
- 性别:
- 来自: 常州
文章分类
- 全部博客 (345)
- java (63)
- Struts2 (11)
- Spring2.5 + (17)
- Hibernate (25)
- Struts2 Spring hibernate (5)
- log4j (3)
- apache tomcat (12)
- oracle (22)
- oracle_存储过程 (4)
- mysql (18)
- jquery (11)
- prototype (5)
- js (19)
- quartz (5)
- 设计模式 (6)
- eclipse/MyEclipse 注意事项 (9)
- eclipse (0)
- css (6)
- 正则表达式 (2)
- linux (18)
- PHP (6)
- 多线程 (20)
- XML (1)
- jstl (3)
- mongoDB (7)
- android (20)
- 反射 (1)
- IOS (46)
- SVN (3)
- C/C++ (4)
- 百度地图 (2)
- IO/SOCKET (3)
- 百度地图JS (1)
- 树莓派/香蕉派 (1)
最新评论
-
anny101:
想转发一下,不知道怎么转发。评论一下吧。方便查看。
fetch = FetchType.EAGER 作用 -
Navee:
果然我这也是是防火墙问题
解决 Linux 安装 httpd局域网无法访问 -
dhyang909:
...
oracle 10g+ 行列转换 -
国产希特勒:
真强,居然有人把公司的面试题挂到javaeye上了
锦江国际的一道面试题(很简单) -
tomfish88:
比如我要拦截不同业务的service类里面的方法 @Poi ...
Spring AOP annotation 拦截表达式 分析
最后输出(可以看出各种通知的时间):
我是前置通知。
我是环绕--进。
aa 已成功保存
我是后置通知。
我是最终通知。
我是环绕--出。
app.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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <aop:aspectj-autoproxy /> <context:component-scan base-package="com.mhm.spring"/> </beans>
MyAOP.Java
package com.mhm.spring.mng.impl; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; @Component @Aspect public class MyAOP { //声明切入点 //注意,impl下面的类必须实现了mng中的接口,不然不能实现代理,但如果还是要使用代理 //那么,在项目中加入cglib,它会根据二进制来实现代理 @Pointcut("execution(* com.mhm.spring.mng.impl..*.*(..))") public void anyMethod(){}; //前置通知 @Before("anyMethod()") public void dobefore() { System.out.println("我是前置通知。"); } //后置通知 @AfterReturning("anyMethod()") public void doafterReturning() { System.out.println("我是后置通知。"); } //最终置通知 @After("anyMethod()") public void doafter() { System.out.println("我是最终通知。"); } //异常通知 @AfterThrowing("anyMethod()") public void doexception() { System.out.println("我是异常通知。"); } //环绕通知 @Around("anyMethod()") public Object doprocess(ProceedingJoinPoint pjp) throws Throwable { System.out.println("我是环绕--进。"); Object o = pjp.proceed(); System.out.println("我是环绕--出。"); return o; } }
PersonMng.java
package com.mhm.spring.mng; public interface PersonMng { public void save(String name); public String get(int id); public void delete(String id); }
PersonMngImpl:
package com.mhm.spring.mng.impl; import org.springframework.stereotype.Service; @Service public class PersonMngImpl implements PersonMng { @Override public void delete(String id) { System.out.println(id + " 已成功删除。"); } @Override public String get(int id) { return "返回ID为 " + id + " 的人"; } @Override public void save(String name) { //throw new RuntimeException("我是运行时异常。"); System.out.println(name + " 已成功保存"); } }
测试:package com.mhm.spring.mng.impl;
import org.junit.AfterClass; public class PersonMngImplTest { @BeforeClass public static void setUpBeforeClass() throws Exception { } @Test public void save() { try{ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); PersonMng personMng = (PersonMng)context.getBean("personMngImpl"); personMng.save("aa"); } catch (Exception e) { e.printStackTrace(); } } @AfterClass public static void tearDownAfterClass() throws Exception { } }
//下面是稍微复杂点的MyAOP
package com.mhm.spring.mng.impl; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; @Component @Aspect public class MyAOP { //声明切入点 @Pointcut("execution(* com.mhm.spring.mng.impl..*.*(..))") public void anyMethod(){}; //前置通知 //拦截参数为String类型的方法 @Before("anyMethod() && args(name)") public void dobefore(String name) { System.out.println("我是前置通知。" + name); } //后置通知 //拦截 返回类型为String 的方法 @AfterReturning(pointcut="anyMethod()", returning="result") public void doafterReturning(String result) { System.out.println("result: " + result); System.out.println("我是后置通知。" ); } //最终置通知 @After("anyMethod()") public void doafter() { System.out.println("我是最终通知。"); } //异常通知 @AfterThrowing(pointcut="anyMethod()", throwing="ex") public void doexception(Exception ex) { System.out.println("我是异常通知: " + ex); } //环绕通知 @Around("anyMethod()") public Object doprocess(ProceedingJoinPoint pjp) throws Throwable { System.out.println("我是环绕--进。"); Object o = pjp.proceed(); System.out.println("我是环绕--出。"); return o; } }
发表评论
-
springmvc mybatis,注解事务的使用
2015-12-04 17:30 1191两个XML配置中,需要把扫描的类区分出来。 sp ... -
spring mvc rest风格的URL输入中文乱码问题
2015-12-04 10:48 1871<!-- 处理请求时返回json字符串的中文乱码 ... -
Spring +hibernate 声明式 事物 + annotation
2012-03-22 19:49 912在applicationContext.xml中 &l ... -
Spring AOP annotation 用环绕通知写日志
2011-11-16 10:34 1438package com.mhm.mng.impl; im ... -
@Transactional 事务回滚 分析
2011-11-08 16:51 4283@Transactional 事务回滚 Spring的 ... -
Spring AOP annotation 拦截表达式 分析
2011-11-08 14:28 1785在app.xml中要加入aop的命名空间,看 http:// ... -
自定义标签中 如何使用 Spring 的 ioc
2011-11-07 17:38 1368自定义标签,继承了BodyTagSupport 在标签 ... -
简单模拟 Spring 的 ioc
2011-07-15 14:33 1222beans.xml <?xml version=&qu ... -
Spring AOP 使用
2010-11-25 00:14 1144applicationContext.xml <?xml ... -
Spring 中创建通知
2010-11-09 13:25 959这些通知类型,结合方 ... -
AOP 概念
2010-11-09 10:55 942连接点(jointpoint):一个连接点是一个程序执行过程的 ... -
Spring.AOP下的 hello world !
2010-11-09 10:48 1041此方法只做演示,项目中不使用 项目中用annotation ... -
web启动,quartz 关联的servlet 启动,得到Spring的bean ,servletContext 获取数据源
2010-07-29 17:21 2537package com.cal.servlet; imp ... -
使用annotation减少spring bean的配置
2010-04-23 17:42 1050http://blog.csdn.net/dqatsh/arc ... -
用main调试spring
2010-04-23 17:36 1212import org.springframework.c ... -
使用 Annotation将配置资源注入到Bean中
2010-04-23 16:21 1140csnd速度太慢,今天写博客这个功能都无法使用 关于 &qu ...
相关推荐
NULL 博文链接:https://bijian1013.iteye.com/blog/2142528
aop入门。
SpringAOP的注解配置完成切面的编程,完成execution,annotation两种表达式的实例Ddemo
NULL 博文链接:https://baobeituping.iteye.com/blog/1201798
在Spring1.2或之前的版本中,实现AOP的传统方式就是通过实现Spring的AOP API来定义Advice,并设置代理对象。Spring根据Adivce加入到业务流程的时机的不同,提供了四种不同的Advice:Before Advice、After Advice、...
spring中自定义注解(annotation)与AOP中获取注解.通过实例演示自定义注解。
本篇文章主要介绍了详解Spring Aop实例之AspectJ注解配置,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
(三) AOP的annotation实例 22 (四) AspectJ的专业术语 23 (五) 织入点语法 23 (六) Advice 24 (七) Pointcut 26 (八) annotatin方式的AOP实例 26 二、 AOP配置xml方式 27 三、 AOP实现动态代理注意 28 第九课:...
(三) AOP的annotation实例 22 (四) AspectJ的专业术语 23 (五) 织入点语法 23 (六) Advice 24 (七) Pointcut 26 (八) annotatin方式的AOP实例 26 二、 AOP配置xml方式 27 三、 AOP实现动态代理注意 28 第九课:...
Android AOP 注解详解及简单使用实例(三) 一、简介 在Android 里面 注解主要用来干这么几件事: 和编译器一起给你一些提示警告信息。 配合一些ide 可以更加方便快捷 安全有效的编写Java代码。谷歌出的support-...
Hibernate: 第一课:第一个hibernate项目 第二课:测试实体对象的生命周期 第三课:hibernate基本映射 ...第四课:spring对AOP的只是(采用Annotation的方式) 第五课:spring对AOP的只是(采用配置文件的方式)
2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 2.3.3. 对bean命名pointcut( bean name pointcut element)的支持 2.3.4. 对AspectJ装载时织入(AspectJ load-time weaving)的支持 2.4. 中间层 ...
2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 2.3.3. 对bean命名pointcut( bean name pointcut element)的支持 2.3.4. 对AspectJ装载时织入(AspectJ load-time weaving)的支持 2.4. 中间层 ...
14.4.4 Spring OXM 简单实例 14.5 小结 第15章 Spring MVC 15.1 Spring MVC概述 15.1.1 体系结构 15.1.2 配置DispatcherServlet 15.1.3 一个简单的实例 15.2 注解驱动的控制器 15.2.1 使用@RequestMapping映射请求 ...
Bee事务注解@Tran使用实例工程; 基于Spring boot + Bee; 涉及spring aop, 注解拦截,应该有类上和应该在方法的注解拦截. @within,@annotation使用实例.
14.4.4 Spring OXM 简单实例 14.5 小结 第15章 Spring MVC 15.1 Spring MVC概述 15.1.1 体系结构 15.1.2 配置DispatcherServlet 15.1.3 一个简单的实例 15.2 注解驱动的控制器 15.2.1 使用@RequestMapping映射请求 ...
3、struts2的配置采用xml(目前不支持annotation),spring和hibernate使用annotation配置; 4、开发者下载该应用后,只需修改WebRoot\WEB-INF\applicationContext.xml,将其中数据库连接信息修改为自己的即可;
本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mvc和rest小例子没有介绍到数据层的内容,现在这一篇补上。下面开始贴代码。 文中用的框架版本:spring 3,hibernate 3,没有的,自己上网下。 先说...
然后我们就使用AOP + Annotation来解决这个问题,同时使用自动加载机制来实现数据“常驻内存”。 在infoq发表的文章 推荐使用这个, 中也有可运行的例子。 原始阅读 已经实现基于aspectj的AOP,代码在。想通过阅读...
查询出列表,也就是返回list, 在我们这个例子中也就是 List<User> , 这种方式返回数据,需要在User.xml 里面配置返回的类型 resultMap, 注意不是 resultType, 而这个resultMap 所对应的应该是我们自己配置的 ...