`

spring的xml string applicationcontext实现

阅读更多
这里有两种实现方式:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractXmlApplicationContext;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;

/**
* 字符串形式的Spring ApplicationContext实现。
* 支持动态订阅spring配置的处理
*
* @author linxuan
*
*/
public class StringXmlApplicationContext extends AbstractXmlApplicationContext {
     private Resource[] configResources;

     public StringXmlApplicationContext(String stringXml) {
          this(new String[] { stringXml }, null);
     }

     public StringXmlApplicationContext(String[] stringXmls) {
          this(stringXmls, null);
     }

     public StringXmlApplicationContext(String[] stringXmls, ApplicationContext parent) {
          super(parent);
          this.configResources = new Resource[stringXmls.length];
          for (int i = 0; i < stringXmls.length; i++) {
               this.configResources[i] = new ByteArrayResource(stringXmls[i].getBytes());
          }
          refresh();
     }

     protected Resource[] getConfigResources() {
          return this.configResources;
     }
}


另外一种:
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.support.AbstractXmlApplicationContext;
import org.springframework.core.io.ByteArrayResource;

import java.io.IOException;

public class StringXmlApplicationContext extends AbstractXmlApplicationContext {

    private static final String DEFAULT_CHARSET = "UTF-8";

    /**
     * 必须是UTF-8编码的XML,即XML的第一行必须是<?xml version="1.0" encoding="UTF-8"?>
     */
    private String xml;

    public StringXmlApplicationContext(String xml) {
        this.xml = xml;
        refresh();
    }

    @Override
    protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {
        byte[] bytes = xml.getBytes(DEFAULT_CHARSET);
        ByteArrayResource resource = new ByteArrayResource(bytes);
        reader.loadBeanDefinitions(resource);
    }

}
分享到:
评论

相关推荐

    spring aop 实现源代码--xml and annotation(带lib包)

    在Spring1.2或之前的版本中,实现AOP的传统方式就是通过实现Spring的AOP API来定义Advice,并设置代理对象。Spring根据Adivce加入到业务流程的时机的不同,提供了四种不同的Advice:Before Advice、After Advice、...

    spring applicationContext 配置文件

    &lt;?xml version="1.0" encoding="UTF-8"?&gt; xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" ...

    spring技术入门相关源码

    ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); //输出spring容器 System.out.println(ctx); //打印加载的bean名称 System.out.println(java.util.Arrays....

    springmybatis

    2. Configuration.xml 里面 的&lt;mapper resource="com/yihaomen/mybatis/model/User.xml"/&gt;是包含要映射的类的xml配置文件。 3. 在User.xml 文件里面 主要是定义各种SQL 语句,以及这些语句的参数,以及要返回的类型...

    spring源代码解析

    public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml"; public static final String DEFAULT_CONFIG_LOCATION_PREFIX = "/WEB-INF/"; public static final String DEFAULT...

    尚硅谷]_佟刚_Spring IOC 容器中 Bean 的生命周期.pdf

    3、import用于导入其他配置文件的Bean定义,这是为了加载多个配置文件,当然也可以把这些配置文件构造为一个数组(new String[] {“config1.xml”, config2.xml})传给ApplicationContext实现进行加载多个配置文件,...

    Struts2+Spring3+MyBatis3完整实例

    - Loading XML bean definitions from file [E:\Eclipse_C\workspace\MyStrutsTest\WebRoot\WEB-INF\classes\applicationContext-beans.xml] - JSR-330 'javax.inject.Named' annotation found and supported for ...

    spring_MVC源码

    -- 这里在配成spring,下边也要写一个名为spring-servlet.xml的文件,主要用来配置它的controller --&gt; 19. *.do&lt;/url-pattern&gt; 20. &lt;/servlet-mapping&gt; 21. &lt;welcome-file-list&gt; 22. &lt;welcome-file&gt;index.jsp...

    ActiveMQ-demo

    ApplicationContext ac = new ClassPathXmlApplicationContext("spring/applicationContext-jms-producer.xml"); //获取生产者发送消息服务接口 MessageSender messageSender = (MessageSender)ac.getBean...

    第24次课-1 Spring与Hibernate的整合

    Spring通过ApplicationContext管理SessionFactory,可以不使用Hibernate应用必需的hibernate.cfg.xml。 Spring配置管理SessionFactory与数据库的连接,在实际的应用中,数据源会采用依赖注入的方式,传递给...

    ssh(structs,spring,hibernate)框架中的上传下载

    WEB-INF下的applicationContext.xml为Spring的配置文件,struts-config.xml为Struts的配置文件,file-upload.jsp为文件上传页面,file-list.jsp为文件列表页面。  本文后面的章节将从数据持久层->业务层->Web层的...

    maven相关资料

    ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","dao.xml"}); 或者用通配符: ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/*....

    BOS技术整理-05

    将cxf配置在web.xml中 与applicationContext.xml中声明服务的bean 创建一个maven项目(war) 编写实体类,Service 导入applicationContext.xml 配置cxf的发布 客户端测试代码 WebService-RS ...

    spring AOP

    Resource res = new ClassPathResource("applicationContext.xml"); XmlBeanFactory xbf = new XmlBeanFactory(res); //开启事物 StudentDao dao = (StudentDao) xbf.getBean("studentDao"); dao.save(stu...

    Java打包可执行后的执行脚本cmd,sh,spring

    context.load("classpath:sysconfig/applicationContext.xml"); context.refresh(); System.out.println("开整·~~~~"); CommTool.smsthreadisruning=true; ThreadReadPath thread =...

    feign-1.0.0.jar

    在spring的applicationContext.xml 里面配置 其中 feignScanBasePackage 表示要扫描接口包的地址, &lt;bean id="client" class="com.yax.feign.feignAssembly.OkhttpClient"&gt; 即可声明式远程调用http 使用...

    客户关系管理系统框架搭建(二)

    ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); ISysUserGroupDao sysUserGroupDao = (ISysUserGroupDao) ctx.getBean(ISysUserGroupDao.SERVICE_NAME); ...

    spring框架技术+第2天+xmind思维导图

    bean懒加载lazyInit;...构造器注入:①:ApplicationContext.xml;②:UserServie.java;③:Test.java 属性注入:通过set方法传参数(property) name:属性名称,即setDao()方法中的Dao名称,首字母要小写为dao

    spring-framework-reference-4.1.2

    Null and empty string values ..................................................................... 44 XML shortcut with the p-namespace .......................................................... 44 ...

    spring-framework-reference4.1.4

    Null and empty string values ..................................................................... 44 XML shortcut with the p-namespace .......................................................... 44 ...

Global site tag (gtag.js) - Google Analytics