复习笔记:利用spring AOP管理权限[简单实例]

这一排在复习spring的一些知识点,顺便写一下博客,下面看一下利用spring AOP做的管理权限简单实例;
首先定义一个用户:
[code]public class User {
private String username;

public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}[/code]
用户有三种人:未注册用户,注册用户,与管理员
注册用户可以可以发表,回复帖子
管理员除了可以发表,回复帖子,还可以删除帖子!
下面定义TestCommunity接口:
[code]public interface TestCommunity {
public void answerTopic();
public void deleteTopic();
}[/code]

实现上面接口的TestCommunityImpl类:
[code]public class TestCommunityImpl implements TestCommunity {
//注册用户与管理员拥有的功能
public void answerTopic() {
System.out.println("可以发表,回复帖子");
}
//管理员拥有的功能
public void deleteTopic() {
System.out.println("可以删除帖子!");
}
}[/code]

下一步,建立一下依赖注入的实现类TestResultImpl:
[code]public class TestResultImpl {
private TestCommunity test;

public void setTest(TestCommunity test) {
this.test = test;
}
public void answerTopic()
{
test.answerTopic();
}
public void deleteTopic()
{
test.deleteTopic();
}
}[/code]

接下来,就是最重要的一个类,拦截器,Around处理类型的,类TestAuthorityInterceptor:
[code]import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

//创建Around处理应该实现MethodInterceptor接口
public class TestAuthorityInterceptor implements MethodInterceptor {
private User user;

public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}

// invoke方法返回调用的结果
public Object invoke(MethodInvocation invocation) throws Throwable {
String methodName = invocation.getMethod().getName();

if (user.getUsername().equals("unRegistedUser")) {
System.out.println("你的身份是未注册用户,没有权限回复,删除帖子!");
return null;
}
if ((user.getUsername().equals("user"))
&& (methodName.equals("deleteTopic"))) {
System.out.println("你的身份是注册用户,没有权限删除帖子");
return null;
}
// proceed()方法对连接点的整个拦截器链起作用,拦截器链中的每个拦截器都执行该方法,并返回它的返回值
return invocation.proceed();
}

}
[/code]

配置文件:
[code]<?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="authTarget" class="org.test.lighter.TestCommunityImpl" />

<!-- 其中的username可以写为admin,user,和unRegistedUser -->
<bean id="user" class="org.test.lighter.User">
<property name="username" value="user" />
</bean>

<!-- 配置拦截器 -->
<bean id="TestAuthorityInterceptor"
class="org.test.lighter.TestAuthorityInterceptor">
<property name="user" ref="user" />
</bean>

<!-- 配置代理工厂bean -->
<bean id="service"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>org.test.lighter.TestCommunity</value>
</property>
<property name="target" ref="authTarget"/>
<property name="interceptorNames">
<list>
<value>TestAuthorityInterceptor</value>
</list>
</property>
</bean>

<bean id="testResult" class="org.test.lighter.TestResultImpl">
<property name="test" ref="service" />
</bean>
</beans>[/code]

再写一个执行文件BeanTest:
[code]import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class BeanTest {
public static void main(String[] args) throws Exception
{
ApplicationContext ctx = new FileSystemXmlApplicationContext("src/bean.xml");
TestResultImpl test = (TestResultImpl)ctx.getBean("testResult");
test.answerTopic();
test.deleteTopic();
}
}[/code]

执行结果:大家猜一下啦
[code]1、如果是管理员,打印出:
可以发表,回复帖子
可以删除帖子!

2、如果是注册用户:
可以发表,回复帖子
你的身份是注册用户,没有权限删除帖子

3、未注册用户:
你的身份是未注册用户,没有权限回复,删除帖子![/code]
内容概要:本文档详细介绍了基于MATLAB实现多目标差分进化(MODE)算法进行无人机三维路径规划的项目实例。项目旨在提升无人机在复杂三维环境中路径规划的精度、实时性、多目标协调处理能力、障碍物避让能力和路径平滑性。通过引入多目标差分进化算法,项目解决了传统路径规划算法在动态环境和多目标优化中的不足,实现了路径长度、飞行安全距离、能耗等多个目标的协调优化。文档涵盖了环境建模、路径编码、多目标优化策略、障碍物检测与避让、路径平滑处理等关键技术模块,并提供了部分MATLAB代码示例。 适合人群:具备一定编程基础,对无人机路径规划和多目标优化算法感兴趣的科研人员、工程师和研究生。 使用场景及目标:①适用于无人机在军事侦察、环境监测、灾害救援、物流运输、城市管理等领域的三维路径规划;②通过多目标差分进化算法,优化路径长度、飞行安全距离、能耗等多目标,提升无人机任务执行效率和安全性;③解决动态环境变化、实时路径调整和复杂障碍物避让等问题。 其他说明:项目采用模块化设计,便于集成不同的优化目标和动态环境因素,支持后续算法升级与功能扩展。通过系统实现和仿真实验验证,项目不仅提升了理论研究的实用价值,还为无人机智能自主飞行提供了技术基础。文档提供了详细的代码示例,有助于读者深入理解和实践该项目。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值