- 浏览: 6648 次
- 性别:
- 来自: 广州
最新评论
通过创建InvocationHandler代理类及java bean内省方法及读取配置文件,实现spring核心功能IOC及AOP功能.
先看测试类及结果,全代码已上传文件供下载.
测试类
运行结果输出:
AOP前test.Person.sayHello
执行sayhello方法:[name=xiaoma,age=1,work=student,son=null]
AOP后test.Person.sayHello
class.son是否为单例:true
---------------------------------------------------------
error:test方法找不到
AOP前test.Person.sayHello
执行sayhello方法:[name=xieetianya,age=28,work=javaer,son=[name=xiaoma,age=1,work=student,son=null]]
AOP后test.Person.sayHello
true
error:test方法找不到
class.father是否为单例:false
其他文件:
sysconfig.properties
IPerson.java
Person.java
主要文件
AOPFactory.java
AOPMethod.java
AOPParam.java
AOPProxy.java
ParamsUtil.java
先看测试类及结果,全代码已上传文件供下载.
测试类
package test; import core.AOPFactory; public class TestAOP { public static void main(String[] args) { IPerson son = (IPerson) AOPFactory.getBean("class.son"); son.sayHello(); IPerson son2 = (IPerson) AOPFactory.getBean("class.son"); System.out.println("class.son是否为单例:" + (son2 == son)); System.out.println("---------------------------------------------------------"); IPerson father = (IPerson) AOPFactory.getBean("class.father"); father.sayHello(); System.out.println(son == father.getSon()); IPerson father2 = (IPerson) AOPFactory.getBean("class.father"); System.out.println("class.father是否为单例:" + (father2 == father)); } }
运行结果输出:
AOP前test.Person.sayHello
执行sayhello方法:[name=xiaoma,age=1,work=student,son=null]
AOP后test.Person.sayHello
class.son是否为单例:true
---------------------------------------------------------
error:test方法找不到
AOP前test.Person.sayHello
执行sayhello方法:[name=xieetianya,age=28,work=javaer,son=[name=xiaoma,age=1,work=student,son=null]]
AOP后test.Person.sayHello
true
error:test方法找不到
class.father是否为单例:false
其他文件:
sysconfig.properties
# 无.号结尾表示单例 class.son=test.Person class.son.name=java.lang.String-xiaoma class.son.age=int-1 class.son.work=java.lang.String-student # .号结尾非单例 class.father=test.Person. class.father.name=java.lang.String-xieetianya class.father.age=int-28 class.father.work=java.lang.String-javaer # 有意写个错的测试 class.father.test=java.lang.String-haha #ref表示引用 class.father.son=ref-class.son
IPerson.java
package test; public interface IPerson { void sayHello(); int getAge(); void setAge(int age); String getName(); void setName(String name); String getWork(); void setWork(String work); IPerson getSon(); void setSon(IPerson son); String toString(); }
Person.java
package test; public class Person implements IPerson { private String name; private int age; private String work; private IPerson son; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getWork() { return work; } public void setWork(String work) { this.work = work; } public IPerson getSon() { return son; } public void setSon(IPerson son) { this.son = son; } public void sayHello() { System.out.println("执行sayhello方法:" + this); } public String toString() { return "[name=" + name + ",age=" + age + ",work=" + work + ",son=" + son + "]"; } }
主要文件
AOPFactory.java
package core; import java.beans.IntrospectionException; import java.beans.PropertyDescriptor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Hashtable; import java.util.List; import java.util.Map; /** * * AOP工厂类 IOC * * @author QQ:814560828 * @date Jul 4, 2012 */ public class AOPFactory { private static Map<String, Object> map = new Hashtable<String, Object>(); /** * 取代理实例 * * @param name * 在sysconfig.properties配置的变量(类名) * @return */ public static Object getBean(String name) { String clzName = ParamsUtil.get(name); // 判断是否为单例 boolean isSingle = !clzName.endsWith("."); if (!isSingle) { clzName = clzName.substring(0, clzName.length() - 1); } // 单例存在返回结束 if (isSingle && AOPFactory.map.containsKey(name)) { return AOPFactory.map.get(name); } // 单例初始化或非单位初始化 Object result = null; AOPProxy proxy = new AOPProxy(); Object obj = getClassInstance(clzName); // bean设置 if (obj != null) { result = proxy.bind(obj); invoke(obj, name); } // 单例 if (isSingle) { AOPFactory.map.put(name, result); } return result; } /** * 反射生成对象 * * @param clzName * @return */ private static Object getClassInstance(String clzName) { Object obj = null; try { Class<?> clz = Class.forName(clzName); obj = (Object) clz.newInstance(); } catch (ClassNotFoundException cnfe) { System.out.println("ClassNotFoundException:" + cnfe.getMessage()); } catch (Exception e) { e.printStackTrace(); } return obj; } /** * 执行赋值 * * @param obj * @param name */ private static void invoke(Object obj, String name) { List<AOPParam> list = ParamsUtil.getList(name); if (list != null && list.size() != 0) { for (AOPParam param : list) { // java bean 内省赋值 PropertyDescriptor pd = null; String method = param.getName(); try { pd = new PropertyDescriptor(method, obj.getClass()); } catch (IntrospectionException e) { System.out.println("error:" + method + "设置方法找不到"); } if (pd != null) { Method writeMethod = pd.getWriteMethod(); try { writeMethod.invoke(obj, param.getValue()); } catch (IllegalAccessException e) { System.out.println("error:" + method + "设置实例出错"); } catch (IllegalArgumentException e) { System.out.println("error:" + method + "设置参数类型不对"); } catch (InvocationTargetException e) { System.out.println("error:" + method + "设置方法执行出错"); } } // 内省 读属性 // Method readMethod = pd.getReadMethod(); // Object str = readMethod.invoke(obj); // System.out.println(str); } } } }
AOPMethod.java
package core; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * * AOP配置 待优化 * * @author QQ:814560828 * @date Jul 4, 2012 */ public class AOPMethod { /** * 判断是否需要执行aop * * @param obj * 执行的实例 * @param method * 将要执行的方法名 * @return */ public static boolean check(Object obj, String method) { // 这里简单展示设置方法含有say的执行aop Pattern p = Pattern.compile("say"); Matcher m = p.matcher(method); if (m.find()) { return true; } return false; } /** * 方法前执行 * * @param obj * @param method */ public static void before(Object obj, String method) { System.out.println("AOP前" + obj.getClass().getName() + "." + method); } /** * 方法后执行 * * @param obj * @param method */ public static void after(Object obj, String method) { System.out.println("AOP后" + obj.getClass().getName() + "." + method); } }
AOPParam.java
package core; /** * * AOP参数 * * @author QQ:814560828 * @date Jul 4, 2012 */ public class AOPParam { private String name;// 名字 private String type;// 类型 private Object value;// 值 public String getName() { return name; } public void setName(String name) { this.name = name; } public String getType() { return type; } public void setType(String type) { this.type = type; } public Object getValue() { return value; } /** * 待优化 * * @param value */ public void setValue(Object value) { if ("int".equals(type) || "java.lang.Integer".equals(type)) { this.value = Integer.parseInt((String) value); } else if ("short".equals(type) || "java.lang.Short".equals(type)) { this.value = Short.parseShort((String) value); } else if ("long".equals(type) || "java.lang.Long".equals(type)) { this.value = Long.parseLong((String) value); } else if ("float".equals(type) || "java.lang.Float".equals(type)) { this.value = Float.parseFloat((String) value); } else if ("double".equals(type) || "java.lang.Double".equals(type)) { this.value = Double.parseDouble((String) value); } else if ("java.lang.String".equals(type)) { this.value = value; } else if ("ref".equals(type)) { type = ParamsUtil.get((String) value); this.value = AOPFactory.getBean((String) value); } else { this.value = value; } } public String toString() { return "name=" + name + ",value=" + value + ",type=" + type; } }
AOPProxy.java
package core; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Proxy; import java.lang.reflect.Method; /** * * AOP执行类 * * @author QQ:814560828 * @date Jul 4, 2012 */ public class AOPProxy implements InvocationHandler { private Object proxyObj; /** * 绑定类 * * @param obj * @return */ public Object bind(Object obj) { this.proxyObj = obj; return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), this); } /** * 执行方法 */ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object result = null; // 方法执行前 if (AOPMethod.check(proxyObj, method.getName())) { AOPMethod.before(proxyObj, method.getName()); } try { result = method.invoke(proxyObj, args); // 原方法 } catch (Exception e) { e.printStackTrace(); } // 方法执行后 if (AOPMethod.check(proxyObj, method.getName())) { AOPMethod.after(proxyObj, method.getName()); } return result; } }
ParamsUtil.java
package core; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Properties; import java.util.Set; /** * * 读取sysconfig.properties参数 * * @author QQ:814560828 * @date Jul 4, 2012 */ public class ParamsUtil { private static Properties p = new Properties(); static { init(); } public static void init() { try { p.load(ParamsUtil.class.getResourceAsStream("/test/sysconfig.properties")); } catch (IOException e) { System.out.println("加载文件出错/test/sysconfig.properties"); } } public static String get(String key) { return get(key, ""); } /** * 取属性值 * * @param key * @param defaultValue * @return */ public static String get(String key, String defaultValue) { String value = p.getProperty(key); if (value == null || value.equals("")) return defaultValue; return value; } /** * 取key+"."开头的所有参数列表 * * @param key * @return */ public static List<AOPParam> getList(String key) { List<AOPParam> list = new ArrayList<AOPParam>(); Set<Object> set = p.keySet(); String tempKey;// 原始key int tempSplite;// -号索引 String tempValue;// -号后值:类型及值 int tempDotIndex;// .号索引 AOPParam param; for (Object value : set) { tempKey = (String) value; if (tempKey.startsWith(key + ".")) { tempDotIndex = (key + ".").length(); tempValue = get(tempKey); tempSplite = tempValue.indexOf("-"); param = new AOPParam(); param.setName(tempKey.substring(tempDotIndex)); param.setType(tempValue.substring(0, tempSplite)); param.setValue(tempValue.substring(tempSplite + 1)); list.add(param); } } return list; }; }
- src.rar (5.3 KB)
- 下载次数: 8
相关推荐
以注解方式模拟Spring IoC AOP,了解Spring是如何实现的.
spring 整合的典型例子..spring与hibernate的整合...
spring ioc容器初始化流程图 spring ioc容器依赖注入流程图 spring aop实现原理流程图
1.Spring入门和IOC介绍 2.对象依赖 3.AOP入门 4.JDBCTemplate和Spring事务 5.Spring事务原理 6.Spring事务的一个线程安全问题 7.IOC再回顾和面试题 8.AOP再回顾
基于Cglib简单实现Spring体系(Ioc+Aop+Mvc)基于Cglib简单实现Spring体系(Ioc+Aop+Mvc)基于Cglib简单实现Spring体系(Ioc+Aop+Mvc)基于Cglib简单实现Spring体系(Ioc+Aop+Mvc)基于Cglib简单实现Spring体系(Ioc+Aop+Mvc)...
模拟spring ioc aop的功能,理解spring ioc 和 aop 的实现原理和机制。
控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度。其中最常见的方式叫做依赖注入(Dependency Injection,简称DI),还有一种方式叫“依赖查找...
Spring IOC AOP MVC 简单例子
Spring IOC AOP学习示例代码,包含Spring常用操作示例和所有所需jar文件。参考博客:http://blog.csdn.net/daijin888888/article/details/51735291
SpringIOC和AOP实现机制模拟,来自与网络。
spring 的aop的详解如:切面,连接点,通知,切入点,目标对象,代理对象及annotation方式的aop实现和xml方式的事务管理等
本项目包含了两个不同版本的 IoC 和 AOP 实现,相关源码包结构如下: simple,包含了简单的 IoC 和 AOP 实现代码 IoC,包含了较为复杂的 IoC 实现代码 aop,包含了较为复杂的 AOP 实现代码
浅谈 Spring 原理 透析,ioc aop
ioc 及aop 使用基础 与spring结合
自己学习spring课程的笔记。笔记都是根据尚硅谷的课程(spring ioc,spring aop,spring mvc,spring boot等)写的。 主要内容:spring ioc,spring aop,spring mvc,spring boot
spring基础Spring IOC和Spring AOP的原理及使用
《仿 Spring 手写 IoC、AOP 框架》博客附件。 博客地址:https://blog.csdn.net/Supreme_Sir/article/details/109100886
spring ioc aop simulate
spring ioc+aop.zip