打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
spring中aop原理

spring中aop原理

原理:将“方面代码封装”,使其与业务代码分开。
从业务层开始:
1,写一个业务接口
package com.svse.spring.aop;

public interface IBookBiz {
public void buyBook(String bookName,float bookPrice,float accountBalance);
}


2,实现它:
package com.svse.spring.aop;

public class IBookBizImpl implements IBookBiz {


public void buyBook(String bookName, float bookPrice, float accountBalance) {
//开始真正的业务
//从库存减一本书(业务代码)
System.out.println("[业务代码]从库存扣除一本书");
//从帐户扣钱(业务代码)
System.out.println("[业务代码]从帐户扣钱");
}
}


导入spring的core及aop的jar包:
写个方法准备对IBookBiz的实现类的方法进行拦截:
3,类容如下:
package com.svse.spring.aop;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class IBookBizAop implements MethodBeforeAdvice {

public void before(Method method, Object[] params, Object target)
throws Throwable {
// 封装"方面"代码
// method: 代表的是业务逻辑方法(buyBook)
// params: 代表的是业务逻辑方法的参数(String bookName, float bookPrice, float
// accountBalance)
// target: 封装业务逻辑对象(IBookBizImpl)

//书在库存中是否存在(方面代码)
String bookName = (String) params[0];
if (bookName == null) {
System.out.println("[方面代码]书在库存中不存在.");
throw new IllegalArgumentException("书在库存不存在");
}
System.out.println("[方面代码]库存中有货");

// 余额是否足够(方面代码)
float bookPrice = (Float) params[1];
float accountBalance = (Float) params[2];

if (accountBalance < bookPrice) {
System.out.println("[方面代码]余额不足");
throw new IllegalArgumentException("余额不足");
}
System.out.println("[方面代码]余额足够.");
}

}
4,在applicationContext.xml进行组装:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 业务逻辑代码 -->
<bean id="BookBizImpl" class="com.svse.spring.aop.IBookBizImpl"></bean>

<!-- 方面代码 -->
<bean id="BookAop" class="com.svse.spring.aop.IBookBizAop" ></bean>

<!-- 将业务代码和方面代码进行粘合,ProxyFactoryBean类的作用是将业务代码和方面代码进行粘合(比作胶水) -->
<bean id="BookBiz" class="org.springframework.aop.framework.ProxyFactoryBean" >
<!-- 业务逻辑接口 -->
<property name="proxyInterfaces" >
<value>com.svse.spring.aop.IBookBiz</value>
</property>
<!-- 定义的是方面代码 -->
<property name="interceptorNames">
<list>
<value>BookAop</value>
</list>
</property>
<!-- 定义业务代码 -->
<property name="target" ref="BookBizImpl"></property>
</bean>
</beans>
5,测试:
package com.svse.spring.aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AopMain {

/**
* @p测试
*/
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
IBookBiz bookBiz = (IBookBiz)context.getBean("BookBiz");
try
{
bookBiz.buyBook("Java Book", 12.45f, 34.6f);
}
catch(Exception e)
{}
}
}
运行结果:
[方面代码]库存中有货
[方面代码]余额足够.
[业务代码]从库存扣除一本书
[业务代码]从帐户扣钱
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
一个简单的Spring的AOP例子 - dragon - BlogJava
Spring AOP 详解
Spring AOP 简单入门示例
spring自带的定时任务功能,基于注解和xml配置
第3章 Spring AOP
Spring AOP是什么?你都拿它做什么?
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服