这里有两种实现方式:
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);
}
}
分享到:
相关推荐
在Spring1.2或之前的版本中,实现AOP的传统方式就是通过实现Spring的AOP API来定义Advice,并设置代理对象。Spring根据Adivce加入到业务流程的时机的不同,提供了四种不同的Advice:Before Advice、After Advice、...
<?xml version="1.0" encoding="UTF-8"?> xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" ...
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); //输出spring容器 System.out.println(ctx); //打印加载的bean名称 System.out.println(java.util.Arrays....
2. Configuration.xml 里面 的<mapper resource="com/yihaomen/mybatis/model/User.xml"/>是包含要映射的类的xml配置文件。 3. 在User.xml 文件里面 主要是定义各种SQL 语句,以及这些语句的参数,以及要返回的类型...
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...
3、import用于导入其他配置文件的Bean定义,这是为了加载多个配置文件,当然也可以把这些配置文件构造为一个数组(new String[] {“config1.xml”, config2.xml})传给ApplicationContext实现进行加载多个配置文件,...
- 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,下边也要写一个名为spring-servlet.xml的文件,主要用来配置它的controller --> 19. *.do</url-pattern> 20. </servlet-mapping> 21. <welcome-file-list> 22. <welcome-file>index.jsp...
ApplicationContext ac = new ClassPathXmlApplicationContext("spring/applicationContext-jms-producer.xml"); //获取生产者发送消息服务接口 MessageSender messageSender = (MessageSender)ac.getBean...
Spring通过ApplicationContext管理SessionFactory,可以不使用Hibernate应用必需的hibernate.cfg.xml。 Spring配置管理SessionFactory与数据库的连接,在实际的应用中,数据源会采用依赖注入的方式,传递给...
WEB-INF下的applicationContext.xml为Spring的配置文件,struts-config.xml为Struts的配置文件,file-upload.jsp为文件上传页面,file-list.jsp为文件列表页面。 本文后面的章节将从数据持久层->业务层->Web层的...
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","dao.xml"}); 或者用通配符: ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/*....
将cxf配置在web.xml中 与applicationContext.xml中声明服务的bean 创建一个maven项目(war) 编写实体类,Service 导入applicationContext.xml 配置cxf的发布 客户端测试代码 WebService-RS ...
Resource res = new ClassPathResource("applicationContext.xml"); XmlBeanFactory xbf = new XmlBeanFactory(res); //开启事物 StudentDao dao = (StudentDao) xbf.getBean("studentDao"); dao.save(stu...
context.load("classpath:sysconfig/applicationContext.xml"); context.refresh(); System.out.println("开整·~~~~"); CommTool.smsthreadisruning=true; ThreadReadPath thread =...
在spring的applicationContext.xml 里面配置 其中 feignScanBasePackage 表示要扫描接口包的地址, <bean id="client" class="com.yax.feign.feignAssembly.OkhttpClient"> 即可声明式远程调用http 使用...
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); ISysUserGroupDao sysUserGroupDao = (ISysUserGroupDao) ctx.getBean(ISysUserGroupDao.SERVICE_NAME); ...
bean懒加载lazyInit;...构造器注入:①:ApplicationContext.xml;②:UserServie.java;③:Test.java 属性注入:通过set方法传参数(property) name:属性名称,即setDao()方法中的Dao名称,首字母要小写为dao
Null and empty string values ..................................................................... 44 XML shortcut with the p-namespace .......................................................... 44 ...
Null and empty string values ..................................................................... 44 XML shortcut with the p-namespace .......................................................... 44 ...