SpringAOP的实现方式

1.使用SpringAPI实现AOP

<aop:config>
        <!-- 切入点:需要操作的目标类中的目标方法
            execution中只需要修改全类名
        -->
        <aop:pointcut id="pointcut" expression="execution(* 全类名.*(..))" />

        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>

2.自定义类实现AOP

<!-- 自定义类 -->
<bean id="diy" class="com.jay.pojo.diy"/>
    
<aop:config>
        <!-- 切面 -->
        <aop:aspect ref="diy">
            <!-- 切入点 -->
            <aop:pointcut id="pointcut" expression="execution(* 全类名.*(..))"/>

            <aop:before method="before" pointcut-ref="pointcut"/>
            <aop:after method="after" pointcut-ref="pointcut"/>
        </aop:aspect>
 </aop:config>

3.使用注解实现AOP

注解实现类
//必须写切面注解,否则无法切入
@Aspect
public class diy {

    @Before("execution(* com.jay.service.serviceImpl.*(..))")
    public void before(){
        System.out.println("这是before方法");
    }

    @After("execution(* com.jay.service.serviceImpl.*(..))")
    public void after(){
        System.out.println("这是after方法");
    }
}
XML文件配置
<bean id="service" class="com.jay.service.serviceImpl"/>

<!-- 注解实现的AOP类 -->
<bean id="diy" class="com.jay.pojo.diy"/>

<!-- 识别注解,自动代理 -->
<aop:aspectj-autoproxy/>

猜你喜欢

转载自www.cnblogs.com/chenxi-mxj/p/11454567.html