首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 企业软件 > 行业软件 >

spring aop权限小范例

2012-06-29 
spring aop权限小实例1.首先写一个User实例类,在程序中我们以个实体类userName属性来判断当前用户的权限:p

spring aop权限小实例

1.首先写一个User实例类,在程序中我们以个实体类userName属性来判断当前用户的权限:

package org.pan.bean;public class User {private String userName;public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}}

?2.定义一个业务接口TestCommunity;

package org.pan.bean.dao;public interface TestCommunity {public void answerTopic();public void deleteTopic();}

?

3.实例上面这个接口的服务:

package org.pan.bean.dao.impl;import org.pan.bean.dao.TestCommunity;public class TestCommunityImpl implements TestCommunity{@Overridepublic void answerTopic() {System.out.println("可以发表,回复帖子");}@Overridepublic void deleteTopic() {System.out.println("可以删除帖子");}}

?4.定义一个拦截器类,这个类将拦截上面接口中的方法,并判断当前User是否有权限调用接口中的方法:

package org.pan.interceptor;import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;import org.pan.bean.User;public class TestAuthorityInterceptor implements MethodInterceptor{private User user;public void setUser(User user) {this.user = user;}@Overridepublic Object invoke(MethodInvocation methodInvocation) throws Throwable {String methodName=methodInvocation.getMethod().getName();if(user.getUserName().equals("unRegisterUser")){System.out.println("你现在还没有登陆,没有权限回复帖子,删除帖子");return null;}if(user.getUserName().equals("user")&&methodName.equals("deleteTopic")){System.out.println("您的身份是注册用户,没有权限删除帖子");return null;}return methodInvocation.proceed();}}

?5.spring配置文件内容:

<?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: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.5.xsd    http://www.springframework.org/schema/aop     http://www.springframework.org/schema/aop/spring-aop-2.5.xsd     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ">    <bean id="testCommunity" /><bean name="user" value="user" /></bean><bean id="testAuthorityInterceptor" ref="user" /></bean><bean name="proxyAop" ref="testCommunity" /><property name="interceptorNames"><list><value>testAuthorityInterceptor</value></list></property></bean></beans>

?6.大功告成,来个testMain测试类了:

package org.pan.test;import org.pan.bean.dao.TestCommunity;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class testMain {public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");TestCommunity testCommunity = (TestCommunity) ctx.getBean("proxyAop");testCommunity.answerTopic();testCommunity.deleteTopic();}}

?

符件帖上工程全图与工程包:

?

热点排行