使用Spring进行面向切面(AOP)编程
要进行AOP编程,首先我们要在spring的配置文件中引入aop命名空间:
<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"
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">
</beans>
Spring提供了两种切面声明方式,实际工作中我们可以选用其中一种:
l
基于XML配置方式声明切面。
l
基于注解方式声明切面。
首先启动对@AspectJ注解的支持(蓝色部分):
bean.xml
<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"
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">
<aop:aspectj-autoproxy/>
<bean id="orderservice" class="cn.itcast.service.OrderServiceBean"/>
<bean id="log" class="cn.itcast.service.LogPrint"/>
</beans>
下面我们使用spring注解方式完成aop编程
步骤:
(1).导入依赖jar文件
如果使用了切面编程(AOP),还需要下列jar文件
lib/aspectj/aspectjweaver.jar和aspectjrt.jar
lib/cglib/cglib-nodep-2.1_3.jar
如果使用了JSR-250中的注解,如@Resource/@PostConstruct/@PreDestroy,还需要下列jar文件
lib\j2ee\common-annotations.jar
lib/aspectj/aspectjweaver.jar和aspectjrt.jar
lib/cglib/cglib-nodep-2.1_3.jar
如果使用了JSR-250中的注解,如@Resource/@PostConstruct/@PreDestroy,还需要下列jar文件
lib\j2ee\common-annotations.jar
(2) 在bean.xml中 配置上面的aop支持,并且打开
<aop:aspectj-autoproxy/>
(3) 写接口和实现,方便测试
接口 PersionSevice
package cn.com.xinli.service;
public interface PersionSevice
{
public void save(String name);
public void update(String name, Integer personid);
public String getPersonName(Integer personid);
}
实现 PersionServiceBean
package cn.com.xinli.service.impl;
import org.apache.log4j.Logger;
import cn.com.xinli.service.PersionSevice;
public class PersionServiceBean implements PersionSevice
{
Logger log=Logger.getLogger(PersionServiceBean.class);
public String getPersonName(Integer personid) {
System.out.println("我是getPersonName()方法");
return "xxx";
}
public void save(String name) {
System.out.println("我是save()方法");
}
public void update(String name, Integer personid) {
System.out.println("我是update()方法");
}
}
(4) 建立一个切面类 MyInterceptor (第一次测试的时候我懒得敲代码,直接复制现成的,没注意切面中的方法还有参数
,导致报错:实例化不了一个bean, 结果整了1天才发现原因,教训丫....)
package cn.com.xinli.service;
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;
/**
* 切面
*
*/
@Aspect
public class MyInterceptor
{
/**
* 1.第一个* 号代表 返回值的类型可以为任意,也可以指定比如 String,Int
* 2.cn.itcast.service.impl.PersonServiceBean.. 表示对cn.itcast.service.impl.PersonServiceBean
* 包下及其子包的下都进行拦截(注意是两个.) 如果是一个 . ,那么就拦截这个包下的类,不会去拦截子包
* 3. .. 后面的 *.* 第一个星号代表类,为星号说明对所有的类都拦截,第二个星号代表方法,为星号代表拦截所有的方法
* 4. (..) 代表方法的参数为任意,有也可以,没有也可以
* 5.如果要拦截PersonServiceBean类下所有的业务方法,也可以这么写:
* @Pointcut("execution (* cn.itcast.service.impl.PersonServiceBean*.*(..))")
*
*/
@Pointcut("execution(* cn.com.xinli.service.impl.PersionServiceBean*.*(..))")
private void anyMethod() {}//声明一个切入点
/*对业务方法中只有一个参数的方法实施 前置通知*/
//@Before("anyMethod() && args(name)")
@Before("anyMethod()")
public void doAccessCheck()
{
System.out.println("前置通知");
}
/*在业务方法执行完毕后,后置通知拿到业务的返回值*/
@AfterReturning(pointcut="anyMethod()",returning="result")
//@AfterReturning("anyMethod()")
public void doAfterReturning() {
System.out.println("后置通知");
}
@After("anyMethod()")
public void doAfter() {
System.out.println("最终通知");
}
/*在出现异常的情况下在例外通知中拿到异常*/
@AfterThrowing(pointcut="anyMethod()",throwing="e")
//@AfterThrowing(pointcut="anyMethod()")
public void doAfterThrowing(Exception e) {
System.out.println("例外通知:"+ e);
}
//环绕通知,固定写法,可以实现上面的所有通知
@Around("anyMethod()")
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
//if(){//判断用户是否在权限
System.out.println("进入方法");
Object result = pjp.proceed();
System.out.println("退出方法");
//}
return result;
}
}
(5)
一定要讲业务bean和切面配置在beans.xml中(也可以使用扫描)
<aop:aspectj-autoproxy proxy-target-class="true"/>
<bean id="myInterceptor" class="cn.com.xinli.service.MyInterceptor"></bean>
<bean id="personService" class="cn.com.xinli.service.impl.PersionServiceBean"></bean>
(6) 测试
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
PersionSevice ps=(PersionSevice)ctx.getBean("personService");
ps.save("xxx");
(7)结果:
前置通知
进入方法
我是save()方法
后置通知
最终通知
退出方法
进入方法
我是save()方法
后置通知
最终通知
退出方法