0
点赞
收藏
分享

微信扫一扫

我的容器类

zhongjh 2023-04-26 阅读 24


[code]

package com.lxitedu.homework; 


import java.io.BufferedReader; 

import java.io.File; 

import java.io.FileReader; 

import java.lang.reflect.Constructor; 

import java.net.URL; 

import java.util.HashMap; 

import java.util.Map; 


import com.lxitedu.service.LxitClassService; 


import lombok.SneakyThrows; 


public class Container { 


 @SneakyThrows 

 public static Object instance(String param) { 

 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 

 URL url = classLoader.getResource("beanConfig"); 

 File file = new File(url.getFile()); 

 BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); 

 String value = bufferedReader.readLine(); 

 String type = null; 

 String[] array = null; 

 String nestedClass = null; 

 Class<?> classes = null; 

 while (value != null) { 

 array = value.split("="); 

 for (int i = 0; i < array.length; i++) { 

 if (array[i].equals(param)) { 

 if (array[i + 1].contains("(")) { 

 nestedClass = array[i + 1].split("\\(")[i + 1].substring(0, array[i + 1].split("\\(")[i + 1].length() - 1); 

 classes = Class.forName(getBeanPath(nestedClass)); 

 System.out.println("class " + getBeanPath(nestedClass)); 

 int size = array[i + 1].length() - nestedClass.length()-2; 

 type = array[i + 1].substring(0, size); 

 } else { 

 type = array[i + 1]; 

 } 

 } 

 } 

 value = bufferedReader.readLine(); 

 } 

 Constructor<?> constructor = getConstructor(classes, type, array); 

 return getConstructorInstance(type, constructor); 

 } 


 @SneakyThrows 

 public static String getBeanPath(String param) { 

 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 

 URL url = classLoader.getResource("beanConfig"); 

 File file = new File(url.getFile()); 

 BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); 

 String value = bufferedReader.readLine(); 

 String type = null; 

 String[] array = null; 

 while (value != null) { 

 array = value.split("="); 

 for (int i = 0; i < array.length; i++) { 

 if (array[i].trim().equals(param)) { 

 type = array[i + 1]; 

 } 

 } 

 value = bufferedReader.readLine(); 

 } 

 if (type == null) { 

 return null; 

 } else { 

 return type; 

 } 

 } 


 @SneakyThrows 

 private static Constructor<?> getConstructor(Class<?> nestedClass, String type, String[] array) { 

 Class<?> classes = null; 

 Constructor<?> constructor = null; 

 Map<Integer, Class<?>> map = new HashMap<Integer, Class<?>>(); 

 for (int j = 0; j < array.length; j++) { 

 if (map.get(j) == null) { 

 classes = Class.forName(type); 

 if (nestedClass != null) { 

 constructor = classes.getConstructor(nestedClass); 

 } else { 

 constructor = classes.getConstructor(); 

 } 

 map.put(j, classes); 

 } else { 

 classes = map.get(j); 

 } 

 } 

 return constructor; 

 } 


 @SneakyThrows 

 private static Object getConstructorInstance(String type, Constructor<?> constructor) { 

 if (type == null) { 

 return null; 

 } else { 

 return constructor.newInstance(); 

 } 

 } 


 public static void main(String[] args) { 

 LxitClassService test = (LxitClassService) instance("lxitClassService"); 

 System.out.println(test.getList()); 

 } 


}


[/code]

举报

相关推荐

0 条评论