Spring切面的定义有四种:
一,基于Annotation也就是@AspectJ的注解方式
二,基于<aop:aspect>的方式
三,基于<aop:advisor>的方式
四,基于Advisor类的方式
注:第四种一般不用,其他的看情况用
先看第一种基于Annotation的:
beans.xml(此处要加aop和context的命名空间的名字和location,然后加那三个配置,第一个是基于annotation,第二个是自动检测组件,第三个就是aspectj的支持)
<?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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <context:annotation-config /> <context:component-scan base-package="com.lbx"/> <aop:aspectj-autoproxy /> </beans>
AOP就类似拦截器,使用AOP就可以轻松的处理很多的东西,例如:事务的处理,权限的处理,日记的处理,统计等
下面就是一个Advice(增强)(其中@Aspect表示是一个切面,@Pointcut是一个切点,相当于一个查询条件,@Before是一个前置增强,@After是一个后置增强)
package com.lbx.aop;
import org.aspectj.lang.annotation.AfterReturning;
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 {
@Pointcut("execution(public * com.lbx.dao..*.*(..))")
public void getMethod(){}
@Before("getMethod()")
public void before(){
System.out.println("LogInterceptor.before()");
}
//@After("getMethod()")
//@AfterReturning("getMethod()")
@AfterReturning("getMethod()")
public void afterReturning(){
System.out.println("LogInterceptor.afterReturning()");
}
}
测试方法没写,只要是满足那个@Pointcut条件的都可以进行增强
第二种基于<aop:aspect>的方式
bean.xml文件配置(把要植入的那个Advice加一个bean,然后使用<aop:aspect>来配置)
<?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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <context:annotation-config /> <context:component-scan base-package="com.lbx"/> <bean id="logInterceptor" class="com.lbx.aop.LogInterceptor"></bean> <aop:config> <aop:pointcut expression="execution(public * com.lbx.dao..*.*(..))" id="servicePointCut"/> <aop:aspect id="logAspect" ref="logInterceptor"> <aop:before method="before" pointcut-ref="servicePointCut"/> <aop:after method="afterReturning" pointcut-ref="servicePointCut"/> </aop:aspect> </aop:config> </beans>
Advice代码
package com.lbx.aop;
public class LogInterceptor {
public void getMethod(){}
public void before(){
System.out.println("LogInterceptor.before()");
}
public void afterReturning(){
System.out.println("LogInterceptor.afterReturning()");
}
}
测试没写(效果和上面的一样)