コードに加えて、元のプログラムは、維持すべきではない、混乱コードにソリューションをリードする場合、プログラムは、ログおよびその他のニーズを必要とします。
- プロキシを使用してください。
- AOPスプリングの使用。
AOPは方法の春を注釈します:
1.追加のjarパッケージ(com.springsource.org.aopalliance、sapectj.weaver、春-AOP)
2.セクションクラス(アスペクト)を作成します。
パッケージcom.zhiyou100.kfs.aspects。
輸入org.aspectj.lang.JoinPoint。 輸入org.aspectj.lang.annotation.After。 輸入org.aspectj.lang.annotation.AfterReturning。 輸入org.aspectj.lang.annotation.Aspect; 輸入org.aspectj.lang.annotation.Before; 輸入org.springframework.stereotype.Component。
@Aspect //セクション @Component //豆 パブリッククラスLogAspects { / * *最初*:任意のアクセス修飾子と任意の戻り値をワイルドカード * *第二:クラス *第三*:方法 * ..:変数パラメータ * / @Before(値= "実行(* com.zhiyou100.kfs.aspects。*。*(..))") (ジョインポイントのジョインポイント)前ます。public void { オブジェクト[]引数の=のjoinpoint.getArgs(); //参数 String name = joinpoint.getSignature().getName();//方法名字 System.out.println("begin,method:"+name); } @AfterReturning @After(value="execution(* com.zhiyou100.kfs.aspects.*.*(..))") //方法执行return前执行 public void after(JoinPoint joinpoint) { String name = joinpoint.getSignature().getName();//方法名字 System.out.println("end,method:"+name+",result:"); } } |
3.在spring配置文件开启切面注解
<!-- 包扫描 --> <context:component-scan base-package="com.zhiyou100.kfs"/> <!-- 开启切面注解 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> |
4.测试代码
ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml"); TestProxy t=(TestProxy)app.getBean("testProxyImp"); t.add(1, 8,1); |
spring的AOPxml方式使用:
1.创建一个需要被通知的接口和相应实现类
接口:
package com.zhiyou100.kfs.aspects.xml;
public interface TestProxy { public void add(int a,int b); public int add(int a,int b,int c); } |
相应实现类:
package com.zhiyou100.kfs.aspects.xml;
public class TestProxyImp implements TestProxy {
public void add(int a,int b){ int sum=a+b; System.out.println(sum); throw new RuntimeException("运行时异常"); }
public int add(int a,int b,int c){ int sum=a+b+c; System.out.println(sum); return sum; } } |
2.在spring配置文件中配置。
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<!-- 定义被通知的程序类(可以用注解) --> <bean id="testProxyImp" class="com.zhiyou100.kfs.aspects.xml.TestProxyImp"></bean> <!-- 定义切面类的bean --> <bean id="logAspect" class="com.zhiyou100.kfs.aspects.xml.LogAspects"></bean> <!-- 配置切面 --> <aop:config> <!-- 定义表达式:切点 --> <aop:pointcut expression="execution(* com.zhiyou100.kfs.aspects.xml.*.*(..))" id="pointcut"/> <!-- 定义切面 --> <aop:aspect ref="logAspect"> <!-- 定义前置通知 --> <aop:before method="before" pointcut-ref="pointcut"/> <!-- 定义后置通知 --> <aop:after method="after" pointcut-ref="pointcut"/> <!-- 定义返回通知 --> <aop:after-returning method="cc" pointcut-ref="pointcut" returning="result"/> <!-- 定义异常通知 --> <aop:after-throwing method="e" pointcut-ref="pointcut" throwing="e"/> </aop:aspect> </aop:config> </beans> |
3.最后去测试