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. 如何创建R包并将其发布在 CRAN / GitHub 上--转载

    转载--https://www.analyticsvidhya.com/blog/2017/03/create-packages-r-cran-github/ 什么是 R 包?我开始创建 R 包的原因 ...

  2. 利用angularjs完成注册表单

    ng-init="username = 'first'"设置初始显示first字段 ng-class="{'error':signUpForm.username.$inv ...

  3. CIKM 18 | 蚂蚁金服论文:基于异构图神经网络的恶意账户识别方法

    小蚂蚁说: ACM CIKM 2018 全称是 The 27th ACM International Conference on Information and Knowledge Managemen ...

  4. Ubuntu18.04的网络配置

    网卡与DNS配置 1)打开命令窗口(右键单机桌面选择Open Terminal或者用快捷键Ctrl+Alt+T打开终端),输入ip a查看自己的网卡编号 2)输入命令sudo vim /etc/net ...

  5. MySQL基本使用

    来自李兴华视频. 1. 启动命令行方式 2. 连接mysql数据库,其中“-u”标记的是输入用户名,“-p”标记的是输入密码. 3. 建立一个新数据库——mldn,使用UTF-8编码: create ...

  6. R语言网页爬虫

    R 是统计计算和数据分析的利器.给定一个数据集,利用前几章介绍到的 R 中灵活的数据结构或高性能计算,我们可以很方便地进行数据转换.建模和数值分析.一般来说,商业数据库会将数据以表格的形式很好地组织起 ...

  7. 记录Python类与继承的一个错误

    今天在学python的类与继承的时候遇到一个错误,原来是自己在ctrl+c  ctrl+v的时候漏了一个括号 class Car(): def __init__(self,make,year,mode ...

  8. vue 脚手架搭建新项目以及element-ui等vue组件的使用

    vue快速搭建项目(前提是你的电脑已经安装了node的环境和vue脚手架安装,不会的自行百度) 1:打开终端: 这里说下此时位置是在User下的lijuntao文件夹下面,我一般会在桌面新建一个文件夹 ...

  9. Unity中角度与弧度之间的相互转换

    弧度数 = 角度数 * Mathf.Deg2Rad角度数 = 弧度数 * Mathf.Rad2Deg

  10. python中的面向对象学习以及类的多态

    接下来类的第三个重要的特性:多态(一种接口,多种实现) 多态性(polymorphisn)是允许你将父对象设置成为和一个或更多的他的子对象相等的技术,赋值之后,父对象就可以根据当前赋值给它的子对象的特 ...