SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-009-带参数的ADVICE2 配置文件为XML...

本文深入探讨了在Spring框架中利用XML配置和AOP技术实现读心术功能的过程,具体展示了如何在不使用注解的情况下通过配置类和切入点表达式来拦截和获取志愿者的思想。

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

一、

1.配置文件为xml时则切面类不用写aop的anotation

 1 package com.springinaction.springidol;
 2 
 3 public class Magician implements MindReader {
 4   private String thoughts;
 5 
 6   public void interceptThoughts(String thoughts) {
 7     System.out.println("Intercepting volunteer's thoughts");
 8     this.thoughts = thoughts;
 9   }
10 
11   public String getThoughts() {
12     return thoughts;
13   }
14 }

 

2.

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4  xmlns:aop="http://www.springframework.org/schema/aop"
 5  xsi:schemaLocation="http://www.springframework.org/schema/beans 
 6      http://www.springframework.org/schema/beans/spring-beans.xsd
 7      http://www.springframework.org/schema/aop
 8      http://www.springframework.org/schema/aop/spring-aop.xsd">
 9 
10   <bean id="volunteer" 
11       class="com.springinaction.springidol.Volunteer" />
12   <bean id="magician" 
13       class="com.springinaction.springidol.Magician" />
14   
15 <!--<start id="mindreading_aspect"/>--> 
16   <aop:config>
17     <aop:aspect ref="magician">
18       <aop:pointcut id="thinking" 
19         expression="execution(* 
20         com.springinaction.springidol.Thinker.thinkOfSomething(String)) 
21              and args(thoughts)" />
22           
23       <aop:before 
24           pointcut-ref="thinking"
25           method="interceptThoughts" 
26           arg-names="thoughts" />
27     </aop:aspect>
28   </aop:config>
29 <!--<end id="mindreading_aspect"/>-->
30 
31 </beans>

 

转载于:https://www.cnblogs.com/shamgod/p/5240084.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值