一:基于XML配置
<!--目标对象 -->
<bean id="AOPservice" class="com.study.spring.service.impl.AOPServiceImpl"/>
<!-- advice通知 -->
<bean id="adviceMessage" class="com.study.spring.aop.advice.Advicer"/>
<bean id="perfAdvice" class="com.study.spring.aop.advice.PerfAdvicer"/>
<!-- 切入点adviser -->
<bean id="adviser" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="adviceMessage"/>
<property name="pattern" value="com.study.spring.service.impl.AOPServiceImpl.print"></property>
</bean>
<bean id="perfAdviser" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="perfAdvice"/>
<property name="pattern" value="com.study.spring.service.impl.AOPServiceImpl.*(..)"></property>
</bean>
<!-- 代理对象 返回实例是目标对象 target属性指定的AOPservice对象-->
<bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref bean="AOPservice"/>
</property>
<!--源码内固定的属性private String[] interceptorNames; -->
<property name="interceptorNames">
<list>
<value>adviser</value>
<value>perfAdviser</value>
</list>
</property>
</bean>
二:基于XML配置
<bean id="perfAdviser" class="com.study.spring.aop.advice.PerfAdvicer2" />
<aop:config proxy-target-class="true">
<aop:aspect ref="perfAdviser">
<aop:pointcut id="methodPoint" expression="execution(* com.study.spring.service.impl.AOPServiceImpl.*(..)) "/>
<!--在该切入点使用自定义拦截器-->
<aop:before pointcut-ref="methodPoint" method="before"/>
<aop:around pointcut-ref="methodPoint" method="around"/>
<aop:after pointcut-ref="methodPoint" method="after"/>
<aop:after-returning pointcut-ref="methodPoint" method="afterRet"/>
<aop:after-throwing pointcut-ref="methodPoint" method="afterError"/>
</aop:aspect>
</aop:config>
三:基于注解配置
<bean id="AOPservice" class="com.study.spring.service.impl.AOPServiceImpl"/>
<bean id="perfAdvice" class="com.study.spring.aop.advice.PerfAdvicer3"/>
<aop:aspectj-autoproxy proxy-target-class="true"/>
本文介绍了 Spring AOP 的三种实现方式:基于 XML 配置的切入点和通知、基于 XML 配置的 AOP 代理及基于注解的 AOP 配置。通过示例详细展示了如何使用这些方式来实现面向切面编程。
442

被折叠的 条评论
为什么被折叠?



