`

AOP监听的简单例子

    博客分类:
  • java
阅读更多

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的使用

分享到:
评论

相关推荐

    spring代码课堂笔记

    Spring AOP 通知种类:设置次要业务与(被监听方法)绑定执行顺序问题 1.前置通知: 1)切面:次要业务方法 2) 执行切入点:被拦截的主要业务方法 2.后置通知: 1)执行切入点:被拦截的主要业务方法 2)切...

    Spring.3.x企业应用开发实战(完整版).part2

    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 切点函数详解 ...

    Spring3.x企业应用开发实战(完整版) part1

    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 切点函数详解 ...

    Spring 2.0 开发参考手册

    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的表单标签库 ...

    Spring-Reference_zh_CN(Spring中文参考手册)

    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. ...

    spring chm文档

    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的表单标签库 ...

    Spring中文帮助文档

    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. 中间层 ...

    Spring API

    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. 中间层 ...

    Java语言基础下载

    简单的输入、输出例子 547 内容总结 551 独立实践 552 第二十八章: Servlet 553 学习目标 553 Java Servlet概述 554 Servlet能够做什么 554 Servlet的生命周期 557 Java Servlet API 560 Web上使用的...

    Spring.net框架

    通过ConfigHandler的解析,我们最终得到一个ConfigInfo实例,Factory就是根据这个实例中所包含的配置信息,利用反射技术对所需对象生成并组装的。SayHelloFactory的代码如下: using System; using System.IO; using...

    Spring学习

    这里启动的Linstener均在应用之上就已经实例化完成了,而且Linstener也不是Spring组件,因此无法进行Spring的Bean注入操作。但是当上下文重新刷新完毕后可以通过context工厂获取Bean2. SpringBean生命周期3.Spring...

    从J2SE到J2EE知识点介绍

    2. 事件监听 48 (三) I/O输入输出 63 1. 流及输入输出流概述 63 2. File类 64 3. 带缓存的输入输出流 72 4. 序列化 76 (四) 多线程 80 1. 概念与原理 80 2. 两种实现方式 81 3. 生命周期及状态转换 84 4. 线程调度 ...

    疯狂JAVA讲义

    学生提问:为什么静态内部类实例方法也不能访问外部类的实例属性呢? 207 学生提问:接口里是否能定义内部接口? 208 6.7.3 使用内部类 208 学生提问:既然内部类是外部类的成员,是否可以为外部类定义子类,在...

    Spring攻略(第二版 中文高清版).part2

    第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 解决方案...

    Spring攻略(第二版 中文高清版).part1

    第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 解决方案...

    ssh(structs,spring,hibernate)框架中的上传下载

     其中第16行通过类路径的映射方式,将sshfile.model类包目录下的所有领域对象的映射文件装载进来,在本文的例子里,它将装载进Tfile.hbm.xml映射文件。如果有多个映射文件需要声明,使用类路径映射方式显然比直接...

    Java学习笔记-个人整理的

    {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}{...

Global site tag (gtag.js) - Google Analytics