Spring AOP实现方式

本文介绍了两种Spring AOP的实现方式:调用Spring API和自定义实现。通过详细步骤展示了如何添加日志和执行环绕增强。在比较中指出,调用Spring API的方式更强大,能获取切入点信息,而自定义实现适用于不需使用切入点信息的情况。

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

方式一:调用spring api实现

1.编写UserService接口

public interface UserService {
    void add();
    void delete();
    void update();
    void query();
}

2.实现UserService接口

public class UserServiceImpl implements UserService {
    public void add() {
        System.out.println("增加方法。。。");
    }

    public void delete() {
        System.out.println("删除方法。。。");
    }

    public void update() {
        System.out.println("更新方法。。。");
    }

    public void query() {
        System.out.println("查询方法。。。");
    }
}

3.在方法前添加日志

public class Log implements MethodBeforeAdvice {
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName()+"类的"+method.getName()+"方法被执行");
    }
}

4.在方法后添加日志

public class AfterLog implements AfterReturningAdvice {
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName()+"类的"+method.getName()+"方法被执行,执行结果:"+returnValue);
    }
}

5.编写applicationContext.xml,导入AOP约束,添加切入点及 执行环绕增加

<?xml version="1.0" encoding="UTF-8"?>
<beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">


<bean id="userService" class="com.ld.service.impl.UserServiceImpl"/>
<bean id="log" class="com.ld.log.Log"/>
<bean id="afterLog" class="com.ld.log.AfterLog"/>

<!--配置aop需要导入aop约束-->
    <aop:config>
<!--        切入点 expression表达式 execution(要执行的位置)-->
        <aop:pointcut id="pointcut" expression="execution(* com.ld.service.UserService.*(..))"/>
<!--        执行环绕增加-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
    </aop:config>



</beans>

6.调用

public class MyTest {

    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = (UserService)context.getBean("userService");
        userService.add();
    }
}

7.运行结果

 

方式二:自定义实现

1.自定义类添加所需实现功能

public class DiyLog {

    public void before(){
        System.out.println("方法执行前");
    }

    public void after(){
        System.out.println("方法执行后");
    }
}

2.接口及接口实现类沿用方法一

3.编写application.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">


<bean id="userService" class="com.ld.service.impl.UserServiceImpl"/>
<bean id="diy" class="com.ld.log.DiyLog"/>
<aop:config>
    <aop:aspect ref="diy">
        <aop:pointcut id="point" expression="execution(* com.ld.service.impl.UserServiceImpl.*(..))"/>
        <aop:before method="after" pointcut-ref="point"/>
        <aop:after method="before" pointcut-ref="point"/>
    </aop:aspect>
</aop:config>
</beans>

4.调用

public class MyTest { @Test public void test(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService = (UserService)context.getBean("userService"); userService.add(); } }

5.运行结果

 

两种方式比较:

方式一更为强大些,能直接获取切入点的相关信息,若无需使用切入点类中信息,也可使用自定义类实现切面编程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值