基于反射启动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. ZUI分页器的使用案例(ECLIPSE SMS项目)

    不足:并未编写导出功能 HTML代码: <form action="${basePath}/SMSLogList.cwai" method="post" ...

  2. 前端(九):react生命周期

    一.组件渲染 当组件的props或者state发生改变时,组件会自动调用render方法重新渲染.当父组件被重新渲染时,子组件也会被递归渲染.那么组件是如何渲染的呢? # 方案一 1.state数据 ...

  3. Why we should overwrite the hashCode() when we overwrite the equals()

    Preface Though I have used Java programme language for almost a year, I'm not familiar with a notion ...

  4. SpringMVC整合Shiro安全框架(一)

    一. 准备工作 1. 本文参考自张开涛的 <跟我学Shiro> 二. 简介 1. Apache Shiro是Java的一个安全框架.可以帮助我们完成:认证.授权.加密.会话管理.与Web集 ...

  5. 高并发第十一弹:J.U.C -AQS(AbstractQueuedSynchronizer) 组件:Lock,ReentrantLock,ReentrantReadWriteLock,StampedLock

    既然说到J.U.C 的AQS(AbstractQueuedSynchronizer)   不说 Lock 是不可能的.不过实话来说,一般 JKD8 以后我一般都不用Lock了.毕竟sychronize ...

  6. c#之new关键词——隐藏基类方法

    当从基类继承了一个(非抽象成员时),也就继承了父类的实现代码.如果是virtual成员,可以override:另外一种方法也能隐藏父类的实现代码(虚成员和非虚成员都可使用):定义与父类相同的方法名,加 ...

  7. 获取本地内网和外网IP地址

    public class IPUtil { /// <summary> /// 获取本地内网IP /// </summary> /// <returns></ ...

  8. Vue项目打包报错Failed to load resource: net::ERR_FILE_NOT_FOUND

    webpack.prod.conf.js 中output添加参数publicPath:'./' 修改webpack.base.conf.js中: publicPath: process.env.NOD ...

  9. p2p登录拦截

    1.编写拦截标签 package com.xmg.p2p.base.util; import java.lang.annotation.ElementType; import java.lang.an ...

  10. Git『Everything up-to-date』问题解决

    今天提交代码的时候遇到了一个小问题,这里解决了记下小记. 提交代码遇到『Everything up-to-date』 上网查了下,发现中文大多答非所问,少数能解决的并没有阐述原理,所以我写这篇文章记录 ...