Spring底层代理-----

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       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="before" class="cn.bdqn.service.Advice.BeforeAdvice"/>
    <bean id="userService" class="cn.bdqn.service.impl.UserServiceImpl"/>
    <bean id="after" class="cn.bdqn.service.Advice.AfterAdvice"/>
    <bean id="around" class="cn.bdqn.service.Advice.AroundAdvice"/>
    <!--配置异常目标duixiang-->
    <bean id="userException" class="cn.bdqn.service.UserPackage.UserServiceImpl"/>
    <bean id="myException" class="cn.bdqn.service.Advice.ExceptionAdvice"/>

    <bean id="uException" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="targetName" value="userException"/>
        <property name="interceptorNames">
            <array>
                <value>uException</value>
            </array>
        </property>
        <!--代理类的优化  是否使用cglib动态代理-->
        <property name="optimize" value="true"/>
        <!-- <property name="proxyTargetClass" value="true"/>
        proxyTargetClass:默认是false  ,默认执行jdk动态代理!
                          设置成true,强制执行cglib!
        optimize :  代理类的优化
                   有接口就是用jdk,没有接口使用cglib动态代理
        -->
    </bean>
    <!--
         我们的动态代理 (在程序运行期间,动态生成的代理类) 分为两种方式:
            01.jdk     只能应用于实现接口的情况
            02.cglib   应用于实现接口和类的情况
            如果我们是接口的情况,使用jdk效率高!
            如果我们是类的情况,必须使用cglib!
           问题?
              程序  spring容器怎么知道我们是用的类还是接口??
             public class ProxyConfig implements Serializable
               private boolean proxyTargetClass = false;
               private boolean optimize = false;
               spring底层默认使用cglib! 现在我们的项目中使用的是接口!
               用spring默认的性能不高!
              proxyTargetClass 和optimize都是用来设置 我们使用的代理模式是jdk还是cglib!
              @Override
       public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
         //  根据我们配置文件中 proxyTargetClass 和 optimize的配置
           if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {
               Class<?> targetClass = config.getTargetClass();
               if (targetClass == null) {
                   throw new AopConfigException("TargetSource cannot determine target class: " +
                           "Either an interface or a target is required for proxy creation.");
               }
              //根据目标对象返回对应的动态代理
               if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {
                   return new JdkDynamicAopProxy(config);
               }
               return new ObjenesisCglibAopProxy(config);
           }
           else {
               return new JdkDynamicAopProxy(config);
           }
       }
       -->


    <bean id="userProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="targetName" value="userService"/>
       <!-- <property name="interceptorNames" value="before"/>-->
        <property name="interceptorNames" >
            <array>
                <value>before</value>
                <value>after</value>
                <value>around</value>
            </array>
        </property>

    </bean>
</beans>
原文地址:https://www.cnblogs.com/laosunlaiye/p/7679421.html