<?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>