public class SpringLoader {

    private Map<String, Class<?>> classMap = new HashMap<>();

    public void loadJar(String jarPath) throws IOException, ClassNotFoundException {
File file = new File(jarPath);
if (!file.exists()) {
return;
} JarFile jarFile = new JarFile(jarPath);
JarEntry jarEntry;
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URLClassLoader appClassLoader; URL url = new URL("jar", "", "file:" + file.getAbsolutePath() + "!/");
System.out.println("====" + url);
appClassLoader = new URLClassLoader(new URL[]{url}, classLoader);
List<String> xmlList = new ArrayList<>();
Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) {
jarEntry = entries.nextElement();
System.out.println(jarEntry.getName());
if (jarEntry.getName().endsWith("spring.xml")) {
xmlList.add("jar:" + file.toURL().toString() + "!/" + jarEntry.getName());
System.out.println("---====" + file.getAbsolutePath());
System.out.println("+++====" + file.toURL());
}
} Thread.currentThread().setContextClassLoader(appClassLoader);
FileSystemXmlApplicationContext fileSystemXmlApplicationContext = new FileSystemXmlApplicationContext(xmlList.toArray(new String[]{})); Hello hello = fileSystemXmlApplicationContext.getBean(Hello.class);
hello.say();     // or
    
Class<?> clazz = appClassLoader.loadClass("com.demo.MyHello");
Hello hello = (Hello) clazz.newInstance();
hello.say();
    }

    public static void main(String[] args) {
SpringLoader springLoader = new SpringLoader();
try {
springLoader.loadJar("target/target.jar");
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} }

dynamic load jar and init spring的更多相关文章

  1. Unreal: Dynamic load map from Pak file

    Unreal: Dynamic load map from Pak file 目标:在程序运行时加载自定义 Pak 文件,并打开指定关卡,显示其中的完整 map 内容 Unreal 的 Pak 文件内 ...

  2. eclipse&myeclipse 生成jar包后,spring无法扫描到bean定义

    问题:eclipse&myeclipse 生成jar包后,spring无法扫描到bean定义 在使用getbean或者扫包时注入bean失败,但在IDE里是可以正常运行的? 原因:导出jar未 ...

  3. spring.jar是包含有完整发布的单个jar 包,spring.jar中包含除了spring-mock.jar里所包含的内容外其它所有jar包的内容,因为只有在开发环境下才会用到 spring-mock.jar来进行辅助测试,正式应用系统中是用不得这些类的。

    Spring jar包的描述:针对3.2.2以上版本 org.springframework spring-aop ——Spring的面向切面编程,提供AOP(面向切面编程)实现 org.spring ...

  4. 基于注解的Spring MVC整合Hibernate(所需jar包,spring和Hibernate整合配置,springMVC配置,重定向,批量删除)

    1.导入jar watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/400 ...

  5. 基于注解Spring MVC综合Hibernate(需要jar包,spring和Hibernate整合配置,springMVC组态,重定向,)批量删除

    1.进口jar 2.web.xml配置 <?xml version="1.0" encoding="UTF-8"?> <web-app ver ...

  6. Maven聚合、Maven仓库jar包以及Spring+MyBatis+JUnit+Maven整合测试的搭建过程

    一.Maven将父项目创建到父项目的内部 在父项目的pom.xml上 点右键,选择maven-->new-->maven module  project 二.Maven聚合 在某个项目的p ...

  7. loadrunner load generator设置init人数

    Load Generator中还有一个很重要的设置.就是用来设置init人数的,我们在运行脚本的时候会发现,在场景监控中,init默认不会超过50个人,也就是最大并发是50个人,我们想使用超过50个人 ...

  8. spring boot:多模块项目生成jar包(spring boot 2.3.3)

    一,多模块项目的优点: 1,为什么要使用多模块项目? 相比传统的单体工程,使用Maven的多模块配置, 有如下优点: 帮助项目划分模块,鼓励重用, 防止POM变得过于庞大, 方便某个模块的构建,而不用 ...

  9. gcc dynamic load library

    Linux下一般都是直接在编译生成时挂接上链接库,运行时,把链接库放到系统环境里就可以了 但是windows出现带来了动态链接的概念,也就兴起了非windows世界的插件的概念的范潮 对应于windo ...

随机推荐

  1. _itemmod_stat

    制作几种基础模板,用模板快速生成装备 `comment` 备注 `entry`目标装备 entry `src_entry` 模板装备entry `stat_muilt` 属性倍率 `mindmg_mu ...

  2. hdu 1115 Lifting the Stone 多边形的重心

    Lifting the Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  3. Spring boot 整合JSP开发步骤

    1. 新建Springboot项目,war <dependency> <groupId>org.springframework.boot</groupId> < ...

  4. PostgreSQL安装及使用教程二(zip解压方式)

    下载postgre包  下载地址:https://www.postgresql.org/download/ 选DOWNLOADS--Windows--zip archive--version10.6- ...

  5. The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

    关于出现这个·问题的原因貌似也是多种多样的? 在stack overflow上的帖子如下:https://stackoverflow.com/questions/43186315/tomcat-404 ...

  6. python Django 项目创建

    注:后续如不特色说明,使用python版本均为python3 创建项目 django-admin startproject projectName 启动服务 python manage.py runs ...

  7. js获取时间戳(new date()参数获取)

    当获取截止到某一个时间点的时间戳时: 例如:到 2018-03-15 11:03:55 这个时间点的时间戳的时候 正确的写法: var data = new Date("2018/03/15 ...

  8. Skip level 1 on 1

    2019-01-08 16:43:29 Skip level 1:1 什么是 Skip level 1 on  1 :你和你老板的老板(的老板) 1:1 如果你的老板是first line manag ...

  9. 合并k个排序的列表 Merge k Sorted Lists

    2018-11-25 22:58:52 问题描述: 问题求解: 本题可以使用优先队列高效的进行求解,整体的时间复杂度为O(nlogk). public ListNode mergeKLists(Lis ...

  10. 20181013xlVba计算优秀率及合格率

    Sub 计算高一优秀合格率() Dim Wb As Workbook Dim Sht As Worksheet Dim oSht As Worksheet Dim dOs As Object 'Out ...