采用AOP配置方式的AOP实现及XML解析
AOP的重要概念
SpringAOP通知(Advice)种类介绍
Advice通知类通知方法中传入了连接点参数JoinPoint,可以通过该参数获取目标对象的类名,目标方法名和目标方法参数等信息。 package hl.aop;
import java.util.List;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
public class AllLogAdvice {
private Logger logger = Logger.getLogger(AllLogAdvice.class);
public void myBeforeAdvice(JoinPoint joinpoint){
//获取被调用的类名
String targetClassName = joinpoint.getTarget().getClass().getName();
//获取被调用的方法名
String targetMethodName = joinpoint.getSignature().getName();
//日志格式字符串
String logInfoText = "前置通知:"+targetClassName+"类的"+targetMethodName+"方法开始执行";
//将日志写入配置文件中
logger.info(logInfoText);
}
public void myAfterReturnAdvice(JoinPoint joinPoint){
String targetClassName = joinPoint.getTarget().getClass().getName();
String targetMethodName = joinPoint.getSignature().getName();
String logInfoText = "返回后通知:"+targetClassName+"类的"+targetMethodName+"方法执行完毕";
logger.info(logInfoText);
}
public void myThrowingAdvice(JoinPoint joinPoint,Exception e){
String targetClassName = joinPoint.getTarget().getClass().getName();
String targetMethodName = joinPoint.getSignature().getName();
String logInfoText = "异常通知:执行"+targetClassName+"类的"+targetMethodName+"方法时发生异常";
logger.info(logInfoText);
}
/** * 目标方法若有返回值,则该环绕通知也必须要有与目标方法相同的放回值 * 否者会发生空指针异常(即:目标方法中的返回值被拦截并且不会向下传递) * @param pJoinpoint * @return * @throws Throwable */
public List myAroundAdvice(ProceedingJoinPoint pJoinpoint) throws Throwable{
long beginTime = System.currentTimeMillis();
List list = (List)pJoinpoint.proceed();
long endTime =System.currentTimeMillis();
long custTime = endTime-beginTime;
String targetMethodName = pJoinpoint.getSignature().getName();
String logInfoText = "环绕通知:"+targetMethodName+"方法调用开始时间"+beginTime+"毫秒,调用结束时间"+endTime+"毫秒,共耗时+"+custTime+"毫秒";
logger.info(logInfoText);
return list;
}
}
applicationContext.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<!--driverClassName 该属性要加上,否则会出现no suitable Driver错误 -->
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url" value="jdbc:mysql://localhost:3306/bank"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>hl/entity/Account.hbm.xml</value>
</list>
</property>
</bean>
<bean id="AccountDao" class="hl.dao.AccountDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="AccountBiz" class="hl.bean.AccountBizImpl">
<property name="accountDao" ref="AccountDao"></property>
</bean>
<bean id="searchAction" class="hl.action.SearchAction">
<property name="accountBiz" ref="AccountBiz"></property>
</bean>
<bean id="allLogAdvice" class="hl.aop.AllLogAdvice"></bean>
<aop:config>
<!-- 配置日志切面 -->
<aop:aspect id="logaop" ref="allLogAdvice">
<!-- 定义切入点,切入点采用正则表达式 (此处含义为:对hl.action中的所有方法都进行拦截) -->
<aop:pointcut expression="execution(* hl.bean..*.*(..))" id="logpointcut" />
<!-- 配置前置通知 -->
<aop:before method="myBeforeAdvice" pointcut-ref="logpointcut" />
<!-- 配置返回后通知 -->
<aop:after-returning method="myAfterReturnAdvice" pointcut-ref="logpointcut" />
<!-- 配置异常通知 -->
<aop:after-throwing method="myThrowingAdvice" pointcut-ref="logpointcut" throwing="e"/>
<!-- 配置环绕通知 -->
<aop:around method="myAroundAdvice" pointcut-ref="logpointcut" />
</aop:aspect>
</aop:config>
</beans>
<!-- 切入点表达式 通常情况下,表达式中使用”execution“就可以满足大部分的要求。表达式格式如下: execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?) modifiers-pattern:方法的操作权限 ret-type-pattern:返回值 declaring-type-pattern:方法所在的包 name-pattern:方法名 parm-pattern:参数名 throws-pattern:异常 其中,除ret-type-pattern和name-pattern之外,其他都是可选的。上例中,execution(* hl.bean.*.*(..))表示hl.bean包下,返回值为任意类型;方法名任意;参数不作限制的所有方法。 JoinPoint 接口提供了一系列有用的方法, 比如 getArgs() (返回方法参数)、getThis() (返回代理对象)、getTarget() (返回目标)、getSignature() (返回正在被通知的方法相关信息)和 toString() (打印出正在被通知的方法的有用信息。 -->
如果要在工程中使用AOP需要几个jar包: xml配置文件解析Xml技术在各类框架中都有重要的作用。除了xml的配置,Spring还支持基于注解及基于Java类的Bean配置,由于不同配置方式在“质”上都是基本相同的,只是存在“形”的区别,而基于XML的配置方式是功能最强的,所以我们先以基于XML的配置方式入手,后续再讲述其他两种配置方式。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |