`
panmingzhi0815
  • 浏览: 13117 次
  • 性别: Icon_minigender_2
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

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{

	@Override
	public void answerTopic() {
		System.out.println("可以发表,回复帖子");
		
	}

	@Override
	public 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;
	}
	@Override
	public 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" class="org.pan.bean.dao.impl.TestCommunityImpl" />
	
	<bean name="user" class="org.pan.bean.User">
		<property name="userName" value="user" />
	</bean>
	
	<bean id="testAuthorityInterceptor" class="org.pan.interceptor.TestAuthorityInterceptor">
		<property name="user" ref="user" />
	</bean>
	
	<bean name="proxyAop" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="target" 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();
	}
}

 

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

 

  • 大小: 10.1 KB
分享到:
评论

相关推荐

    spring aop 附带测试实例

    在提供的压缩包文件"springAOP"中,可能包含了以下内容: - **切面类(Aspect Class)**:包含切点和通知的Java类,可能使用了`@Aspect`注解。 - **目标类(Target Class)**:被AOP代理的对象,通常包含业务逻辑。...

    Spring Aop使用实例

    **Spring AOP 使用实例** Spring AOP(Aspect Oriented Programming,面向切面编程)是Spring框架的一个重要组成部分,它提供了一种在不修改原有代码的情况下,通过代理方式添加额外功能的技术。这种技术使得我们...

    spring aop xml 实例

    Spring AOP的XML配置实例展示了如何将横切关注点(如日志、事务等)与业务逻辑解耦,提高了代码的可复用性和可维护性。这种编程模式在大型项目中尤其有用,因为它使得系统的结构更加清晰,每个组件都专注于自己的...

    spring AOP的代码实例

    以下是一个简单的Spring AOP代码实例,用于展示如何创建切面、通知以及如何配置切入点: 1. 首先,定义一个切面类,包含一个前置通知: ```java @Aspect @Component public class LoggingAspect { @Before(...

    spring aop小实例

    本文将基于"spring aop小实例"的标题,结合描述中的链接,深入探讨Spring AOP的基本概念、工作原理以及如何通过实际示例来理解和应用。 1. **AOP基本概念** - 面向切面编程(AOP)是面向对象编程(OOP)的补充,它允许...

    spring 应用aop 实例

    在Spring框架中,AOP(面向切面编程)是一种强大的设计模式,它允许开发者将关注点从核心业务逻辑中分离出来,比如日志记录、事务管理、权限控制等。本实例将深入探讨如何在Spring 4.0版本中实现AOP。 首先,AOP的...

    spring_aop_拦截实例

    本实例主要探讨如何在Spring AOP中实现拦截器。 首先,我们来理解Spring AOP的基本概念。AOP的核心是代理,Spring提供了两种代理方式:JDK动态代理和CGLIB代理。JDK动态代理基于接口,当目标对象实现了接口时使用;...

    Maven 项目 Spring Aop 用户注册 实例 代码.rar

    此外,Spring AOP允许我们在不修改源代码的情况下,对程序进行横向关注点的处理,如日志记录、权限检查和事务管理。在这个用户注册实例中,Spring AOP可能被用来实现诸如用户输入验证、操作日志记录等功能。 Spring...

    SpringMVC+springAOP+spring security+Hibernate整合实例代码

    在这个整合实例中,Spring Security可能会配置用户登录、权限控制和URL访问过滤,以确保只有授权的用户能访问特定资源。 4. Hibernate:Hibernate是一个对象关系映射(ORM)框架,它简化了Java应用与数据库之间的...

    spring aop学习实例

    **Spring AOP学习实例** Spring AOP(Aspect Oriented Programming,面向切面编程)是Spring框架中的一个重要组成部分,它提供了一种在不修改源代码的情况下,对程序进行功能增强的技术。AOP允许开发者定义“切面”...

    spring-aop实例demo

    在Spring框架中,AOP(面向切面编程)是一种强大的工具,它允许程序员定义“切面”,这些切面封装了应用程序中的交叉关注点,如日志、事务管理、权限检查等。Spring AOP是基于代理的,它可以为普通Java对象(POJOs)...

    SpringAOP.zip

    Spring AOP,全称为Aspect Oriented Programming,是Spring框架中的一个重要模块,主要负责处理系统中的...文件"5.SpringAOP_01"和"6.SpringAOP_02"很可能是课程的分阶段内容,涵盖了从基础概念到进阶实践的详细讲解。

    spring aop

    在Web项目中,Spring AOP通常用于处理跨多个控制器和业务层方法的通用任务,如记录请求日志、验证权限、事务管理等。通过配置或注解,你可以确保这些逻辑在每次相关方法执行时自动触发,从而提高代码的可读性和可...

    springAop默认代理方式.zip

    8. **AOP应用场景**:Spring AOP广泛应用于日志记录、事务管理、权限控制、缓存管理等场景。通过切面可以将这些通用功能与业务逻辑解耦,提高代码的可维护性和可重用性。 总之,Spring AOP的默认代理方式是动态代理...

    mybatis 拦截器 + spring aop切面 + spring事务+ 反射工具类

    在IT行业中,MyBatis、Spring AOP、Spring事务管理和反射工具类是常见的技术栈,它们在构建高效、灵活的企业级应用中起着至关重要的作用。以下是对这些知识点的详细阐述: 1. MyBatis拦截器(MyBatis Interceptor)...

    JDK动态代理 spring aop 的原理

    Spring AOP(面向切面编程)则是一种流行的应用框架,它利用动态代理来实现对业务代码的切面增强,如日志、事务管理等。现在让我们深入探讨JDK动态代理和Spring AOP的原理。 首先,JDK动态代理基于Java的反射机制,...

    spring 2.0使用AOP实例(基于Annotation的配置方式)

    在Spring框架中,AOP(面向切面编程)是一种强大的设计模式,它允许开发者将关注点从业务逻辑中分离出来,比如日志记录、事务管理、权限检查等。Spring 2.0引入了基于注解的AOP配置,极大地简化了AOP的使用。这篇...

    使用Spring配置文件实现AOP

    在Spring框架中,面向切面编程(Aspect Oriented Programming,简称AOP)是一种强大的设计模式,它允许我们定义横切关注点,如日志、事务管理、权限检查等,然后将这些关注点与核心业务逻辑解耦。这篇教程将详细讲解...

Global site tag (gtag.js) - Google Analytics