코드 플러스 원래의 프로그램이 유지되어서는 안된다, 혼란 코드에 솔루션을 이끌 경우 프로그램, 로그 및 기타 요구 사항을 요구한다 :
- 프록시를 사용합니다.
- AOP 스프링의 사용.
AOP는 방법의 봄을 주석에 :
1. 추가 항아리 패키지 (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 *. *. * (...))") 공공 무효 전에 (joinpoint를의 joinpoint를) { 개체 []의 인수에 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.最后去测试