继上篇spring IOC,又学习了,spring AOP,同样是注解模式:
面向切面:
import com.pac;//1.包名--不可变
Class className{//2.类名--不可变
//3.返回类型--不可变
//4.方法名称--不可变
//5.入参类型--不可变
//6.入参值--可变
//7.异常--可捕获
//8.返回值--可变
public T getDate(D inData)throws Exception{
return T;
}
}
面向方法的切面:
执行前:对入参D inData或者调用者进行处理
执行后:对返回值T 或者调用者进行处理
异常处理:对获取的异常或调用者进行处理
执行之中:对调用者,入参,返回值,以及异常进行处理--很强大,其实直接用这个就行。
小例子:
1.有一个业务,很简单,对传入的参数进行处理,然后返回
package com.aop.service; import org.springframework.stereotype.Component; import com.aop.bo.InputVal; import com.aop.bo.ReturnVal; @Component public class ServiceImpl { public ReturnVal getData(InputVal val) throws Exception{ ReturnVal rtVal = new ReturnVal(); //模拟处理 rtVal.setKey(val.getKey()+"处理后"); rtVal.setValue(val.getValue()+"处理后"); System.out.println("------------执行中-------------"); //模拟异常 // if(val.getKey().equals("ex")){ // throw new Exception("异常!"); // } return rtVal; } public String toString() { return "ServiceImpl调用者"; } }
2.入参数据:
package com.aop.bo; public class InputVal { private String key = null; private String value = null; public String getKey() { return key; } public void setKey(String key) { this.key = key; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } @Override public String toString() { return "InputVal [key=" + key + ", value=" + value + "]"; } }
3.出参数据:
package com.aop.bo; public class ReturnVal { private String key = null; private String value = null; public String getKey() { return key; } public void setKey(String key) { this.key = key; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } @Override public String toString() { return "ReturnVal [key=" + key + ", value=" + value + "]"; } }
4.对业务进行切面:
package com.aop.aop; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; import com.aop.bo.InputVal; import com.aop.bo.ReturnVal; @Component @Aspect public class AopService { // @Pointcut("execution(* com.aop.service.ServiceImpl.getData(..))") // private void cut(){} private final String CUT = "execution(* com.aop.service.ServiceImpl.getData(..))"; /* //方法执行前调用 @Before(CUT) public void before(){ System.out.println("-----------执行前--------------------"); } //方法执行后调用 @After(CUT) public void after() { System.out.println("------------执行后------"); } */ @Around(CUT) //spring中Around通知 环绕通知 public Object logAround(ProceedingJoinPoint joinPoint) { Object tarjet = joinPoint.getTarget();//1.调用者 //-----日志,调用者 System.out.println("1.调用者:------"+tarjet); Object[] args = joinPoint.getArgs();//2.传参 System.out.println("2.传参:----"+args[0]); if(args[0] instanceof InputVal){ InputVal val = (InputVal) args[0]; val.setKey(val.getKey()+"改了么?"); } Object obj = null; try { obj = joinPoint.proceed(args); if(obj.getClass().equals(ReturnVal.class)){ ReturnVal rval = (ReturnVal) obj; rval.setValue(rval.getValue()+"改了?"); } } catch (Throwable e) { e.printStackTrace(); } System.out.println("3.返回参数:---"+obj);//3.返回参数 return obj; } }
5.调用测验:
package com.aop.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.aop.bo.InputVal; import com.aop.service.ServiceImpl; public class Main { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml"); ServiceImpl si = (ServiceImpl) ctx.getBean("serviceImpl"); InputVal val = new InputVal(); val.setKey("inKey"); val.setValue("inVal"); System.out.println(si.getData(val)); } }
6.配置文件,要打开aop注解扫描:
<?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" 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"> <!-- 扫描com.ioc下的所有类,自动为spring容器管理 --> <context:component-scan base-package="com"/> <!-- 打开面向切面工具 --> <aop:aspectj-autoproxy/> </beans>