Spring AOP示例
一.使用Annotation
1.定义切面
package com.yyj.aspect;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class TestAspect {
@Pointcut("execution(* com.yyj.service.*.*(..))")
public void testPointCut(){
}
}
注: @Aspect说明该类是个切面,@Component将该bean自动注入到springIOC容器,
默认id为类名第一个字母转小写;
@Pointcut(“execution(* com.yyj.service..(..))”)定义一个切入点,
后面的表达式说明对com.yyj.service包及子包下的所有方法进行拦截,植入其它操作,
其它操作由对应的Advise定义。
2.定义advise
package com.yyj.aspect;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class TestAdvise {
@Before("com.yyj.aspect.TestAspect.testPointCut()")
private void testBefore(){
System.out.println("begin-----in-----");
System.out.println("before test...");
System.out.println("end -----in-----");
}
}
注: @Before(“com.yyj.aspect.TestAspect.testPointCut()”),定义了testPointCut这个切入点
要做的操作是在执行被切入方法之前先执行testBefore方法。
3.配置
<?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"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/langhttp://www.springframework.org/schema/lang/spring-lang-3.0.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-3.0.xsd">
<aop:aspectj-autoproxy/>
<!-- 启动Spring注解自动注入Bean -->
<context:component-scan base-package="com.yyj" />
</beans>
4.定义测试类
package com.yyj.service;
import org.springframework.stereotype.Component;
@Component
public class Testa{
public void test(){
System.out.println("test...");
}
}
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.yyj.service.Testa;
public class MainTest {
private static ClassPathXmlApplicationContext springCtx;
synchronized static void startup() {
if (springCtx == null) {
System.out.println("start up...");
springCtx = new ClassPathXmlApplicationContext();
springCtx
.setConfigLocations(new String[] { "applicationContext.xml" });
springCtx.refresh();
Testa ta = (Testa)springCtx.getBean("testa");
ta.test();
System.out.println("start up...ok");
}
}
public static void main(String[] args) {
new MainTest().startup();
}
}
二.不使用Annotation注解也可以如下类似配置
<aop:aspectj-autoproxy/>
<!-- 启动Spring注解 -->
<context:component-scan base-package="com.yyj" />
<!--<tx:annotation-driven proxy-target-class="true" /> -->
<aop:config>
<aop:aspect ref="myPoint">
<aop:pointcut expression="execution(* com.yyj.service.FooService.getFoo(..))" id="pointCut"/>
<aop:before pointcut-ref="pointCut" method="myAroundMethod"/>
</aop:aspect>
</aop:config>
注:PointCut和Advise可以在一个类中,也可以分开定义