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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
<aop:aspectj-autoproxy /> <!-- 支持注解方式 -->
<context:component-scan base-package="com.bjsxt"/>
</beans>
-----------------------------------------------------------
常用切面编程的模板文件用时拷贝一下:
package com.bjsxt.test.spring.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
* 日志拦截器
*/
@Aspect
@Component
public class LogInterceptor {
// public User add(User user)
// @Pointcut("execution(public * com.bjsxt.test.spring.aop.*.add*(..))")
@Pointcut("execution(public * com.bjsxt.test.spring.aop.*.get*(..))") //声明一个名字为log的切入点
private void log(){//如果其他的地方要引用这个切入点,,就写这个方法名
}
@Before("log()"+"&&args(user)") //使用pointcut-->log .前置通知.处理传入的参数跟下面的形参保持一致即可。
private void before(User user){
System.out.println("方法执行前:"+user.getUname());
System.out.println("方法执行前!");
}
@After("log()") //最终后置通知。不管有无异常,最终都会执行!
private void after(){
System.out.println("方法执行后!");
}
@AfterReturning("log()") //方法正常结束,后置通知
private void afterReturning(){
System.out.println("方法执行后!after returning!");
}
@AfterThrowing("log()") //抛出异常会执行,异常通知
private void afterThrowing(){
System.out.println("方法执行后!after throwing!");
}
@Around("log()") //环绕通知在一个方法执行之前和之后执行
private Object around(ProceedingJoinPoint pjp){
System.out.println("环绕通知,方法执行前!");
Object obj = null;
try {
obj = pjp.proceed();//出调用真正的方法
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("环绕通知,方法执行后!");
return obj;
}
}
源文件下载地址:http://pan.baidu.com/share/link?shareid=1804614721&uk=1057836303
----------------------------------下面是配置文件方式实现切面编程
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
<aop:aspectj-autoproxy /> <!-- 支持注解方式 -->
<context:component-scan base-package="com.bjsxt"/>
<!-- 采用配置方式使用AOP -->
<bean id="logInterceptor" class="com.bjsxt.spring.aop.LogInterceptor"></bean>
<aop:config >
<!--定义切点 名字log -->
<aop:pointcut expression="execution(public !java.lang.String com.bjsxt.spring.aop.UserDaoImpl.*(com.bjsxt.spring.aop.User,..))" id="log"/>
<!-- 定义切面 logAspect-->
<aop:aspect id="logAspect" ref="logInterceptor">
<!-- 定义前置通知 -->
<!-- arg-names="user,age,name,test" 多参数 arg-names 指定before参数名 -->
<aop:before method="before" arg-names="user" pointcut="execution(!java.lang.String com.bjsxt.spring.aop.UserDaoImpl.add(..)) and args(user)" />
<!-- 后置通知 -->
<aop:after method="after" pointcut-ref="log" />
<aop:after-throwing method="afterThrowing" pointcut-ref="log"/>
<aop:after-returning method="afterReturning" pointcut-ref="log"/>
<aop:around method="around" pointcut-ref="log"/>
</aop:aspect>
</aop:config>
</beans>
----------------------------------------------
package com.bjsxt.spring.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
* 日志拦截器
*/
public class LogInterceptor {
// @Pointcut("execution(!java.lang.String com.cssxt.spring.aop.UserDaoImpl.add(com.cssxt.spring.aop.User,..))") //生命一个名字为log的切入点
private void log(){
}
// @Before("log()"+"&&args(user)") //使用pointcut-->log .前置通知.处理传入的参数跟下面的形参保持一致即可。
private void before(User user){
System.out.println("方法执行前:"+user.getUname());
System.out.println("方法执行前!");
}
// @After("log()") //最终后置通知。不管有无异常,最终都会执行!
private void after(){
System.out.println("方法执行后!");
}
// @AfterReturning("log()") //方法正常结束,后置通知
private void afterReturning(){
System.out.println("方法执行后!after returning!");
}
// @AfterThrowing("log()") //抛出异常会执行,异常通知
private void afterThrowing(){
System.out.println("方法执行后!after throwing!");
}
// @Around("log()") //环绕通知在一个方法执行之前和之后执行
private Object around(ProceedingJoinPoint pjp){
System.out.println("环绕通知,方法执行前!");
Object obj = null;
try {
obj = pjp.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("环绕通知,方法执行后!");
return obj;
}
}