- 导包
spring-aspects-3.2.0.RELEASE.jar
com.springsourse.org.aspectj.weaver-1.6.8.RELEASE.jar 引入AspectJ的配置
xmlns:aop=”http://www.springframework.org/schema/aop”
值得一提的是xmlns是xml namespaces的缩写,xsi是自定义前缀,后接namespaceURIxml文件中的实现方式
<!--Aspectj实现-->
<aop:config>
<!--表示切入点-->
<aop:pointcut id="mypointcut"
expression="public boolean com.bamzhy.service.impl.AccountService.transferMoney(*)"/>
<!--advice-ref表示如何增强,pointcut-ref表示增强谁-->
advice-ref="myadvice"
pointcut-ref="mypointcut">
</aop:config>
注意点:不需要通过id获取代理类了(getBean(“class”)),直接在执行被增强的方法时执行增强。
AspectJ是什么?
- 基于java的AOP框架
- Spring2.0以后支持了基于AspectJ的切入点表达式的支持
- AspectJ扩展了Java语言,提供了一个专门的编译器,在编译时提供横向代码的织入。
- 允许直接在POJO(普通Bean)里边定义切面类
- 主要用途:不改动现有代码的前提下,自定义开发、植入代码。
切入点表达式
- 语法:execution(修饰符 返回值 (包.类.方法名(参数类型) )throws 异常)
- 修饰符:一般省略,可以写public,* 表示任意
- 返回值:void、String、* 表示任意
- 包:> 可以省略,可以使用* 通配(一个*表示一层包)
- 类:可以省略,可以使用* 通配
如果省略的话会降低效率 - throws可以省略
- AspectJ通知类型:
Before:在切入点之前执行。参数类型的校验(前置通知)
AfterReturning:在切入点之后执行(后置通知),可以对结果进行检查,增加log
Around:环绕通知在切入点之前和之后都会执行,例如增加事务等
AfterThrowing:抛出异常的时候执行通知,正常情况下走不到,只有发生异常的情况下才会去通知,记录一些日志。
After:在finally语句里。不管切入点是否有异常发生都会执行。
xml配置:
<!--这里写的是所有增强代码的实现类-->
<bean id="myadvice" class="com.bamzhy.advice.MyPOJOAdvice"/>
<aop:config>
<!--这里注入实现类-->
<aop:aspect ref="myadvice">
<aop:pointcut
<!--等待被增强的对象-->
id="mypointcut"
expression="execution (public void com.bamzhy.service.impl.AccountServiceimpl.transferMoney())"/>
<!--增强代码1-->
<aop:before method="before" pointcut-ref="mypointcut"/>
<!--增强代码2-->
<aop:around method="around" pointcut-ref="mypointcut"/>
<!--增强代码3-->
<aop:after-throwing method="afterThrowing" pointcut-ref="mypointcut" throwing="throwable"/>
<!--增强代码4-->
<aop:after-returning method="afterReturning" pointcut-ref="mypointcut"/>
<!--增强代码5-->
<aop:after method="finallyMethod" pointcut-ref="mypointcut"/>
</aop:aspect>
</aop:config>
MyPOJOAdvice.java中的方法
package com.bamzhy.advice;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
public class MyPOJOAdvice {
public void before(){
System.out.println("before");
}
public void afterReturning(){
System.out.println("after-returnig");
}
//这个方法跟动态代理那里invoke方法很像
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("beforearound");
Object proceed = joinPoint.proceed();
System.out.println("afteraround");
return proceed;
}
public void afterThrowing (JoinPoint j, Throwable throwable){
System.out.println("error" + throwable.getMessage());
}
public void finallyMethod(){
System.out.println("finallyMethod");
}
}
注解方式配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<!--增加context-->
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.xsd">
<!--扫描下包-->
<context:component-scan base-package="com.bamzhy"/>
<aop:aspectj-autoproxy />
</beans>
使用注解完成配置
增强注解的扫描< context:component-scan base-package=”com.bamzhy” >< /context:component-scan>
< aop:aspectj-autoproxy />
1、@Controller web层
2、@Service 服务 service层
3、@Repository dao层
4、@Component取代< bean class=” ” > @Component(“id”) 取代< bean id=” ” >
5、@Value(” “)给普通字段注入值
6、@Autowired 通过其使用来消除 set ,get方法。首先在容器中查询对应类型的bean
,如果查询结果刚好为一个,就将该bean装配给@Autowired指定的数据,如果查询的结果不止一个,那么@Autowired会根据名称来查找。
7、@PostConstruct @PreDestory 取代 init-method=”init” destory-method=”destory”
8、@Resource(name=”myaddress”) @Component(“myaddress”)
(等价于< property name=”address” ref=”myaddress” >)
@Aspect 声明切面,修饰切面类,从而获得增强
Advice中的:
@Before(“myPointCut”)前置
@AfterReturning(“myPointCut”) 后置
@Around(“myPointCut”)环绕
@AfterThrowing(pointcut = “myPointCut()”,throwing = “throwable”)抛出异常
@After(“myPointCut”)最终
切入点:
@PointCut(里边写要增强的方法的全类名+方法名) 自己在Advice类里边定义一个方法
private void myPointCut(){}