基于反射启动Spring容器
基于反射启动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容器的更多相关文章
- 8 -- 深入使用Spring -- 7...1 启动Spring 容器
8.7.1 启动Spring容器 对于使用Spring的Web应用,无须手动创建Spring容器,而是通过配置文件声明式地创建Spring容器.因此,在Web应用中创建Spring容器有如下两种方式: ...
- Spring系列9:基于注解的Spring容器配置
写在前面 前面几篇中我们说过,Spring容器支持3种方式进行bean定义信息的配置,现在具体说明下: XML:bean的定义和依赖都在xml文件中配置,比较繁杂. Annotation-based ...
- 【Java】利用反射执行Spring容器Bean指定的方法,支持多种参数自动调用
目录 使用情景 目的 实现方式 前提: 思路 核心类 测试方法 源码分享 使用情景 将定时任务录入数据库(这样做的好处是定时任务可视化,也可以动态修改各个任务的执行时间),通过反射执行对应的方法: 配 ...
- 从头看看Tomcat启动Spring容器的原理
通过带注解Spring Boot可以启动一个web容器,并初始化bean容器.那么Tomcat启动并初始化spring容器的原理是怎样的? Tomcat启动web程序时会创建一对父子容器(图1): 有 ...
- 基于注解的Spring容器源码分析
从spring3.0版本引入注解容器类之后,Spring注解的使用就变得异常的广泛起来,到如今流行的SpringBoot中,几乎是全部使用了注解.Spring的常用注解有很多,有@Bean,@Comp ...
- springMVC项目部署 服务器启动spring容器报错bean未从类加载器中找到
bean未从类加载器中找到 警告: Exception encountered during context initialization - cancelling refresh attempt: ...
- ServletContext与Web应用以及Spring容器启动
一.ServletContext对象获取Demo Servlet容器在启动时会加载Web应用,并为每个Web应用创建唯一的ServletContext对象. 可以把ServletContext看作一个 ...
- spring容器启动
1 主要类 ContextLoaderListener:注册在web.xml中,web应用启动时,会创建它,并回调它的initWebApplicationContext()方法,从而创建并启动spri ...
- spring容器启动的三种方式
一.在Web项目中,启动Spring容器的方式有三种,ContextLoaderListener.ContextLoadServlet.ContextLoaderPlugin. 1.1.监听器方式: ...
随机推荐
- 【拓扑 && 模板】Kosaraju算法
#include<bits/stdc++.h> using namespace std; ; vector <int> g1[maxn],g2[maxn]; stack < ...
- 关于jquery的入门,简单的封装。
看过不同的博客,觉得以下的博客写的比较简洁明了,通俗易懂. 关于jquery博客:http://www.cnblogs.com/moqiutao/p/6523924.html 关于js:http:// ...
- 使用springcloud gateway搭建网关(分流,限流,熔断)
Spring Cloud Gateway Spring Cloud Gateway 是 Spring Cloud 的一个全新项目,该项目是基于 Spring 5.0,Spring Boot 2.0 和 ...
- 使用外部属性文件配置Bean以及Bean的生命周期方法
1.使用外部属性文件配置Bean 在配置文件里配置 Bean 时, 有时需要在 Bean 的配置里混入系统部署的细节信息(例如: 文件路径, 数据源配置信息等). 而这些部署细节实际上需要和 Bean ...
- Eclipse发布的Dynamical web项目在Tomacat文件夹下显示
Eclipse设置了Tomacat后,项目信息会在你的workspace上,在Tomacat文件夹上是没有的.但是通过设置是可以在Tomacat文件夹上存在的. 配置好服务器后,先关闭服务器,然后在E ...
- js-NodeList对象和HTMLCollection对象
getElementsByName()和getElementsByTagName()都返回NodeList对象,而类似document.images和document.forms的属性为HTMLCol ...
- js-权威指南学习笔记10
第十章 正则表达式的模式匹配 1.正则表达式是一个描述字符模式的对象. 2.可以使用RegExp()构造函数来创建RegExp对象,不过RegExp对象更多是通过一种特殊的直接量语法来创建. 3.程序 ...
- 浏览器根对象window之事件
1. [事件]动画 onanimationstart onanimationend onanimationiteration onwebkitanimationend onwebkitanimatio ...
- Pig foreach用法举例
foreach:一行一行的遍历数据,处理一行的数据,然后返回一个tuple. users = load '/users.data'; 1)别名引用 f = foreach users genera ...
- 五、vue常用UI组件
下面简单的总结下vue常用的一些UI 组件,有一些我也没怎么用过,这里先罗列出来,便于自己后面使用的时候查找方便,大家有更好的可以给我推荐哦~ vuex: vux github ui demo:htt ...