Spring框架(二)
2019-04-01 本文已影响0人
Unclezs
一、AOP
简介
- 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。
- AOP是OOP(面向对象编程)的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。
- 利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
- AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码
- 经典应用:事务管理、性能监视、安全检查、缓存 、日志等【画图】
- Spring AOP使用纯Java实现,不需要专门的编译过程和类加载器,在运行期通过代理方式向目标类织入增强代码
- AspectJ是一个基于Java语言的AOP框架,Spring2.0开始,Spring AOP引入对Aspect的支持,AspectJ扩展了Java语言,提供了一个专门的编译器,在编译时提供横向代码的织入
实现原理
- aop底层将采用代理机制进行实现。
- 接口 + 实现类 :spring采用 jdk 的动态代理Proxy。
- 没有接口的类与有接口的实现类:spring 采用 cglib字节码增强。
手动实现AOP式例
切面增强类MyApect
public class MyAspect {
public void before(){
System.out.println("增强头。。");
}
public void after(){
System.out.println("增强尾。。");
}
}
UserServiceImpl类
public interface UserService {
public void add();
}
public class UserServiceImpl implements UserService {
@Override
public void add() {
System.out.println("hello aop");
}
}
AOP实现类
public class MyBeanFactory {
//cglib实现AOP
public static UserService createUserService(){
Enhancer enhancer=new Enhancer();
enhancer.setSuperclass(new UserServiceImpl().getClass());
enhancer.setCallback(new MethodInterceptor() {
@Override
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
MyAspect aspect=new MyAspect();
aspect.before();
Object o=methodProxy.invokeSuper(proxy,args);
aspect.after();
return o;
}
});
UserServiceImpl serviceProxy= (UserServiceImpl) enhancer.create();
return serviceProxy;
}
//jdk动态代理实现AOP
public static UserService createUserServices(){
final UserServiceImpl userService=new UserServiceImpl();
UserService serviceProxy= (UserService) Proxy.newProxyInstance(userService.getClass().getClassLoader(), userService.getClass().getInterfaces(), new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
MyAspect aspect=new MyAspect();
aspect.before();
Object res=method.invoke(userService,args);
aspect.after();
return res;
}
});
return serviceProxy;
}
}
测试类
@Test
public void testAOP() throws InstantiationException, IllegalAccessException {
UserService userService = MyBeanFactory.createUserService();
userService.add();
}
Spring实现半自动AOP
切面类MyAspect2
public class MyAspect2 implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("begin....");
Object o=methodInvocation.proceed();
System.out.println("end.......");
return o;
}
}
bean.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userService" class="com.unclezs.service.impl.UserServiceImpl"/>
<bean id="myAspect" class="com.unclezs.service.impl.MyAspect2"/>
<bean id="serviceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="interfaces" value="com.unclezs.service.UserService"/>
<property name="target" ref="userService"/>
<property name="interceptorNames" value="myAspect"></property>
<!--<property name="optimize" value="true"/>-->
</bean>
</beans>
Spring实现全自动AOP
需要jar包

配置
<?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">
<!--开启注解-->
<!--<context:annotation-config/>-->
<!--配置作用范围-->
<!--<context:component-scan base-package="com.unclezs"/>-->
<bean id="userService" class="com.unclezs.service.impl.UserServiceImpl"/>
<bean id="myAspect" class="com.unclezs.aspect.MyAspect2"/>
<!--半自动AOP-->
<!--<bean id="serviceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">-->
<!--<property name="interfaces" value="com.unclezs.service.UserService"/>-->
<!--<property name="target" ref="userService"/>-->
<!--<property name="interceptorNames" value="myAspect"></property>-->
<!--<!–<property name="optimize" value="true"/>–>-->
<!--</bean>-->
<!--全自动AOP-->
<aop:config proxy-target-class="true">
<aop:pointcut id="myPointcut" expression="execution(* com.unclezs.service.*.*(..))"/>
<aop:advisor advice-ref="myAspect" pointcut-ref="myPointcut"/>
</aop:config>
</beans>
expresstion说明
参数 包名.类名.方法名(参数列表)
eg: * com.unclez..(..)
AspectJ配置
XML配置
<aop:config proxy-target-class="true">
<aop:aspect ref="myAspect">
<aop:pointcut id="myCut" expression="execution(* com.unclezs.service.*.*(..))"/>
<aop:before method="before" pointcut-ref="myCut"/>
<aop:after-returning method="after" pointcut-ref="myCut" returning="rev"/>
<aop:around method="around" pointcut-ref="myCut"/>
<aop:around method="around" pointcut-ref="myCut"/>
<aop:after-throwing method="around" pointcut-ref="myCut" throwing="e"/>
</aop:aspect>
</aop:config>
注解配置
- @Aspect 声明切面,修饰切面类,从而获得 通知。
- @Before 前置
- @AfterReturning 后置
- @Around 环绕
- @AfterThrowing 抛出异常
- @After 最终切入点
- @PointCut ,修饰方法 private void xxx(){} 之后通过“方法名”获得切入点引用
xml中
<!--扫描注解包-->
<context:component-scan base-package="com.unclezs.service"/>
<!--确认AOP注解生效-->
<aop:aspectj-autoproxy/>
<aop:config>
<aop:aspect ref="myAspect"/>
</aop:config>