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(面向切面编程)简介 在我们平时的开发中,我们一般都是面对对象编程,面向对象的特点是继承.多态和封装,我们的业务逻辑代码主要是写在这一个个的类中,但我们在实现业务的同时,难免也到多个重复的操 ...
随机推荐
- C# 启动外部进程
C# 启动外部进程: var processInfo = new ProcessStartInfo(fullName); processInfo.UseShellExecute = false; pr ...
- css中绝对定位和相对定位的区别
先说个技巧一般用:子绝父相,即相对定位是给父级的,绝对定位的时候是给子级的. 一:绝对定位 position: absolute;绝对定位:绝对定位是相对于元素最近的已定位的祖先元素(即是设置了绝对定 ...
- Visual Studio 2017 注册码
Visual Studio 2017(VS2017) 企业版 Enterprise 注册码:NJVYC-BMHX2-G77MM-4XJMR-6Q8QF Visual Studio 2017(VS201 ...
- /etc/security/limits.conf不生效
总结下来发现要使limits.conf生效,需要在 /etc/ssh/sshd_config 配置中 设置 UsePAM yes UseLogin yes #这个保证其他的用户也能修改设置 ...
- vue--transition-group
1.为什么要使用<transition-group> <transition></transition>是vue封装的过渡组件 <transition nam ...
- Vue开发与调试工具
vscode: Visual Studio Code https://code.visualstudio.com/Download 可以下载各个版本的,Windows/ Debian /Mac 等 W ...
- OpenGL.教程
5.第五课:带纹理的立方体.html(http://www.opengl-tutorial.org/cn/beginners-tutorials/tutorial-5-a-textured-cube/ ...
- 保密数据!泽宝曝光各个主要店铺收入 核心SKU数量少得惊人
今年跨境电商圈的一大并购,上市公司星徽精密并购知名跨境电商大卖家泽宝股份正在进程中.星徽精密在向证监会行政许可项目审查回复中,披露了泽宝股份众多保密数据,揭开了泽宝股份众多经营关键点,值得跨境电商卖家 ...
- NopCommerce源码架构详解
NopCommerce源码架构详解--初识高性能的开源商城系统cms 很多人都说通过阅读.学习大神们高质量的代码是提高自己技术能力最快的方式之一.我觉得通过阅读NopCommerce的源码,可以从 ...
- sql server 将两列数据合并到一列 拼接
create table a( s nvarchar null, ss nvarchar null, f decimal(18,1) null, ff decimal(18,1) null,)INSE ...