`
chinrui
  • 浏览: 93902 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

Spring AOP 简单使用

阅读更多
Spring AOP

在beans.xml进行配置,声明aop标签的使用规则
<?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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
  <context:annotation-config/>
  <context:component-scan base-package="com.edu.hpu" />
  <aop:aspectj-autoproxy/>
</beans>

导入相关jar包
  • aspectj-1.7.2.jar
  • aspectjweaver.jar
  • spring-aop-3.2.1.RELEASE.jar
  • spring-aspects-3.2.1.RELEASE.jar
  • aopalliance-alpha1.jar
  • aopalliance.jar

编写需要加入逻辑的类LogInteceptor,并在UserDAOImpl类save方法前插入逻辑
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component(value="logInterceptor")
public class LogInterceptor {

	@Before("execution(public void
              com.edu.hpu.impl.UserDAOImpl.save(com.edu.hpu.model.User))")
	public void before() {
		System.out.println("method start");
	}
}

在save方法正常执行完毕的时候加入逻辑
@AfterReturning("execution(public void
 com.edu.hpu.impl.UserDAOImpl.save(com.edu.hpu.model.User))")
public void afterReturning() {
	System.out.println("after Returning!");
}

在任何方法出现异常的时候加入逻辑
@AfterThrowing("execution(public * com.edu.hpu.impl.*.*(..))")
public void afterThrowing() {
	System.out.println("after Throwing");
}

在方法前后加入逻辑
@Around("execution(public * com.edu.hpu.impl.*.*(..))")
public void around(ProceedingJoinPoint pjp) throws Throwable {
	System.out.println("around method start");
	pjp.proceed();
	System.out.println("around method end");
}

定义通用方法,使用Pointcut切面
@Pointcut("execution(public * com.edu.hpu.impl.*.*(..))")
public void myMethod() {};

@Before("myMethod()")
public void before() {
	System.out.println("method start");
}

使用XML进行Aspect配置
在相应save方法前加上LogInterceptor类before方法的逻辑
第一种方法:声明一个全局的切面(Pointcut)
<bean id="logInteceptor" class="com.edu.hpu.aop.LogInterceptor"></bean>
<aop:config>
	<aop:pointcut expression="
execution(public * com.edu.hpu.impl.UserDAOImpl.*(com.edu.hpu.model.User))" id="servicePointcut"/>

	<aop:aspect id="logAspect" ref="logInteceptor">
		<aop:before method="before" pointcut-ref="servicePointcut"/>
	</aop:aspect>
</aop:config>

第二种方法:声明一个局部的Pointcut
<bean id="logInterceptor" class="com.edu.hpu.aop.LogInterceptor"></bean>
<aop:config>
	<aop:aspect id="logAspect" ref="logInterceptor">
		<aop:pointcut 
expression="execution(public * *.*(..))" id="servicePointcut"/>
		<aop:before method="before" pointcut-ref="servicePointcut"/>
	</aop:aspect>
</aop:config>

第三种方法:不声明Pointcut
<bean id="logInterceptor" class="com.edu.hpu.aop.LogInterceptor"></bean>
<aop:config>
	<aop:aspect id="logAspect" ref="logInterceptor">
		<aop:before method="before" pointcut="
execution(public * com.edu.hpu.impl.UserDAOImpl.*(com.edu.hpu.model.User))" />
	</aop:aspect>
</aop:config>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics