05 配置方式完成aop

前面几节我们使用三种方式完成了代理,接下来,我们将看一下在spring中如何完成aop。

1、前提约束

2、操作步骤

  • 在src/main/resources文件夹下创建application-config-aop.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.xsd   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="userService" class="net.wanho.service.UserService"></bean>

    <bean id="youraspect" class="net.wanho.aspect.YourAspect"></bean>
    <aop:config>
        <aop:pointcut id="pt" expression="execution(* net.wanho.service.*.*(..))"/>
        <aop:aspect id="aspect" ref="youraspect">
            <aop:after method="after"  pointcut-ref="pt"></aop:after>
            <aop:before method="before" pointcut-ref="pt"></aop:before>
        </aop:aspect>
    </aop:config>
</beans>
  • 在src/main/java文件夹下创建net.wanho.service.UserService.java,内容如下:
public class UserService {
    public void query() {
        System.out.println("query");
    }
}
  • 在src/main/java文件夹下创建net.wanho.aspect.YourAspect.java,内容如下:
public class YourAspect{
    public void before()
    {
        System.out.println("before");
    }
    public void after()
    {
        System.out.println("after");
    }
}
  • 在src/main/java文件夹下创建Test.java,内容如下:
public class Test{
        public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application-config-aop.xml");
        UserService userService = applicationContext.getBean("userService",UserService.class);
        userService.query();
    }
}

以上就是通过配置方式完成aop的过程。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring AOP 中,配置上下文可以通过以下步骤完成: 1. 在 Spring 配置文件中引入 aop 命名空间,如下所示: ```xml <beans xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> ``` 2. 定义切面类,其中包含切点和通知的定义。例如,创建一个名为 `LoggingAspect` 的切面类: ```java public class LoggingAspect { // 定义切点 @Pointcut("execution(* com.example.service.*.*(..))") private void serviceMethods() {} // 定义前置通知 @Before("serviceMethods()") public void beforeAdvice() { System.out.println("Before advice executed!"); } // 定义后置通知 @After("serviceMethods()") public void afterAdvice() { System.out.println("After advice executed!"); } } ``` 3. 在 Spring 配置文件中配置切面类的 bean: ```xml <bean id="loggingAspect" class="com.example.aspect.LoggingAspect" /> ``` 4. 配置 AOP 代理,将切面应用到目标对象上。可以使用 `<aop:config>` 元素来配置代理。例如: ```xml <aop:config> <aop:aspect ref="loggingAspect"> <aop:before method="beforeAdvice" pointcut-ref="serviceMethods" /> <aop:after method="afterAdvice" pointcut-ref="serviceMethods" /> </aop:aspect> </aop:config> ``` 在上述配置中,`ref` 属性指定了切面类的 bean ID,`method` 属性指定了通知方法的名称,`pointcut-ref` 属性指定了切点的引用。 通过以上配置,你可以在目标对象的方法执行前后执行相应的通知方法。注意,上述示例中的切点表达式是一个简单的示例,你需要根据实际情况进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值