1.动态代理
我们知道Spring是用来处理业务逻辑的,也就是Service层的服务。但是,当我们所定义的业务逻辑已经无法满足我们的需求的时候,就需要用动态代理的方式来解决。
所以说,动态代理就是用来补充或者是修改我们的业务需求的。
我们来写一个有关于eat的demo。
首先,跟之前一样,建项目,导包
然后建立service接口和实现类
package service.impl; import service.EatService; public class EatServiceImpl implements EatService { @Override public void eat() { // TODO Auto-generated method stub System.out.println("可以吃饭了"); } }
然后我们写一个动态代理的方法来测试一下。
package demo; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import org.junit.Test; import service.EatService; import service.impl.EatServiceImpl; public class TestProxy { @Test public void testProxy(){ EatService proxy=(EatService) Proxy.newProxyInstance(EatServiceImpl.class.getClassLoader(), EatServiceImpl.class.getInterfaces(), new InvocationHandler() { @Override public Object invoke(Object arg0, Method method, Object[] arg2) throws Throwable { // TODO Auto-generated method stub //前置: System.out.println("要洗手"); EatServiceImpl impl = new EatServiceImpl(); Object result = method.invoke(impl); //后置 System.out.println("洗碗"); return result; } }); proxy.eat(); } }
我们来分析一下这段代码:
Proxy.newProxyInstance(EatServiceImpl.class.getClassLoader(), EatServiceImpl.class.getInterfaces(), new InvocationHandler()
Proxy是jdk给我们提供的一个代理类,而newProxyInstance这个方法就是为了创建一个这个代理类的对象的,再看看这个方法的参数,EatServiceImpl.class.getClassLoader()就是通过加载类的方法获取EatService的实现类。EatServiceImpl.class.getInterfaces()就是通过获取接口的方法获取当前接口UserService。再看看这个,new InvocationHandler() 这是一个反射执行器,它需要重写invoke()方法,为啥呢?因为动态代理对象需要自己定义实现逻辑。我们看invoke()方法的参数列表,有一个Method类型的参数,它是通过反射机制来实现的动态代理的,也就是说,通过method.invoke(impl)这种方式就可以实现。
我们来测试一下。看结果是啥:
2.AOP
(1)为什么要学AOP?
可以在不修改源代码的前提下,对业务进行增强!
(2)怎么用?
用户自己开发只记得业务类和业务方法
用户可以开发自己的业务逻辑并放在一个Advice类中,
在配置文件中进行相应配置,告诉AspectJ(AOP提供的面向切面编程框架),为哪些业务类增强了哪些方法。
(3)写个demo
a.建立项目,导包
处了6个基本包,还有2个有关AOP的包
* spring-aop-4.2.4.RELEASE.jar
* com.springsource.org.aopalliance-1.0.0.jar
2个关于AspectJ的开发包
* com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
* spring-aspects-4.2.4.RELEASE.jar
b.写service接口和实现类
package service.impl; import service.UserService; public class UserServiceImpl implements UserService { @Override public void aboutAop() { // TODO Auto-generated method stub System.out.println("调用dao层的方法处理操作"); } }
c.写增强类Advice
package advice; public class UserServiceAdvice { //前置增强方法 public void beforeAdvice(){ System.out.println("在aboutAop方法前增强"); } //后置增强 public void afterAdvice(){ System.out.println("在aboutAop方法后增强"); } }
d.写配置文件applicationContext.xml
<?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="userService" class="service.impl.UserServiceImpl"></bean> <bean id="advice" class="advice.UserServiceAdvice"></bean> <!--Aop的配置--> <aop:config> <!--配置切面 --> <aop:aspect ref="advice"> <!--配置切点--> <aop:pointcut expression="execution(* service.*.*(..))" id="point"/> <!--切点的前置增强--> <aop:before method="beforeAdvice" pointcut-ref="point"/> <!--切点的后置增强--> <aop:after method="afterAdvice" pointcut-ref="point"/> </aop:aspect> </aop:config> </beans>
这里需要解释一下:
首先,需要引入具体的AOP的schema约束。
关于AOP的配置,首先需要配置切面,因为我们需要知道需要用的的是哪一个增强类,也就是这段ref="advice",然后需要配置切点,expression就是表达式的意思,这句"execution(* service.*.*(..))",service这的是这个项目src根目录下,以service这个包为根目录下面的所有内容,"execution(* service.*.*(..))" 这个.*指的是这个service包下面各个子包的内容,"execution(* service.*.*(..))"这个.*指的是子包下面的所有类,"execution(* service.*.*(..))"这个..指的是这个类里面的各个方法。最后需要配置增强方式,是前置增强还是后置增强等等,pointcut-ref="point"指的是,需要引用这个切入点表明在哪里增强。
f.测试类
package demo; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import service.UserService; public class TestAop { @Test public void testAop(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService=(UserService) context.getBean("userService"); userService.aboutAop(); } }
g.结果
关于AOP的一个问题
为什么我们调用Service层的业务类,SpringAOP会帮我们增强了呢?
因为通过配置AOP制定了切面、切点以及切面增强类。
Spring提供了拦截器拦截了切点配置里的类中的方法
当我们运行service层的方法时,拦截器拦截了这个请求,通过动态代理机制为这个service创建了一个动态代理对象,
通过invoke()方法来实现,先调用了前置增强的方法,在调用原有业务类的方法,在调用后置增强方法。