1 全路径读取jdbc.properties配置文件
2 不全路径读取文件
3 把类中的资源文件作为流
4 分模块开发
5 pom.xml文件的一些依赖设置
6 软件设计的一般原则
7 由ClassBean设置类A,类B的全限定类名,经过反射获取类A,类B
8 创建A,B对象,并且创建context集合装A,B对象,然后执行createb方法完成对对象B的封装
package com.westos.service;
import org.apache.commons.lang3.StringUtils;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Main {
private static Map<String,Object> context=new HashMap<String, Object>();
public static void main(String[] args) throws IllegalAccessException, InstantiationException, ClassNotFoundException, InvocationTargetException {
ClassBean classBean1 = new ClassBean();
classBean1.setId("a1");
classBean1.setName("com.westos.service.A");
ClassBean classBean2 = new ClassBean();
classBean2.setId("b1");
classBean2.setName("com.westos.service.B");
Map<String, Object> hm = new HashMap<>();
hm.put("a","a1");
classBean2.setProperties(hm);
A a1 = (A)create(classBean1); //创建A对象
context.put(classBean1.getId(),a1); //把A对象放进集合
B b1 =(B)create(classBean2); //创建B对象
context.put(classBean2.getId(),b1); //把B对象放进集合
System.out.println(context);
//完成对复杂对象B的装配
createb(context,classBean2); //因为B对象里面有A, B属于复杂对象,所以这里只装配B对象
System.out.println("装配后的结果==="+b1);
b1.getA().doSomething();
}
public static Map<String,Object> getFactory(){
return context;
}
//创建A,B对象
public static Object create(ClassBean classBean) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
Class<?> clc = Class.forName(classBean.getName());
Object obj = clc.newInstance();
return obj;
}
//完成对复杂对象B的装配
public static void createb(Map<String,Object> context,ClassBean classBean) throws InvocationTargetException, IllegalAccessException {
Map<String, Object> map = classBean.getProperties();
if (map!=null){
Set<String> keys = map.keySet();
for (String key:keys){
System.out.println(key); //a
System.out.println(map.get(key)); //a1
invoke(context.get(classBean.getId()),context.get(map.get(key)),key);
} //B对象b1 //A对象a1 //a
}
}
public static void invoke(Object target,Object value,String propertyname) throws InvocationTargetException, IllegalAccessException {
//已知对象target,属性名propertyname,属性值value,给对象赋值
// 找到属性名对应的set方法——使用反射
//
Method[] methodS = target.getClass().getMethods();
String methodname="set"+ StringUtils.capitalize(propertyname);
System.out.println(methodname);
Method method001=null;
for (Method m:methodS){
//此处可能会存在bug,只能代理方法名不重复的
if(m.getName().equals(methodname)){
method001=m;
break;
}
}
//执行set方法
method001.invoke(target,new Object[]{value});
}
}
9 在8的基础上增加了fastjson序列化classbean1,classbean2,以及fastjson进行反序列化
package com.westos.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Main {
private static Map<String,Object> context=new HashMap<String, Object>();
public static void main(String[] args) throws IllegalAccessException, InstantiationException, ClassNotFoundException, InvocationTargetException {
ClassBean classBean1 = new ClassBean();
classBean1.setId("a1");
classBean1.setName("com.westos.service.A");
ClassBean classBean2 = new ClassBean();
classBean2.setId("b1");
classBean2.setName("com.westos.service.B");
Map<String, Object> hm = new HashMap<>();
hm.put("a","a1");
classBean2.setProperties(hm);
//使用fastjson序列化classbean1,classbean2
JSONArray jsonArray=new JSONArray();
jsonArray.add(classBean1);
jsonArray.add(classBean2);
System.out.println(jsonArray.toJSONString());
A a1 = (A)create(classBean1); //创建A对象
context.put(classBean1.getId(),a1); //把A对象放进集合
B b1 =(B)create(classBean2); //创建B对象
context.put(classBean2.getId(),b1); //把B对象放进集合
System.out.println(context);
//完成对复杂对象B的装配
createb(context,classBean2); //因为B对象里面有A, B属于复杂对象,所以这里只装配B对象
System.out.println("装配后的结果==="+b1);
b1.getA().doSomething();
}
//使用fastjson进行反序列化
public static void init() throws IOException, IllegalAccessException, InstantiationException, ClassNotFoundException, InvocationTargetException {
String text = new String();
//读取文件ioc.json
String currentpath="";
currentpath=System.getProperty("user.dir"); // C\\IdeaProjects\\hello4
System.out.println(currentpath);
String filename=currentpath+File.separator+"src"+File.separator+"main"+File.separator+"resources"+File.separator+"ioc.json";
File file = new File(filename);
FileInputStream in = new FileInputStream(file);
text= IOUtils.toString(in,"utf-8");
List<ClassBean> list = JSON.parseArray(text, ClassBean.class);
for (ClassBean classBean:list){
context.put(classBean.getId(),create(classBean));
}
for (ClassBean classBean:list){
createb(context,classBean);
}
}
public static Map<String,Object> getFactory(){
return context;
}
//创建A,B对象
public static Object create(ClassBean classBean) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
Class<?> clc = Class.forName(classBean.getName());
Object obj = clc.newInstance();
return obj;
}
//完成对复杂对象B的装配
public static void createb(Map<String,Object> context,ClassBean classBean) throws InvocationTargetException, IllegalAccessException {
Map<String, Object> map = classBean.getProperties();
if (map!=null){
Set<String> keys = map.keySet();
for (String key:keys){
System.out.println(key); //a
System.out.println(map.get(key)); //a1
invoke(context.get(classBean.getId()),context.get(map.get(key)),key);
} //B对象b1 //A对象a1 //a
}
}
public static void invoke(Object target,Object value,String propertyname) throws InvocationTargetException, IllegalAccessException {
//已知对象target,属性名propertyname,属性值value,给对象赋值
// 找到属性名对应的set方法——使用反射
//
Method[] methodS = target.getClass().getMethods();
String methodname="set"+ StringUtils.capitalize(propertyname);
System.out.println(methodname);
Method method=null;
for (Method m:methodS){
//此处可能会存在bug,只能代理方法名不重复的
if(m.getName().equals(methodname)){
method=m;
break;
}
}
//执行set方法
method.invoke(target,new Object[]{value});
}
}
10 在9的基础上增加springioc的测试
11 Subject与RealSubject与ProxySubject
ProxySubject这里只能代理一种类型的对象RealSubject realSubject
ProxyObject 这里能代理任意类型的对象Object realObject
使用ProxyHandler实现动态代理多个类型对象
使用ProxyHandler通过DAOHandler举例