Spring AOP的几种实现方式

本文介绍了Spring中三种AOP配置方式:ProxyFactoryBean、BeanNameAutoProxyCreator及<aop:config>标签,并对其原理进行了分析。ProxyFactoryBean虽然功能强大但配置繁琐;BeanNameAutoProxyCreator通过bean名称自动创建代理;而<aop:config>标签则提供了一种简洁且功能全面的配置方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Spring中AOP有几种配置方式,根据我对spring源码的浏览,发现几种实现方式原理如下:

1. ProxyFactoryBean

   

   

<bean name="myController" class="org.springframework.aop.framework.ProxyFactoryBean">
	<property name="interceptorNames">
		<list>
			<value>pointcut.advisor2</value>
			<value>pointcut.advisor1</value>
			<value>myRawController</value>
		</list>
	</property>
</bean>

 

 

    这个属于最费力不讨好类型的,配置起来很麻烦。原理是根据spring的获取bean的方式,继承了FactoryBean接口的bean在取bean的时候会调用对应的bean class的getObject方法。下面是ProxyFactoryBean的getObject方法:

 

 public Object getObject()
        throws BeansException
    {
        initializeAdvisorChain();
        if(isSingleton())
            return getSingletonInstance();
        if(targetName == null)
            logger.warn("Using non-singleton proxies with singleton targets is often undesirable. Enable prototype proxies by setting the 'targetName' property.");
        return newPrototypeInstance();
    }

private synchronized Object newPrototypeInstance()
    {
        if(logger.isTraceEnabled())
            logger.trace((new StringBuilder("Creating copy of prototype ProxyFactoryBean config: ")).append(this).toString());
        ProxyCreatorSupport copy = new ProxyCreatorSupport(getAopProxyFactory());
        TargetSource targetSource = freshTargetSource();
        copy.copyConfigurationFrom(this, targetSource, freshAdvisorChain());
        if(autodetectInterfaces && getProxiedInterfaces().length == 0 && !isProxyTargetClass())
            copy.setInterfaces(ClassUtils.getAllInterfacesForClass(targetSource.getTargetClass(), proxyClassLoader));
        copy.setFrozen(freezeProxy);
        if(logger.isTraceEnabled())
            logger.trace((new StringBuilder("Using ProxyCreatorSupport copy: ")).append(copy).toString());
        return getProxy(copy.createAopProxy());
    }

 

   于是,通过这种方式实现了bean的代理。不过这种方式的缺点也是显而易见的,那就是配置起来相当麻烦。

 

2.  BeanNameAutoProxyCreator  

 

 

    配置方式如下:

  

<bean id="userService" class="com.aop.service.UserService"/>
<bean id="beforeAdvice" class="com.aop.advice.BeforeAdvice"/>
<bean id="xxxxxx" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property value="beanNames">
               <list>
                      <value>*service</value>
               </list>
        </property>
        <property value="interceptorNames">
               <value>beforeAdvice</value>
        </property>
</bean>

 

  

     这个类实现了BeanPostProcessor接口的子接口:SmartInstantiationAwareBeanPostProcessor,

     每个被这个类care的类在取得bean实例前,会调用以下方法:

    

public Object postProcessBeforeInstantiation(Class beanClass, String beanName)
        throws BeansException
    {
        Object cacheKey = getCacheKey(beanClass, beanName);
        if(!targetSourcedBeans.contains(cacheKey))
        {
            if(advisedBeans.contains(cacheKey) || nonAdvisedBeans.contains(cacheKey))
                return null;
            if(isInfrastructureClass(beanClass) || shouldSkip(beanClass, beanName))
            {
                nonAdvisedBeans.add(cacheKey);
                return null;
            }
        }
        TargetSource targetSource = getCustomTargetSource(beanClass, beanName);
        if(targetSource != null)
        {
            targetSourcedBeans.add(beanName);
            Object specificInterceptors[] = getAdvicesAndAdvisorsForBean(beanClass, beanName, targetSource);
            Object proxy = createProxy(beanClass, beanName, specificInterceptors, targetSource);
            proxyTypes.put(cacheKey, proxy.getClass());
            return proxy;
        } else
        {
            return null;
        }
    }

  

3.  <aop:config>标签

     通过aop namespace下的一个标签aop:config来实现aop代理,这个也是用起来相当方便的一种配置方式

    

 <bean id="fooService" class="DefaultFooService"/>

<!-- this is the actual advice itself -->

<bean id="profiler" class="SimpleProfiler"/>

 <aop:config>

<aop:aspect ref="profiler">

 <aop:pointcut id="aopafterMethod"

expression="execution(* FooService.*(..))"/>

<aop:after pointcut-ref="aopafterMethod"

method="afterMethod"/>


<aop:pointcut id="aopBefore"

expression="execution(* FooService.getBefore(String)) and args(myName)"/>

<aop:before pointcut-ref="aopBefore"

method="beforeMethod"/>

</aop:aspect>

</aop:config>

 

   配置很简短,功能很全面。

   这种配置方式的原理则是在进行配置文件解析的时候,由AopNameSpaceHandler对此标签进行解析,然后

   注册一个“org.springframework.aop.config.internalAutoProxyCreator” bean,这个bean的实现类是:

   org/springframework/aop/aspectj/autoproxy/AspectJAwareAdvisorAutoProxyCreator,此类也实现了

   BeanPostProcessor接口。

 

至此,把大致原理分析了一下。当然,分析的不是很详细,有兴趣的朋友可以跟我联系大家一起交流一下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值