菜鸟学习Spring——60s配置XML方法实现简单AOP

本文介绍使用XML配置方式实现AOP的过程。通过简单的示例代码,展示了如何定义切面、切入点和通知,以及如何将这些元素组合在一起以实现对业务方法的安全检查。

一、概述。

       上一篇博客讲述了用注解的形式实现AOP如今讲述第二种AOP实现的方式利用XML来实现AOP。

二、代码演示。

        准备工作參照上一篇博客《菜鸟学习Spring——60s使用annotation实现简单AOP》

  文件夹结构:

   

        事实上比起上一篇博客中用annotation来实现AOP的方式我们仅仅要把SecurityHandler.java和配置文件applicationContext.xml更改为以下内容就能够了。以下我把这两个文件的代码写下来。

SecurityHandler.java

package com.tgb.spring;


public class SecurityHandler{


	private void checkSecurity(){
		System.out.println("checkSecurity");

	}


}


applicationContext.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" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="userManager" class="com.tgb.spring.UserManagerImpl" /> <bean id="securityHandler" class="com.tgb.spring.SecurityHandler"/> <aop:config> <aop:aspect id="securityAspect" ref="securityHandler"> <aop:pointcut id="addAddMethod" expression="execution(* com.tgb.spring.*.*(..))" /> <aop:before method="checkSecurity" pointcut-ref="addAddMethod" /> </aop:aspect> </aop:config> </beans>



效果图:


三、总结。

XML实现的AOP对代码没有了侵入性而且可以灵活的配置不用又一次编译。可是有个缺点就是配置文件太多了不好管理。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值