Spring aop
概念:Springaop 就是将公共的业务(如日志,安全等)和领域业务结合,当执行领域业务时,将会把公共业务加进来,实现公共业务的重复利用,领域业务更加纯粹,程序员专注于业务,其本质还是动态代理
第一种:实现aop业务的相应接口
公共业务
package log;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice; /** 通过这种方式,需要实现对应的接口,然后重写相应的方法 一般方法分为before,after,around等 其实这些方法所不同之处就是加在领域业务方法的时候,执行顺序的不同 比如before就是在所加进去的方法之前所执行的 After就是在方法之后所执行 等等 */ publicclass After implements AfterReturningAdvice{
publicvoid afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { System.out.println(target.getClass().getName()+"的"+method.getName()+"被成功执行,返回值是:"+returnValue); } } package log;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
publicclass Before implements MethodBeforeAdvice{
@Override publicvoid before(Method method, Object[] args, Object target) throws Throwable { System.out.println(target.getClass().getName()+"的"+method.getName()+"方法被执行"); } }
|
接口
package service;
publicinterface Service {
public String add(); publicvoid delete(); }
|
接口的实现类
package service;
publicclass ServiceImpl implements Service{
@Override public String add() { System.out.println("增加用户"); return"爱你一万年"; }
@Override publicvoid delete() { System.out.println("删除用户"); }
}
|
测试类
package test;
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.Service;
public class Test {
public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml"); Service service = (Service) ac.getBean("service"); service.add();
} } |
Spring 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" 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.xsd"> <!—将三个类纳入到ioc容器当中 --> <bean id="service" class="service.ServiceImpl"/> <bean id="After" class="log.After"/> <bean id="Before" class="log.Before"/> <!—使用aop将公共业务和领域业务相关联--> <aop:config> <!—切入目标--> 返回值 包 类 方法 参数 <aop:pointcut expression="execution(* service.*.*(..))" id="pointcut"/> <!—执行切入,代表将After中的方法切入进去--> <aop:advisor advice-ref="After" pointcut-ref="pointcut"/> <aop:advisor advice-ref="Before" pointcut-ref="pointcut"/> </aop:config> </beans> |
第二种:自定义类实现aop
公共业务
package com.ss.log;
/** *切面 自定义是实现 不用再实现接口 */ publicclass Log {
publicvoid before(){ System.out.println("方法之前被调用"); } publicvoid after(){ System.out.println("方法之后调用"); } }
|
领域业务
package com.ss.service;
publicinterface Service {
publicvoid add(); publicvoid delete(); }
package com.ss.service;
publicclass ServiceImpl implements Service{
@Override publicvoid add() { System.out.println("主类增加add方法"); }
@Override publicvoid delete() { System.out.println("主类增加delete方法"); }
} |
测试
package com.test;
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ss.service.Service;
public class Test {
public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml"); Service service = (Service)ac.getBean("service"); service.add(); } } |
配置文件:
<?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.xsd"> <bean id="service" class="com.ss.service.ServiceImpl"/> <bean id="log" class="com.ss.log.Log"/> <aop:config> <!—是将id=“log”的这个类切入--> <aop:aspect ref="log"> <!—切入目标--> <aop:pointcut expression="execution(* com.ss.service.ServiceImpl.*(..))" id="pointcut"/> <!—具体的某个方法切入--> <aop:before method="before" pointcut-ref="pointcut"/> <aop:after method="after" pointcut-ref="pointcut"/> </aop:aspect>
</aop:config> </beans> |
注意:当不在采用接口的时候,就必须得依赖于配置文件当中所提供的方法
结果:
采用注解开发
公共代码
package com.log;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before;
@Aspect publicclass log {
@Before("execution(* com.service.*.*(..))") publicvoid add(){ System.out.println("方法之前执行"); } @After("execution(* com.service.*.*(..))") publicvoid delete(){ System.out.println("方法之后执行"); } @Around("execution(* com.service.*.*(..))") public Object aroud(ProceedingJoinPoint jp) throws Throwable{ System.out.println("环绕前"); System.out.println("签名:"+jp.getSignature()); //执行目标方法 Object result = jp.proceed(); System.out.println("环绕后"); returnresult; } }
|
领域业务
package com.service;
publicinterface Service {
publicvoid add(); publicvoid delete(); }
package com.service;
publicclass ServiceImpl implements Service{
@Override publicvoid add() { System.out.println("增加方法"); }
@Override publicvoid delete() { // TODO Auto-generated method stub System.out.println("删除"); }
}
|
测试
package com.test;
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.service.Service;
publicclass Test {
publicstaticvoid main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml"); Service service = (Service) ac.getBean("service"); service.add();
} }
|
配置文件代码
<?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.xsd"> <bean id="service" class="com.service.ServiceImpl"/> <bean id="log" class="com.log.log"/> <!-- 采用注解须配置 --> <aop:aspectj-autoproxy/> </beans> |