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动态代理实现用户权限管理(实现篇)的更多相关文章

  1. JAVA AOP面向切面编程与动态代理

    1.静态代理和动态代理的概念:   在我的另一篇博文:Java 静态代理和动态代理 中有讲到,这里就不做赘述了. JDK动态代理它的好处理是可以为我们生成任何一个接口的代理类,并将需要增强的方法织入到 ...

  2. AOP面向切面的基石——动态代理(一)

    其实动态代理在Java里不是什么新技术了,早在java 1.2之后便通过 java.lang.reflect.InvocationHandler 加入了动态代理机制. 下面例子中,LancerEvol ...

  3. java aop面向切面编程

    最近一直在学java的spring boot,一直没有弄明白aop面向切面编程是什么意思.看到一篇文章写得很清楚,终于弄明白了,原来跟python的装饰器一样的效果.http://www.cnblog ...

  4. [转] AOP面向切面编程

    AOP面向切面编程 AOP(Aspect-Oriented Programming,面向切面的编程),它是可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. ...

  5. 【原创】Android AOP面向切面编程AspectJ

    一.背景: 在项目开发中,对 App 客户端重构后,发现用于统计用户行为的友盟统计代码和用户行为日志记录代码分散在各业务模块中,比如在视频模块,要想实现对用户对监控点的实时预览和远程回放行为进行统计, ...

  6. Spring:AOP面向切面编程

    AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效果. AOP是软件开发思想阶段性的产物,我们比较熟悉面向过程O ...

  7. Spring Boot2(六):使用Spring Boot整合AOP面向切面编程

    一.前言 众所周知,spring最核心的两个功能是aop和ioc,即面向切面和控制反转.本文会讲一讲SpringBoot如何使用AOP实现面向切面的过程原理. 二.何为aop ​ aop全称Aspec ...

  8. Spring 08: AOP面向切面编程 + 手写AOP框架

    核心解读 AOP:Aspect Oriented Programming,面向切面编程 核心1:将公共的,通用的,重复的代码单独开发,在需要时反织回去 核心2:面向接口编程,即设置接口类型的变量,传入 ...

  9. AOP 面向切面编程, Attribute在项目中的应用

    一.AOP(面向切面编程)简介 在我们平时的开发中,我们一般都是面对对象编程,面向对象的特点是继承.多态和封装,我们的业务逻辑代码主要是写在这一个个的类中,但我们在实现业务的同时,难免也到多个重复的操 ...

随机推荐

  1. 王之泰《面向对象程序设计(java)》课程学习总结

    第一部分:理论知识学习部分 总复习纲要 1. Java语言特点与开发环境配置(第1章.第2章) 2. Java基本程序结构(第3章) 3. Java面向对象程序结构(第4章.第5章.第6章) 4. 类 ...

  2. 微信小程序框架集合

    UI组件 weui-wxss ★852 - 同微信原生视觉体验一致的基础样式库 Wa-UI ★122 - 针对微信小程序整合的一套UI库 wx-charts ★105 - 微信小程序图表工具 wema ...

  3. jQuary学习の五のAJAX

    AJAX 是与服务器交换数据的技术,它在不重载全部页面的情况下,实现了对部分网页的更新. 一.jQuery load() 方法 jQuery load() 方法是简单但强大的 AJAX 方法. loa ...

  4. P4389 付公主的背包

    注意 初始化的时候要这样写 for(int i=1,x;i<=n;i++){ scanf("%d",&x); v[x]++; } for(int i=1;i<= ...

  5. [译]RabbitMQ教程C#版 - 路由

    先决条件 本教程假定 RabbitMQ 已经安装,并运行在localhost标准端口(5672).如果你使用不同的主机.端口或证书,则需要调整连接设置. 从哪里获得帮助 如果您在阅读本教程时遇到困难, ...

  6. 【HNOI 2016】网络

    Problem Description 一个简单的网络系统可以被描述成一棵无根树.每个节点为一个服务器.连接服务器与服务器的数据线则看做一条树边.两个服务器进行数据的交互时,数据会经过连接这两个服务器 ...

  7. acm:屁屁上的巴掌

    涉及算法:深度搜索 题目: 题目描述 小新是个调皮的孩子,他总是会把衣服搞脏,他的妈妈美伢非常的生气,于是在<和妈妈的约定条款>加上了第三百七十七条:小新衣服上每有一块污渍妈妈就会打小新的 ...

  8. [原]osg模型动画|骨骼动画

    参考源码:osg的官方例子:osganimationviewer 首先制作一个带骨骼动画的模型  demo.FBX 这里面我们做了两个骨骼动画:1.open   2.close 下面开始在osg中使用 ...

  9. 合并两个有序链表(java实现)

    题目: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1->1-&g ...

  10. 20175317 《Java程序设计》第六周学习总结

    20175317 <Java程序设计>第六周学习总结 教材学习内容总结 第六周我学习了教材第七章与第十章的内容,了解了内部类.异常类与输入输出流的知识,学到了以下内容: 什么是内部类 如何 ...