- 浏览: 198325 次
- 性别:
- 来自: 广州
最新评论
-
lxyoucan:
特地注册账号来感谢
在jsp上显示pdf||网页直接打开PDF 在IE中显示pdf文档的方法及参数设置 -
jinjiaoliu:
...
ant使用scp上传文件问题 -
AspirantHui:
非常感谢,你提供的方法解决了我的问题!
在jsp上显示pdf||网页直接打开PDF 在IE中显示pdf文档的方法及参数设置 -
cxh61207:
...
ant使用scp上传文件问题 -
daoyuanjiao:
如果用的是福昕PDF阅读器,该怎么弄呢?
在jsp上显示pdf||网页直接打开PDF 在IE中显示pdf文档的方法及参数设置
Spring AOP是面向切面的方式,大部分项目使用它都是在事物的处理方面,有关具体的AOP的概念这里就不介绍了,今天我主要通过一个简单的例子让大家来了解AOP的相关应用
1.首先看下我项目中service的配置文件
<?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:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" 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/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd "> <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory" /> <!-- the transactional advice (what 'happens'; see the <aop:advisor/> bean below) --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <!-- the transactional semantics... --> <tx:attributes> <!-- methods starting with 'save', 'update' or 'remove' use the default transaction settings --> <tx:method name="opt*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="BusinessException"/> <tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="BusinessException"/> <tx:method name="create*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="BusinessException"/> <tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="BusinessException"/> <tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="BusinessException"/> <tx:method name="insert*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="BusinessException"/> <!-- other methods are set to read only <tx:method name="*" read-only="true"/> --> </tx:attributes> </tx:advice> <!-- ensure that the above transactional advice runs for any execution of an operation defined by the FooService interface --> <aop:config> <aop:advisor pointcut="@within(com.berheley.aop.Dbaop)" advice-ref="txAdvice" /> <aop:advisor pointcut="execution(* com.berheley.oa.project.business.service.impl..*(..))" advice-ref="txAdvice" /> <aop:advisor pointcut="execution(* com.berheley.oa.project.desktop.module.DtModuleBO.*(..))" advice-ref="txAdvice" /> <aop:advisor pointcut="execution(* com.berheley.oa.project.desktop.layout.DtLayoutBO.*(..))" advice-ref="txAdvice" /> <aop:advisor pointcut="execution(* com.berheley.oa.project.desktop.SetLayOutBo.*(..))" advice-ref="txAdvice" /> <aop:advisor pointcut="execution(* com.berheley.oa.project.remind.RemindBO.*(..))" advice-ref="txAdvice" /> </aop:config> <!-- 此句使aspectj可用,必需加入 --> <aop:aspectj-autoproxy/> <!-- 此句声明Aspect类,Spring会根据里面的定义对相关类作AOP处理 --> <bean id="JBPMAspectJ" class="com.berheley.jbpm.plugin.JBPMAspectJ" /> <bean id="ProcessEventListener" class="com.berheley.oa.listener.air.ProcessEventListener" /> <!-- 业务类 --> <bean id="commonService" class="com.berheley.oa.project.business.service.impl.CommonBO"> <property name="dao"> <ref bean="commonDao"/> </property> <property name="scheduler" ref="schedulerFactory" /> </bean> </beans>
从这个配置文件中,可以看出事物也用到了AOP;如果要要某个方法进行拦截或者监听,就必须加入:<aop:aspectj-autoproxy/> ,然后再下面在配置做AOP处理的类,类似: <bean id="JBPMAspectJ" class="com.berheley.jbpm.plugin.JBPMAspectJ" />;
2.AOP处理类:
package com.berheley.oa.listener.air; import java.util.List; import java.util.Map; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.web.context.WebApplicationContext; import com.berheley.jbpm.model.CustomTaskInstance; import com.berheley.oa.common.ConstantDefine; import com.berheley.oa.project.business.service.def.workflow.IWorkFlowServiceBO; import com.berheley.oa.project.persistence.model.TUgUser; import com.berheley.util.ApplicationContextKeeper; @Aspect public class ProcessEventListener { @Before("execution(* com.berheley.oa.project.business.service.impl.workflow.WorkFlowServiceBO.optBusinessServiceBeforeWFTask*(..))") public void doLogBefore() { } @AfterReturning("execution(* com.berheley.oa.project.business.service.impl.workflow.WorkFlowServiceBO.getTaskInstanceInfo(..))") public void doLogAfter(JoinPoint jp) throws Exception { WebApplicationContext wac = (WebApplicationContext) ApplicationContextKeeper.getAppCtx(); IWorkFlowServiceBO workflowBo = (IWorkFlowServiceBO) wac.getBean(ConstantDefine.WORKFLOW_SERVICE); Object[] args = jp.getArgs();// 获得方法的参数 // 获得流程的实例 CustomTaskInstance taskInstance = (CustomTaskInstance) args[0]; // 判断流程是否结束,如果结束不做处理 if (taskInstance.getProcessInstance().hasEnded()) { } else { // 通过实例获得流程的代办人和流程的id List<Map<String, Object>> list = workflowBo.getAcorIdByTaskInstance(taskInstance); if (list.size() > 0) { if (list.get(0).get("userName") != null && !"".equals(list.get(0).get("userName"))) { // 打开流程代办的url String url = "/workflow/service/doTask.ao?type=message&method=goTask&defaltPage=0&taskInstanceId=" + list.get(0).get("taskId"); // 当前流程的代办人的username String userName = list.get(0).get("userName").toString(); TUgUser user = workflowBo.getUserByUsername(userName); // 取出当前代办人的所有代办流程信息 String message = workflowBo.getTaskListReMindForMainPage("120", user); MessagePacking mp = new MessagePacking(); String processMessage = mp.packingCurrentProcessMessage(message, list.get(0).get("taskId").toString(), user); // 把取出的流程消息还原成json MessageSocketClient ms = new MessageSocketClient(); ms.sendMessage(processMessage); } } } } }
通过这段代码,应该可以让大家初步的掌握AOP的使用
发表评论
-
Java之RMI设计模式基本原理与示例
2013-01-23 14:45 10551. Name RMI是Romote Method Invo ... -
Java常用的通信协议效率比较(转)
2013-01-23 14:38 1543本文比较了RMI,Hessian, ... -
JQuery工具插件qTip
2011-12-14 17:38 1183好吧,原谅我偷懒:http://messense.me/jqu ... -
JAVA解压缩文件——包含嵌套的压缩文件
2011-09-15 09:19 3024今天主要介绍JAVA处理ZIP文件,JAVA提供了相应的类、方 ... -
JAVA:附加码生成器(图片)
2011-08-29 11:30 1431〔Picture.java〕 package creato ... -
面试题--求质数
2011-04-29 13:04 1191求100以内的质数(指在一个大于1的自然数中,除了1和此整数自 ... -
Workspace in use or cannot be created, choose a different one.--错误解决办法
2010-11-25 13:16 1595eclipse 使用一段时间后,有时会因为一些故障自己就莫名 ... -
Java定时任务Timer
2010-11-19 11:12 1360Java定时任务,用到的地方可能比较多,例如:定时对数据库的某 ... -
Tomcat 启动时加载方法
2010-11-11 15:54 1547最近遇见一个需求,需要在项目中加入一个Mina通讯的客户端,而 ... -
AOP几个重要的概念
2010-11-09 16:45 1749《Spring参考手册》中定义了以下几个AOP的重要概念: ... -
结果集转换成json字符串
2010-08-25 11:20 3143在项目中做查询时一般使用:sql或者hql,今天分别介绍一下怎 ... -
java还原科学计数法的数值
2010-08-06 15:26 1973java.text.DecimalFormat nf = ne ... -
java.lang.IllegalArgumentException: No bean specified
2010-08-04 17:04 2904java.lang.IllegalArgumentExcept ... -
argument type mismatch问题解决
2010-07-01 09:31 6503type Exception report messag ... -
实现sybase数据库字段自增
2010-04-23 13:44 1062例如:在sybase中有一张用户表t_ug_user,其中包含 ... -
Hibernate框架ORM的原理
2010-04-21 14:48 1186hibernate,通过对jdbc进行封装,对 java类和 ... -
net.sourceforge.jtds.jdbc.ClobImpl 问题
2010-02-08 14:45 2811小弟最近在做数据查询时,遇到一个这样的问题: 数据列 ... -
父子页面之间值传递问题
2009-12-31 17:26 1545第一:页面中包含iframe ... -
Oracle函数和mysql函数比较
2009-12-31 17:23 21401. Oracle中的to_number()转换成 ... -
fusioncharts生成图表flash遮挡页面中元素的情况
2009-12-31 15:57 2186在做web开发中遇到fusionc ...
相关推荐
Spring AOP 通知种类:设置次要业务与(被监听方法)绑定执行顺序问题 1.前置通知: 1)切面:次要业务方法 2) 执行切入点:被拦截的主要业务方法 2.后置通知: 1)执行切入点:被拦截的主要业务方法 2)切...
7.3.2 一个简单的例子 7.3.3 如何通过配置使用@AspectJ切面 7.4 @AspectJ语法基础 7.4.1 切点表达式函数 7.4.2 在函数入参中使用通配符 7.4.3 逻辑运算符 7.4.4 不同增强类型 7.4.5 引介增强用法 7.5 切点函数详解 ...
7.3.2 一个简单的例子 7.3.3 如何通过配置使用@AspectJ切面 7.4 @AspectJ语法基础 7.4.1 切点表达式函数 7.4.2 在函数入参中使用通配符 7.4.3 逻辑运算符 7.4.4 不同增强类型 7.4.5 引介增强用法 7.5 切点函数详解 ...
2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 2.4. 中间层 2.4.1. 在XML里更为简单的声明性事务配置 2.4.2. JPA 2.4.3. 异步的JMS 2.4.4. JDBC 2.5. Web层 2.5.1. Spring MVC的表单标签库 ...
2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 2.4. 中间层 2.4.1. 在XML里更为简单的声明性事务配置 2.4.2. JPA 2.4.3. 异步的JMS 2.4.4. JDBC 2.5. Web层 2.5.1. Spring MVC的表单标签库 2.5.2. ...
2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 2.4. 中间层 2.4.1. 在XML里更为简单的声明性事务配置 2.4.2. JPA 2.4.3. 异步的JMS 2.4.4. JDBC 2.5. Web层 2.5.1. Spring MVC的表单标签库 ...
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. 中间层 ...
简单的输入、输出例子 547 内容总结 551 独立实践 552 第二十八章: Servlet 553 学习目标 553 Java Servlet概述 554 Servlet能够做什么 554 Servlet的生命周期 557 Java Servlet API 560 Web上使用的...
通过ConfigHandler的解析,我们最终得到一个ConfigInfo实例,Factory就是根据这个实例中所包含的配置信息,利用反射技术对所需对象生成并组装的。SayHelloFactory的代码如下: using System; using System.IO; using...
这里启动的Linstener均在应用之上就已经实例化完成了,而且Linstener也不是Spring组件,因此无法进行Spring的Bean注入操作。但是当上下文重新刷新完毕后可以通过context工厂获取Bean2. SpringBean生命周期3.Spring...
2. 事件监听 48 (三) I/O输入输出 63 1. 流及输入输出流概述 63 2. File类 64 3. 带缓存的输入输出流 72 4. 序列化 76 (四) 多线程 80 1. 概念与原理 80 2. 两种实现方式 81 3. 生命周期及状态转换 84 4. 线程调度 ...
学生提问:为什么静态内部类实例方法也不能访问外部类的实例属性呢? 207 学生提问:接口里是否能定义内部接口? 208 6.7.3 使用内部类 208 学生提问:既然内部类是外部类的成员,是否可以为外部类定义子类,在...
第3章 Spring AOP和AspectJ支持 112 3.1 启用Spring的AspectJ注解支持 113 3.1.1 问题 113 3.1.2 解决方案 113 3.1.3 工作原理 113 3.2 用AspectJ注解声明aspect 115 3.2.1 问题 115 3.2.2 解决方案...
第3章 Spring AOP和AspectJ支持 112 3.1 启用Spring的AspectJ注解支持 113 3.1.1 问题 113 3.1.2 解决方案 113 3.1.3 工作原理 113 3.2 用AspectJ注解声明aspect 115 3.2.1 问题 115 3.2.2 解决方案...
其中第16行通过类路径的映射方式,将sshfile.model类包目录下的所有领域对象的映射文件装载进来,在本文的例子里,它将装载进Tfile.hbm.xml映射文件。如果有多个映射文件需要声明,使用类路径映射方式显然比直接...
{1.11}简单算法}{38}{section.1.11} {1.11.1}打乱算法}{38}{subsection.1.11.1} {1.11.2}排序算法}{38}{subsection.1.11.2} {1.11.2.1}选择排序}{38}{subsubsection.1.11.2.1} {1.11.2.2}冒泡排序}{39}{...