4.1AOP-XML配置Helloworld

戴着假发的程序员出品  抖音ID:戴着假发的程序员  欢迎关注

[查看视频教程]

关于SprngAOP的概念和思想我们在springAOP(Annotation)章节已经全部解释过了,所以本章节主要讲解SpringAOP的XML配置方式。不再赘述概念。

好的,让我们开始搭建springAOP的XML-Hellowrold程序吧。

如果我们要在配置问中配置AOP,则需要Schema-based AOP 支持。我们会在程序的配置文件中直接添加。

再说明一件事,spring是支持annotation和xml混合使用的,本章节我们主要将XML配置AOP,所以我们就全部使用xml方式配置。

创建一个Maven项目,添加如下依赖:

 1         <dependency>
 2             <groupId>org.springframework</groupId>
 3             <artifactId>spring-context</artifactId>
 4             <version>5.1.3.RELEASE</version>
 5         </dependency>
 6         <dependency>
 7             <groupId>org.springframework</groupId>
 8             <artifactId>spring-aspects</artifactId>
 9             <version>5.1.3.RELEASE</version>
10         </dependency>
11         <dependency>
12             <groupId>org.springframework</groupId>
13             <artifactId>spring-aop</artifactId>
14             <version>5.1.3.RELEASE</version>
15         </dependency>

我们准备一个业务类:

 1 /**
 2  * @author 戴着假发的程序员
 3  * @company http://www.boxuewa.com
 4  * @description
 5  */
 6 public class MessageBean {
 7     //输出信息的业务方法
 8     public String printMessage(String msg){
 9         System.out.println("MessageBean-printMessage:"+msg);
10         return msg;
11     }
12 }

添加一个Aspect类,注意在XML模式下,我们可以在Aspect类上方注解@Aspect,也可以不注解,完全使用XML配置的方式。这里我们就完全使用XML配置的方法。

我们的Aspect类就是一个普通的javaBean,在其中添加一个前置通知的方法,并且传入Jointpoint参数。

 1 /**
 2  * @author 戴着假发的程序员
 3  * @company http://www.boxuewa.com
 4  * @description
 5  */
 6 public class DkAspect {
 7     public void before(JoinPoint joinPoint){
 8         System.out.println("前置通知:"+joinPoint);
 9     }
10 }

添加一个配置文件,在其中添加Schema-based AOP 支持。

 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     <!-- 开始Aspect支持 -->
10     <!-- 这里的属性expose-proxy和proxy-target-class默认就是false -->
11     <aop:aspectj-autoproxy expose-proxy="false" proxy-target-class="false"/>
12     <!-- 注册Aspect类  -->
13     <bean id="dkAspcet" class="com.st.aspects.DkAspect"/>
14     <!-- 注册我们的业务类 -->
15     <bean id="msgService" class="com.st.beans.MessageBean"/>
16     <!-- AOP配置 -->
17     <aop:config>
18         <!-- 申明AspectBean,引用我们注册的dkAspect -->
19         <aop:aspect id="aspect" ref="dkAspcet">
20             <!--配置一个前置通知-->
21             <!-- method指定daAspect类中的前置通知方法,pointcut配置切入点表达式 -->
22             <aop:before method="before" pointcut="execution(* com.st.beans..*.*(..))"/>
23         </aop:aspect>
24     </aop:config>
25 </beans>

配置的解释已经在配置文件中注释,这里不再赘述。

测试:

1     @Test
2     public void testAopByXML() throws IOException {
3         ApplicationContext ac =
4                 new ClassPathXmlApplicationContext("applicationContext.xml");
5         MessageBean bean = ac.getBean(MessageBean.class);
6         bean.printMessage("假发的穿戴技巧");
7     }

结果:

posted @ 2020-10-30 17:31  戴着假发的程序员0-1  阅读(143)  评论(0编辑  收藏  举报