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