AOP面向切面编程JAVA动态代理实现用户权限管理(实现篇)
java动态代理机制的功能十分强大,使用动态代理技术能够有效的降低应用中各个对象之间的耦合紧密程度,提高开发的效率以及程序的可维护性,事实上Spring AOP就是建立在Java动态代理的基础之上。其实AOP、IOC、动态代理、序列化等技术与设计思想都是结合在一起使用的,要想做好一个功能强大齐全的系统,这些技术搜需要我们取学习整合的。
开始搬砖
1.创建接口去让我们的实体类去实现其中的方法及属性,也就是我们的用户权限
package com.icommon.aoptest;
public interface AopInterface {
Integer getAopId();
void setAopId(Integer aopid);
}
package com.icommon.aoptest;
public class UserCommonInfo implements AopInterface {//实现AopInterface接口,后续将权限参数直接注入到接口中,这样实体类获取到也是该权限值
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + ", aopId=" + aopId + "]";
}
private Integer id;
private String name;
private Integer age;
private Integer aopId;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getAopId() {
return aopId;
}
public void setAopId(Integer aopid) {
this.aopId = aopid;
}
}
2.实现MethodInterceptor 为Spring AOP完成注入通知
package com.icommon.aoptest;
import org.aopalliance.intercept.MethodInvocation;
public interface ServiceBeforeAdvice {
void handler(MethodInvocation invocation);
}
3.AOPAdvice实现ServiceBeforeAdvice作为整个参数调度的类
package com.icommon.aoptest;
import org.aopalliance.intercept.MethodInvocation;
import com.icommon.exception.OPMException;
public abstract class AopAdvice implements ServiceBeforeAdvice {
@Override
public void handler(MethodInvocation invocation) {
String name = "Tom";
if(needIntercept(name,invocation)){
handler(name,invocation);
}
}
public abstract boolean needIntercept(String name, MethodInvocation invocation);
public abstract void handler(String name, MethodInvocation invocation);
protected static void initVpnId(String name, AopInterface bean){
if(name.equals("Tom")){
bean.setAopId();
}
}
protected static void checkVPNAuthority(String name, AopInterface bean){
String text = "not equel!!!";
Integer aopId = bean.getAopId();
if(aopId != ){
throw new OPMException(text);
}
}
}
4.添加通知ADDAdvice
package com.icommon.aoptest;
import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInvocation;
public class AddAdvice extends AopAdvice{
public static final AddAdvice INSTANCE = new AddAdvice();
private static Method SEND_ONE_COMMAND_METHOD = null;
private AddAdvice(){
}
@Override
public boolean needIntercept(String name, MethodInvocation invocation) {
if (invocation.getMethod() !=SEND_ONE_COMMAND_METHOD && invocation.getArguments()[] instanceof AopInterface) {
OperationEnum ope = (OperationEnum) invocation.getArguments()[];
if (ope == OperationEnum.ADD) {
return true;
}
}
return false;
}
@Override
public void handler(String name, MethodInvocation invocation) {
AopInterface bean = (AopInterface) invocation.getArguments()[];
initVpnId(name, bean);
}
}
5.注册实例,这里可采用单例,在服务启动时只允许有一个此实例
package com.icommon.aoptest;
import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInvocation;
public class AddAdvice extends AopAdvice{
public static final AddAdvice INSTANCE = new AddAdvice();
private static Method SEND_ONE_COMMAND_METHOD = null;
private AddAdvice(){
}
@Override
public boolean needIntercept(String name, MethodInvocation invocation) {
if (invocation.getMethod() !=SEND_ONE_COMMAND_METHOD && invocation.getArguments()[] instanceof AopInterface) {
OperationEnum ope = (OperationEnum) invocation.getArguments()[];
if (ope == OperationEnum.ADD) {
return true;
}
}
return false;
}
@Override
public void handler(String name, MethodInvocation invocation) {
AopInterface bean = (AopInterface) invocation.getArguments()[];
initVpnId(name, bean);
}
}
6.封装一些常量
package com.icommon.aoptest;
public enum OperationEnum {
ADD, DEL
}
7.进行通知
package com.icommon.aoptest; import java.util.ArrayList;
import java.util.List; import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation; public class AopProxyFactoryBean implements MethodInterceptor{
private static List<ServiceBeforeAdvice> advices = new ArrayList<ServiceBeforeAdvice>(); public static void registe(ServiceBeforeAdvice advice) {
if (!advices.contains(advice)) {
advices.add(advice); }
} @Override
public Object invoke(MethodInvocation invocation) throws Throwable { Object obj = invocation.proceed();
for(ServiceBeforeAdvice advice:advices){
advice.handler(invocation);
} return obj;
}
}
9.配置信息
<?xml version="1.0"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id ="aopProxyFactoryBean" class="com.icommon.aoptest.AopProxyFactoryBean"/>
<bean id ="userService" class="com.icommon.serviceimpl.UserManagerImpl"/>
<bean id ="test" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="interceptorNames">
<list>
<value>aopProxyFactoryBean</value>
</list>
</property>
<property name="target" ref="userService"></property>
<property name="proxyTargetClass" value="true"></property>
</bean>
</beans>
10.测试(模拟服务启动、实体类参数赋值)
package com.icommon.aoptest; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.icommon.service.UserManager; public class TestAop {
@SuppressWarnings("resource")
public static void main(String[] args) {
Integer id = ;
String name = "Susan";
Integer age = ;
UserCommonInfo user = new UserCommonInfo();
user.setId(id);
user.setName(name);
user.setAge(age);
StartInitDate.registeAdvice();
ApplicationContext cx = new ClassPathXmlApplicationContext("bean.xml");
UserManager useripm = (UserManager) cx.getBean("test");
useripm.addUser(OperationEnum.ADD, user);
System.out.println("AOP "+user.toString());
}
}
11,运行结果
UserUser [id=2, name=Susan, age=16, aopId=null]
AOP User [id=2, name=Susan, age=16, aopId=12]
AOP面向切面编程JAVA动态代理实现用户权限管理(实现篇)的更多相关文章
- JAVA AOP面向切面编程与动态代理
1.静态代理和动态代理的概念: 在我的另一篇博文:Java 静态代理和动态代理 中有讲到,这里就不做赘述了. JDK动态代理它的好处理是可以为我们生成任何一个接口的代理类,并将需要增强的方法织入到 ...
- AOP面向切面的基石——动态代理(一)
其实动态代理在Java里不是什么新技术了,早在java 1.2之后便通过 java.lang.reflect.InvocationHandler 加入了动态代理机制. 下面例子中,LancerEvol ...
- java aop面向切面编程
最近一直在学java的spring boot,一直没有弄明白aop面向切面编程是什么意思.看到一篇文章写得很清楚,终于弄明白了,原来跟python的装饰器一样的效果.http://www.cnblog ...
- [转] AOP面向切面编程
AOP面向切面编程 AOP(Aspect-Oriented Programming,面向切面的编程),它是可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. ...
- 【原创】Android AOP面向切面编程AspectJ
一.背景: 在项目开发中,对 App 客户端重构后,发现用于统计用户行为的友盟统计代码和用户行为日志记录代码分散在各业务模块中,比如在视频模块,要想实现对用户对监控点的实时预览和远程回放行为进行统计, ...
- Spring:AOP面向切面编程
AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效果. AOP是软件开发思想阶段性的产物,我们比较熟悉面向过程O ...
- Spring Boot2(六):使用Spring Boot整合AOP面向切面编程
一.前言 众所周知,spring最核心的两个功能是aop和ioc,即面向切面和控制反转.本文会讲一讲SpringBoot如何使用AOP实现面向切面的过程原理. 二.何为aop aop全称Aspec ...
- Spring 08: AOP面向切面编程 + 手写AOP框架
核心解读 AOP:Aspect Oriented Programming,面向切面编程 核心1:将公共的,通用的,重复的代码单独开发,在需要时反织回去 核心2:面向接口编程,即设置接口类型的变量,传入 ...
- AOP 面向切面编程, Attribute在项目中的应用
一.AOP(面向切面编程)简介 在我们平时的开发中,我们一般都是面对对象编程,面向对象的特点是继承.多态和封装,我们的业务逻辑代码主要是写在这一个个的类中,但我们在实现业务的同时,难免也到多个重复的操 ...
随机推荐
- SQL server 批量插入和更新数据
批量插入数据 insert into A表数据库名.[dbo].A(a,b,c) (select a,b,c from B表数据库名.[dbo].B) 批量更新数据 根据身份证第二位更新性别 upda ...
- mybatis-ehcache整合中出现的异常 ibatis处理器异常(executor.ExecutorException)解决方法
今天学习mabatis时出现了,ibatis处理器处理器异常,显示原因是Executor was closed.则很有可能是ibatis的session被关闭了, 后面看了一下测试程序其实是把sqlS ...
- Predict Referendum by sklearn package
Background Last day we talk about Python Basics in Chinese. Today, we will do data analysis with pyt ...
- libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)
In Xcode 9 and Swift 4: Print exception stack to know the reason of the exception: Go to show break ...
- window上安装kafka(单机)
1.第一步骤,先安装JDK,请参考:https://www.cnblogs.com/xubao/p/10692861.html 2.第二步骤,安装zookeeper,请参考:https://www.c ...
- mysql学习之check无效的解决及触发器的使用
SQL的约束种类: 一.非空约束 not null 二.唯一约束 unique 三.主键约束 四.外键约束 五.check约束 该约束可用于列之间检查语义限制的,实际应用过程中非常常用!! 然鹅,My ...
- C# 枚举 Flag属性(权限设计)
枚举是一个可以列举元素的对象,常用于权限,日期,类型等. 如果对一个值可以包含多个,那么可以使用枚举,加上Flags [Flag] public enum Permission { create=, ...
- WSGI 的简单理解
WSGI是Web Server Gateway Interface(Web服务器网关接口)的缩写.其位于web应用程序与web服务器之间.python标准库提供的独立WSGI服务器称为wsgiref. ...
- ThinkPHP5.0源码学习之缓存Cache(一)
一.文件 1.缓存配置文件:thinkphp\convention.php 2.缓存文件:thinkphp\library\think\Cache.php 3.驱动目录:thinkphp\librar ...
- [luogu P2375] [NOI 2014] 动物园
[luogu P2375] [NOI 2014] 动物园 题目描述 近日,园长发现动物园中好吃懒做的动物越来越多了.例如企鹅,只会卖萌向游客要吃的.为了整治动物园的不良风气,让动物们凭自己的真才实学向 ...