基于反射启动Spring容器

package com.maple.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List; /**
* author: HuaZhe Ray
* <p>
* describe: TODO
* <p>
* createDate: 2018/1/2
* createTime: 16:16
*/
public class TestSpring { public static void main(String[] args) throws Exception { List<String> xmlPaths = new ArrayList<>(); Enumeration<URL> resources = TestSpring.class.getClassLoader().getResources("services.xml"); while (resources.hasMoreElements()) {
URL nextElement = resources.nextElement(); // not load isuwang-soa-transaction-impl
if (!nextElement.getFile().matches(".*dapeng-transaction-impl.*"))
xmlPaths.add(nextElement.toString());
} // ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new Object[]{xmlPaths.toArray(new String[0])});
// context.start();
Class<?> appClass = TestSpring.class.getClassLoader().loadClass("org.springframework.context.support.ClassPathXmlApplicationContext"); Class<?>[] parameterTypes = new Class[]{String[].class};
//根据参数 反射构造器
Constructor<?> constructor = appClass.getConstructor(parameterTypes); Object context = constructor.newInstance(new Object[]{xmlPaths.toArray(new String[0])}); // ApplicationContext context1 = new ClassPathXmlApplicationContext("services.xml"); // context1.getBean("testService"); Method startMethod = appClass.getMethod("start"); startMethod.invoke(context); Method m = appClass.getMethod("getBean", String.class); TestService service = (TestService) m.invoke(context, "testService");
service.foo(); } }

基于反射启动Spring容器的更多相关文章

  1. 8 -- 深入使用Spring -- 7...1 启动Spring 容器

    8.7.1 启动Spring容器 对于使用Spring的Web应用,无须手动创建Spring容器,而是通过配置文件声明式地创建Spring容器.因此,在Web应用中创建Spring容器有如下两种方式: ...

  2. Spring系列9:基于注解的Spring容器配置

    写在前面 前面几篇中我们说过,Spring容器支持3种方式进行bean定义信息的配置,现在具体说明下: XML:bean的定义和依赖都在xml文件中配置,比较繁杂. Annotation-based ...

  3. 【Java】利用反射执行Spring容器Bean指定的方法,支持多种参数自动调用

    目录 使用情景 目的 实现方式 前提: 思路 核心类 测试方法 源码分享 使用情景 将定时任务录入数据库(这样做的好处是定时任务可视化,也可以动态修改各个任务的执行时间),通过反射执行对应的方法: 配 ...

  4. 从头看看Tomcat启动Spring容器的原理

    通过带注解Spring Boot可以启动一个web容器,并初始化bean容器.那么Tomcat启动并初始化spring容器的原理是怎样的? Tomcat启动web程序时会创建一对父子容器(图1): 有 ...

  5. 基于注解的Spring容器源码分析

    从spring3.0版本引入注解容器类之后,Spring注解的使用就变得异常的广泛起来,到如今流行的SpringBoot中,几乎是全部使用了注解.Spring的常用注解有很多,有@Bean,@Comp ...

  6. springMVC项目部署 服务器启动spring容器报错bean未从类加载器中找到

    bean未从类加载器中找到 警告: Exception encountered during context initialization - cancelling refresh attempt: ...

  7. ServletContext与Web应用以及Spring容器启动

    一.ServletContext对象获取Demo Servlet容器在启动时会加载Web应用,并为每个Web应用创建唯一的ServletContext对象. 可以把ServletContext看作一个 ...

  8. spring容器启动

    1 主要类 ContextLoaderListener:注册在web.xml中,web应用启动时,会创建它,并回调它的initWebApplicationContext()方法,从而创建并启动spri ...

  9. spring容器启动的三种方式

    一.在Web项目中,启动Spring容器的方式有三种,ContextLoaderListener.ContextLoadServlet.ContextLoaderPlugin. 1.1.监听器方式: ...

随机推荐

  1. iOS交互h5— JavaScriptCore ---UIWebview

    JavaScriptCore这个框架,从而让web页面和本地原生应用交互起来非常方便,而且使用此框架可以做到Android那边和iOS相对统一, web前端 在三端交互中,web前端开发人员来定义,让 ...

  2. Android sdk manager 下载速度慢的问题

    不多说了,直接附上方法: 首先打开Ecplise 中Android sdk manager,打开后, 在此窗口的上方打开偏好设置选项,然后在里面设置HTTP Proxy server和HTTP Pro ...

  3. hdu 2030 统计汉字个数

    汉字统计 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  4. 洛谷P4027 [NOI2007]货币兑换(dp 斜率优化 cdq 二分)

    题意 题目链接 Sol 解题的关键是看到题目里的提示... 设\(f[i]\)表示到第\(i\)天所持有软妹币的最大数量,显然答案为\(max_{i = 1}^n f[i]\) 转移为\(f_i = ...

  5. cf623A. Graph and String(二分图 构造)

    题意 题目链接 Sol 可以这样考虑,在原图中没有边相连的点的值肯定是a / c 那么直接二分图染色即可 #include<bits/stdc++.h> #define LL long l ...

  6. linux解压tar.gz

    gnuzip或者tar -zxvf file.tar.gz unzip file.zip

  7. spring作用、spring注解、管理对象的作用域与生命周期、自动装配、Spring的框架包有哪些作用是什么

    Spring 1. 作用 创建和管理对象,使得开发过程中,可以不必使用new关键字创建对象,而是直接获取对象!并且,还可以通过一些配置,使得某些获取到的对象,其中某些属性已经是被赋值的! 2. Spr ...

  8. Ubuntu搜索不到WiFi的解决办法

    时间:2018年1月25日 废话连篇:杭州下了第一场雪,冒险严寒来到实验室,打开电脑,纳尼连不上wifi了,好吧!不要被这件小事影响心情,开始修复了,经过一顿搜索,可能是因为驱动的问题,终端输入以下两 ...

  9. spring <context:annotation-config/> 注解作用

    <context:component-scan>包含<context:annotation-config/>的作用 <context:annotation-config/ ...

  10. web项目启动时,自动执行代码的几种方式

    在项目开发过程中,往往需要一些功能随着项目启动而优先启动,下面我总结几种方式(非spring boot) spring boot的参考 spring boot 学习之路9 (项目启动后就执行特定方法) ...