用spring Aop 做的管理权限(简单实例)


首先定义一个用户: 
Java代码   收藏代码
  1. public class User {  
  2.     private String username;  
  3.   
  4.     public String getUsername() {  
  5.         return username;  
  6.     }  
  7.     public void setUsername(String username) {  
  8.         this.username = username;  
  9.     }  
  10. }  

用户有三种人:未注册用户,注册用户,与管理员 
注册用户可以可以发表,回复帖子 
管理员除了可以发表,回复帖子,还可以删除帖子! 
下面定义TestCommunity接口: 
Java代码   收藏代码
  1. public interface TestCommunity {  
  2.   public void answerTopic();  
  3.   public void deleteTopic();  
  4. }  


实现上面接口的TestCommunityImpl类: 
Java代码   收藏代码
  1. public class TestCommunityImpl implements TestCommunity {  
  2.     //注册用户与管理员拥有的功能  
  3.     public void answerTopic() {  
  4.         System.out.println("可以发表,回复帖子");  
  5.     }  
  6.     //管理员拥有的功能  
  7.     public void deleteTopic() {  
  8.         System.out.println("可以删除帖子!");  
  9.     }  
  10. }  


下一步,建立一下依赖注入的实现类TestResultImpl: 
Java代码   收藏代码
  1. public class TestResultImpl {  
  2.     private TestCommunity test;  
  3.   
  4.     public void setTest(TestCommunity test) {  
  5.         this.test = test;  
  6.     }     
  7.      public void answerTopic()  
  8.      {  
  9.          test.answerTopic();  
  10.      }  
  11.       public void deleteTopic()  
  12.       {  
  13.           test.deleteTopic();  
  14.       }  
  15. }  


接下来,就是最重要的一个类,拦截器,Around处理类型的,类TestAuthorityInterceptor: 
Java代码   收藏代码
  1. import org.aopalliance.intercept.MethodInterceptor;  
  2. import org.aopalliance.intercept.MethodInvocation;  
  3.   
  4. //创建Around处理应该实现MethodInterceptor接口  
  5. public class TestAuthorityInterceptor implements MethodInterceptor {  
  6.     private User user;  
  7.   
  8.     public User getUser() {  
  9.         return user;  
  10.     }  
  11.     public void setUser(User user) {  
  12.         this.user = user;  
  13.     }  
  14.   
  15.     // invoke方法返回调用的结果  
  16.     public Object invoke(MethodInvocation invocation) throws Throwable {  
  17.         String methodName = invocation.getMethod().getName();  
  18.   
  19.         if (user.getUsername().equals("unRegistedUser")) {  
  20.             System.out.println("你的身份是未注册用户,没有权限回复,删除帖子!");  
  21.             return null;  
  22.         }  
  23.         if ((user.getUsername().equals("user"))  
  24.                 && (methodName.equals("deleteTopic"))) {  
  25.             System.out.println("你的身份是注册用户,没有权限删除帖子");  
  26.             return null;  
  27.         }  
  28.         // proceed()方法对连接点的整个拦截器链起作用,拦截器链中的每个拦截器都执行该方法,并返回它的返回值  
  29.         return invocation.proceed();  
  30.     }  
  31.   
  32. }  


配置文件: 
Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  3. <beans>  
  4.     <bean id="authTarget" class="org.test.lighter.TestCommunityImpl" />  
  5.   
  6.     <!-- 其中的username可以写为admin,user,和unRegistedUser -->  
  7.     <bean id="user" class="org.test.lighter.User">  
  8.         <property name="username" value="user" />  
  9.     </bean>  
  10.   
  11.     <!-- 配置拦截器 -->  
  12.     <bean id="TestAuthorityInterceptor"  
  13.         class="org.test.lighter.TestAuthorityInterceptor">  
  14.         <property name="user" ref="user" />  
  15.     </bean>  
  16.   
  17.     <!-- 配置代理工厂bean -->  
  18.     <bean id="service"  
  19.         class="org.springframework.aop.framework.ProxyFactoryBean">  
  20.         <property name="proxyInterfaces">  
  21.             <value>org.test.lighter.TestCommunity</value>  
  22.         </property>  
  23.         <property name="target" ref="authTarget"/>  
  24.         <property name="interceptorNames">  
  25.             <list>  
  26.                 <value>TestAuthorityInterceptor</value>  
  27.             </list>  
  28.         </property>  
  29.     </bean>  
  30.   
  31.     <bean id="testResult" class="org.test.lighter.TestResultImpl">  
  32.         <property name="test" ref="service" />  
  33.     </bean>  
  34. </beans>  


再写一个执行文件BeanTest: 
Java代码   收藏代码
  1. import org.springframework.context.ApplicationContext;  
  2. import org.springframework.context.support.FileSystemXmlApplicationContext;  
  3.   
  4. public class BeanTest {  
  5.   public static void main(String[] args) throws Exception  
  6.   {  
  7.       ApplicationContext ctx = new FileSystemXmlApplicationContext("src/bean.xml");  
  8.       TestResultImpl test = (TestResultImpl)ctx.getBean("testResult");  
  9.       test.answerTopic();  
  10.       test.deleteTopic();  
  11.   }  
  12. }  


执行结果:大家猜一下啦 
Java代码   收藏代码
  1. 1、如果是管理员,打印出:  
  2. 可以发表,回复帖子  
  3. 可以删除帖子!  
  4.   
  5. 2、如果是注册用户:  
  6. 可以发表,回复帖子  
  7. 你的身份是注册用户,没有权限删除帖子  
  8.   
  9. 3、未注册用户:  
  10. 你的身份是未注册用户,没有权限回复,删除帖子!  
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值