基于反射启动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. C# 分页方法

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Web; ...

  2. Eclipse中让Scala缩进变为4

    Windows->preference->Scala->Editor->Formatter->Spaces to indent

  3. 【原】使用Maven完成自动化打包并部署到Linux服务器下(Tomcat7)

    最近在使用maven,顺便尝试了下tomcat部署.网上找到了很多资料但是都不是最新的,所以贴上比较新的Tomcat7部署代码和配置,方便以后回顾-->测试OK. 1. 首先是配置Tomcat ...

  4. 对io进行分流

    package org.richin.io.Stream.util;     import java.io.BufferedInputStream;     import java.io.Buffer ...

  5. Spring boot 入门四:spring boot 整合mybatis 实现CRUD操作

    开发环境延续上一节的开发环境这里不再做介绍 添加mybatis依赖 <dependency> <groupId>org.mybatis.spring.boot</grou ...

  6. python 函数私有方法

    #coding:utf-8 class A(object): def _test1(self): print('this is _test1') def test2(self): print('thi ...

  7. CSS选择器之伪类选择器(交互)

    :link 选取未访问链接 :active 选取活动链接(单击某链接,未松开鼠标时). :visited 选取已被访问的链接. :target 链接指定的目标 :hover 当鼠标浮在元素上方时. : ...

  8. CSDN博客大事日记1

    一.       2016-10-18,申请了博客专家,但是因为PV不够,所以很荣幸的成为了一名CSDN准博客专家,接下,得更加努力了争取早日成为博客专家,在此立帖为证哦.               ...

  9. Elixir木蚂蚁支付服务器验签名方法

    官方范例为java public boolean verify(String sign , String appKey , String orderId) throws UnsupportedEnco ...

  10. 使用WebDAV实现Office文档在线编辑

    Office的文档处理能力是非常强大的,但是它是本地资源,在Office Web App尚未成熟前,仍需要使用本地能力来进行文档编辑,可是现代的系统的主流却是B/S,所以在B/S中调用本地的Offic ...