Spring框架之面向切面编程AOP(1.0)

Spring框架之面向切面编程AOP(1.0)

AOP初步实现:

定义一个业务层:
在这里插入图片描述

1,定义IMessageService接口
package cn.mldn.service;
public interface IMemberService {
	public boolean delete(String mid) throws Exception;
}
2,实现该接口MessageService
package cn.mldn.service.impl;
import org.springframework.stereotype.Service;
import cn.mldn.service.IMemberService;
@Service
public class MemberServiceImpl implements IMemberService {
	@Override
	public boolean delete(String mid) throws Exception {
		System.out.println("********* 【MemberService】数据删除处理。");
		return false;
	}
}
3,定义一个描述AOP程序的结构类ServiceProxy
package cn.mldn.aop;
public class ServiceProxy {
	public void beforeMethod() {
		System.out.println("####### 【ServiceProxy】public void beforeMethod(){}");
	}
	public void afterMethod() {
		System.out.println("####### 【ServiceProxy】public void afterMethod(){}");
	}
}
4,配置applicationContext.xml文件:
xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.1.xsd">
	<context:annotation-config />
	<context:component-scan base-package="cn.mldn" />
	<!-- 如果要想让切面控制有效,那么必须将此类的对象交由Spring进行管理 -->
	<bean id="serviceProxy" class="cn.mldn.aop.ServiceProxy"/>
	<!-- 要求进入到AOP的配置,重点配置切入点,以及确定切入的处理操作方法 -->
	<aop:config>
		<aop:pointcut expression="execution(* cn.mldn.service..*.*(..))" id="pointCut"/>
		<aop:aspect ref="serviceProxy">	<!-- 进行切面的配置,如果是切面控制应该有一个控制的类 -->
			<!-- 不同的切面处理操作可以使用同一个切入点,或者每一个操作来定义属于自己的切入点 -->
			<aop:before method="beforeMethod" pointcut-ref="pointCut"/>
			<aop:after method="afterMethod" pointcut="execution(* cn.mldn.service..*.*(..))"/>
		</aop:aspect>
	</aop:config>
</beans>
5,定义测试类
package cn.mldn.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.mldn.service.IMemberService;
public class TestMember {
	public static void main(String[] args) throws Exception {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		IMemberService ms = ctx.getBean("memberServiceImpl",
				IMemberService.class);
		ms.delete("mldn");
	}
}

运行结果:

在这里插入图片描述

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值