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(面向切面编程)简介 在我们平时的开发中,我们一般都是面对对象编程,面向对象的特点是继承.多态和封装,我们的业务逻辑代码主要是写在这一个个的类中,但我们在实现业务的同时,难免也到多个重复的操 ...
随机推荐
- 什么时候使用“RCC_APBXPeriph_AFIO”
什么时候需要开启复用时钟: (1)使用EXTI (2)重映射(用到外设的重映射功能时才需要使能AFIO的时钟) 举例:重映射USART2 USART2的TX/RX在PA.2/3.但是,PA.2已经被T ...
- Linux (麒麟)系统 重启后无法登陆进图形界面
登录图形化界面的时候,会显示GNOME电源管理器没启动等提示信息,会一直卡在登录界面 在启动的时候按ESC或者在登录界面crtl+alt +f3 进入字符终端界面 查看物理存储空间占用信息,可能会有一 ...
- vue--vant组件库field输入框
安装vant UI框架: cnpm install vant –-save-dev 导入组件-在main.js里: import Vant from 'vant'; import'vant/lib/v ...
- python+selenium测试
网址http://blog.csdn.net/u011541946/article/category/6788788/1
- linux 配置ftp服务
需求:定时远程上传文件,windows->linux linux是一个云服务器,centos7 1:安装vsftpd yum install vsftpd 2:设置开机启动服务chkconfig ...
- Python自学:第三章 使用del语句删除元素
motorcycles = ["honda", "yamaha", "suzuki"] print(motorcycles) del mot ...
- Python实现:汉诺塔问题
汉诺塔问题不管在任何编程语言里都是经典问题,是采用递归算法的经典案例,该问题可以抽象如下: 一 .3根圆柱A,B,C,其中A上面串了n个圆盘 二 .这些圆盘从上到下是按从小到大顺序排列的,大的圆盘任何 ...
- python 字典 dict 该注意的一些操作
在用python处理dict 的时候,有几个该注意的地方,这里跟大家提一下: 1)操作dict 时,尽量少产生新的列表对象.比如: 遍历dict的时候,如果用 dic = {"a" ...
- jQuery的版本兼容问题
之前在做头像上传的时候,使用的jQuery是1.8.2的版本,然后头像上传做完后,发现项目用的jQuery版本是3.3.1的.由于两个版本的差距太大了.所以兼容很差. 3.3.1不支持剪切头像的某些函 ...
- [LeetCode] 83. Remove Duplicates from Sorted List ☆(从有序链表中删除重复项)
描述 Given a sorted linked list, delete all duplicates such that each element appear only once. Exampl ...