首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

spring aop 常例使用

2013-11-16 
spring aop 常规使用package main.daoImplimport org.aspectj.lang.JoinPointpublic class LogAspect {p

spring aop 常规使用
package main.daoImpl;import org.aspectj.lang.JoinPoint;public class LogAspect {public void before(JoinPoint jp) {System.out.println("Args:" + jp.getArgs());System.out.println("Kind:" + jp.getKind());System.out.println("Signature:" + jp.getSignature());System.out.println("Target:" + jp.getTarget());System.out.println("this:" + jp.getThis());System.out.println("正在进行权限校验!before...");}public void after() {System.out.println("结束调用!after...");}public void around() {System.out.println("环绕! around...");}public void doThrow() {System.out.println("抛异常!throw...");}}

?2 定义一个目标类:

package main.daoImpl;import main.dao.TestDao;public class TestDaoImpl implements TestDao{public void test() {System.out.println("test...");System.out.println("test");}}

目标类实现TestDao接口,TestDao接口有一个抽象方法test()

?

3 在spring的配置文件中配置如下:

  <!-- 定义切面类和目标类 -->  <bean id="logAspect" ref="logAspect">        <!-- 配置切入点 -->          <aop:pointcut id="logPointCut"              expression="execution(* main.daoImpl.*.*(..))" />          <!-- 定义通知和连接点 -->        <aop:before pointcut-ref="logPointCut" method="before"/>          <aop:after pointcut-ref="logPointCut" method="after"/>          <aop:around pointcut-ref="logPointCut" method="around"/>          <aop:after-throwing pointcut-ref="logPointCut" method="doThrow" throwing="ex"/>      </aop:aspect>     </aop:config> 

4 测试代码:

@Testpublic void test() {try {TestDao testDaoImpl = (TestDao)context.getBean("testDaoImpl"); System.out.println("===============" + testDaoImpl + "=======================");testDaoImpl.test();} catch (Exception e) {e.printStackTrace();} finally {}}

?测试结果:

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
===============main.daoImpl.TestDaoImpl@1d5a0=======================
Args:[Ljava.lang.Object;@14b5f4a
Kind:method-execution
Signature:void main.dao.TestDao.test()
Target:main.daoImpl.TestDaoImpl@1d5a0
this:main.daoImpl.TestDaoImpl@1d5a0
正在进行权限校验!before...
环绕! around...
结束调用!after...

二 基于Annotation的aop使用:

目标类还使用上面的TestDaoImpl.java;

1 定义切面类

package main.daoImpl;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;@Aspectpublic class LogAnnotationAspect {@Pointcut("execution(* main.daoImpl.*.*(..))")public void allMethod() {}@Before("allMethod()")public void before(JoinPoint jp) {System.out.println("Args:" + jp.getArgs());System.out.println("Kind:" + jp.getKind());System.out.println("Signature:" + jp.getSignature());System.out.println("Target:" + jp.getTarget());System.out.println("this:" + jp.getThis());System.out.println("正在进行权限校验!before...");}@After("allMethod()")public void after() {System.out.println("结束调用!after...");}@Around("allMethod()")public void around() {System.out.println("环绕! around...");}@Around("allMethod()")public void doThrow() {System.out.println("抛异常!throw...");}}

?2 spring配置文件中配置:

  <!-- 启动对aspectJ注解的支持 -->  <aop:aspectj-autoproxy/>  <!-- 定义切面类,目标类 -->  <bean id="logAnnotationAspect" alt="spring aop 常例使用" src="/img/2013/11/16/22293981.png">
    package main.aop;public interface Target {public void target();}
?2 目标类:public class TargetA implements Target {public void target() {System.out.println("targetA is executing!");}}?3 前置通知类:public class BeforeAdvice implements MethodBeforeAdvice {public void before(Method method, Object[] arg1, Object arg2)throws Throwable {System.out.println(method);for (Object obj : arg1) {System.out.println(obj);}System.out.println(arg1);System.out.println(arg2);}}?4 spring配置: <!-- 定义目标类和前置通知类--> <bean id="targetA" name="code">@Testpublic void test() throws URISyntaxException {Target targetA = (Target)context.getBean("targetA");targetA.target();}?测试结果:本人感觉自动代理不如自己配置或注解的aop切面编程灵活和强大,所以暂时写一个自动代理的demo,如有需要的时候继续深入!

??

热点排行