Spring框架(二)

2019-04-01  本文已影响0人  Unclezs

一、AOP

简介

实现原理

手动实现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包
blob.jpg
配置
<?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>
注解配置
    <!--扫描注解包-->
    <context:component-scan base-package="com.unclezs.service"/>
    <!--确认AOP注解生效-->
    <aop:aspectj-autoproxy/>
    <aop:config>
        <aop:aspect ref="myAspect"/>
    </aop:config>
上一篇 下一篇

猜你喜欢

热点阅读