- 浏览: 798775 次
- 性别:
- 来自: 西安
文章分类
- 全部博客 (307)
- struts (8)
- hibernate (3)
- spring (32)
- opensourceproject (12)
- javaScript (9)
- primeton EOS (2)
- journey of heart (10)
- Design pattern (6)
- ejb (17)
- point (37)
- Linux&Unix (22)
- ibatis (10)
- AJAX (6)
- DB (26)
- Protocol (6)
- chart (4)
- web server (11)
- webservice (7)
- integration (3)
- tuxedo (5)
- ext (4)
- android (1)
- c/c++ (12)
- JVM (1)
- paginationFrame (2)
- code (2)
- report (1)
- High-performance web (1)
- svn (1)
- JQuery (1)
- workDaily (2)
- cloud (16)
- Python (8)
- English (2)
- shell (5)
- googleCode (1)
- nio (1)
- hyper-v (1)
- debug (3)
- vbs (2)
- openstack (3)
- K8S (1)
- Mesos (0)
- Spark (0)
- Marathon (0)
最新评论
-
钱图大展:
chao2751021 写道lib包哪里去下载,找不到
大型网站用户行为记录的一个实现--基于clickStream(第一部分) -
钱图大展:
无法下载
大型网站用户行为记录的一个实现--基于clickStream(第一部分) -
fm395728572:
shell脚本中用到了环境变量,但是获取不到,例如脚本中有一句 ...
ganymed-ssh2 for Java -
liuhanjiang:
我qq147229234
大型网站用户行为记录的一个实现--基于clickStream(第一部分) -
liuhanjiang:
博主 我利用您提供的方法实现博文中介绍的clickstream ...
大型网站用户行为记录的一个实现--基于clickStream(第一部分)
使用Spring进行面向切面(AOP)编程
要进行AOP编程,首先我们要在spring的配置文件中引入aop命名空间: <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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> </beans>
Spring提供了两种切面声明方式,实际工作中我们可以选用其中一种:
l基于XML配置方式声明切面。
l
基于注解方式声明切面。
首先启动对@AspectJ注解的支持(蓝色部分):
bean.xml
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<aop:aspectj-autoproxy/>
<bean id="orderservice" class="cn.itcast.service.OrderServiceBean"/>
<bean id="log" class="cn.itcast.service.LogPrint"/>
</beans>
下面我们使用spring注解方式完成aop编程
步骤:
(1).导入依赖jar文件
如果使用了切面编程(AOP),还需要下列jar文件
lib/aspectj/aspectjweaver.jar和aspectjrt.jar
lib/cglib/cglib-nodep-2.1_3.jar
如果使用了JSR-250中的注解,如@Resource/@PostConstruct/@PreDestroy,还需要下列jar文件
lib\j2ee\common-annotations.jar
lib/aspectj/aspectjweaver.jar和aspectjrt.jar
lib/cglib/cglib-nodep-2.1_3.jar
如果使用了JSR-250中的注解,如@Resource/@PostConstruct/@PreDestroy,还需要下列jar文件
lib\j2ee\common-annotations.jar
(2) 在bean.xml中 配置上面的aop支持,并且打开 <aop:aspectj-autoproxy/>
(3) 写接口和实现,方便测试
接口 PersionSevice
package cn.com.xinli.service;
public interface PersionSevice { public void save(String name); public void update(String name, Integer personid); public String getPersonName(Integer personid); }
实现 PersionServiceBean
package cn.com.xinli.service.impl; import org.apache.log4j.Logger; import cn.com.xinli.service.PersionSevice; public class PersionServiceBean implements PersionSevice { Logger log=Logger.getLogger(PersionServiceBean.class); public String getPersonName(Integer personid) { System.out.println("我是getPersonName()方法"); return "xxx"; } public void save(String name) { System.out.println("我是save()方法"); } public void update(String name, Integer personid) { System.out.println("我是update()方法"); } }
(4) 建立一个切面类 MyInterceptor (第一次测试的时候我懒得敲代码,直接复制现成的,没注意切面中的方法还有参数
,导致报错:实例化不了一个bean, 结果整了1天才发现原因,教训丫....)
package cn.com.xinli.service;
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; /** * 切面 * */ @Aspect public class MyInterceptor { /** * 1.第一个* 号代表 返回值的类型可以为任意,也可以指定比如 String,Int * 2.cn.itcast.service.impl.PersonServiceBean.. 表示对cn.itcast.service.impl.PersonServiceBean * 包下及其子包的下都进行拦截(注意是两个.) 如果是一个 . ,那么就拦截这个包下的类,不会去拦截子包 * 3. .. 后面的 *.* 第一个星号代表类,为星号说明对所有的类都拦截,第二个星号代表方法,为星号代表拦截所有的方法 * 4. (..) 代表方法的参数为任意,有也可以,没有也可以 * 5.如果要拦截PersonServiceBean类下所有的业务方法,也可以这么写: * @Pointcut("execution (* cn.itcast.service.impl.PersonServiceBean*.*(..))") * */ @Pointcut("execution(* cn.com.xinli.service.impl.PersionServiceBean*.*(..))") private void anyMethod() {}//声明一个切入点 /*对业务方法中只有一个参数的方法实施 前置通知*/ //@Before("anyMethod() && args(name)") @Before("anyMethod()") public void doAccessCheck() { System.out.println("前置通知"); } /*在业务方法执行完毕后,后置通知拿到业务的返回值*/ @AfterReturning(pointcut="anyMethod()",returning="result") //@AfterReturning("anyMethod()") public void doAfterReturning() { System.out.println("后置通知"); } @After("anyMethod()") public void doAfter() { System.out.println("最终通知"); } /*在出现异常的情况下在例外通知中拿到异常*/ @AfterThrowing(pointcut="anyMethod()",throwing="e") //@AfterThrowing(pointcut="anyMethod()") public void doAfterThrowing(Exception e) { System.out.println("例外通知:"+ e); } //环绕通知,固定写法,可以实现上面的所有通知 @Around("anyMethod()") public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable { //if(){//判断用户是否在权限 System.out.println("进入方法"); Object result = pjp.proceed(); System.out.println("退出方法"); //} return result; } }
(5)一定要讲业务bean和切面配置在beans.xml中(也可以使用扫描)
<aop:aspectj-autoproxy proxy-target-class="true"/> <bean id="myInterceptor" class="cn.com.xinli.service.MyInterceptor"></bean> <bean id="personService" class="cn.com.xinli.service.impl.PersionServiceBean"></bean>
(6) 测试
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); PersionSevice ps=(PersionSevice)ctx.getBean("personService"); ps.save("xxx");
(7)结果:
前置通知
进入方法
我是save()方法
后置通知
最终通知
退出方法
进入方法
我是save()方法
后置通知
最终通知
退出方法
发表评论
-
hibernatetemplate类使用
2010-03-19 22:25 1182http://115893520.iteye.com/blog ... -
Spring 中的JdbcTemplate使用
2010-03-19 22:11 34791.JdbcTemplate的execute()方 ... -
SpringAOP 的使用(两种方式)
2009-10-22 13:42 1486使用Spring AOP 拦截 方法,给被拦截的 ... -
第30讲--Spring提供的CharacterEncoding和OpenSessionInView功能
2009-09-19 18:24 1765CharacterEncoding: 在web ... -
第29讲--为Spring集成的Hibernate配置二级缓存
2009-09-18 00:29 3370合理的使用缓存策略,往往在web开发中提高性能起到关键 ... -
第28讲--Struts与Spring集成方案2(Spring集成Struts)
2009-09-17 00:57 1603集成步骤: 1.和方案1一样需要在web. ... -
第27讲--Struts与Spring集成方案1(Struts集成Spring)
2009-09-16 08:25 1789Spring2.5+Hibernate3.3+Stru ... -
第25,26讲 --搭建和配置Spring与Hibernate整合的环境
2009-06-21 11:21 1582Spring2.5+Hibernate3.3+Stru ... -
第24讲 --使用Spring配置文件实现事务管理
2009-06-20 23:56 1742采用基于XML方式配置事务 <bean ... -
第23讲 --使用Spring注解方式管理事务与传播行为详解
2009-06-11 23:19 2226事务传播属性 REQUIR ... -
第21,22讲 --搭建和配置Spring与jdbc整合的环境+Spring集成的jdbc编码和事务
2009-06-10 08:06 2677Spring+JDBC组合开发配置数据源有两种方式: ... -
第20讲--aspectj的切入点语法定义细节
2009-06-06 17:47 3620表达式分析: expression=" ... -
第19讲 --使用Spring配置文件实现AOP
2009-06-06 17:21 1519上面我们是使用注解的方式实现spring AOP的,下面 ... -
第18讲 --使用Spring的注解方式实现AOP的细节
2009-06-06 15:48 1532上一讲我们使用spring注解的方式理由aop技术 ... -
第16讲 --使用CGLIB实现AOP功能与AOP概念解释
2009-06-04 07:42 1612当代理对象没有实现 ... -
第15讲 --使用JDK中的Proxy技术实现AOP功能
2009-06-04 07:34 2306当目标类实现了接口,我们可以使用jdk的Proxy ... -
第14讲 --让Spring自动扫描和管理Bean
2009-06-02 23:40 8732通过在classpath自动扫描方式把组件纳入sp ... -
第13讲 --Autowire注解与自动装配
2009-06-02 22:46 30981. Autowire 默认是 ... -
第12讲 --编码剖析@Resource注解的实现原理
2009-06-01 23:14 3246在这一讲开始之前,我们先学习一下jdk5.0 中的一个新 ... -
第11讲 --用@Resource注解完成属性装配
2009-05-31 22:42 7009bean的注入方式有3种: 第一种:使用构造器注 ...
相关推荐
使用Spring的注解方式实现AOP的细节
使用Spring的注解方式实现AOP入门
spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...
NULL 博文链接:https://coolszy.iteye.com/blog/540465
基于注解实现SpringAop基于注解实现SpringAop基于注解实现SpringAop
spring注解方式实现aop,希望对大家有所帮助,内附注释
Spring通过注解实现AOP,Spring通过注解实现AOP,Spring通过注解实现AOPSpring通过注解实现AOP,Spring通过注解实现AOP,Spring通过注解实现AOP
演示了spring对aop的支持,包括注解方式、基于xml方式。
注解方式实现AOP编程
NULL 博文链接:https://huihai.iteye.com/blog/858769
Spring4--2.bean注解和AOP
NULL 博文链接:https://coolszy.iteye.com/blog/541997
开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE...
NULL 博文链接:https://sunbin.iteye.com/blog/2373014
Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 ...
以注解方式模拟Spring IoC AOP,了解Spring是如何实现的.
使用Spring配置文件实现AOP
commons-logging-1.1.3,spring-test-4.0.0.RELEASE,spring-aop-4.0.0.RELEASE,spring-aspects-4.0.0.RELEASE
SSM整合-用springaop-demo01实现了注解AOP,SSM-MybatisOneForOne-demo01实现了
草稿:SSM整合-用springaop-demo01实现了注解AOP,SSM-MybatisOneForOne-demo01实现