Spring读取配置XML文件分三步:

一.新建一个Java Bean:
package springdemo;
public class HelloBean {
private String helloWorld;
public String getHelloWorld() {
return helloWorld;
}
public void setHelloWorld(String helloWorld) {
this.helloWorld = helloWorld;
}
}
二.构建一个配置文件bean_config.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" > <beans> <bean id="helloBean" class="springdemo.HelloBean"> <property name="helloWorld"> <value>Hello!chb!</value> </property> </bean> </beans>
三.读取配置文件:
1.利用ClassPathXmlApplicationContext:
ApplicationContext context = new ClassPathXmlApplicationContext("bean_config.xml");
//这种用法不够灵活,不建议使用。
HelloBean helloBean = (HelloBean)context.getBean("helloBean");
System.out.println(helloBean.getHelloWorld());
ClassPathXmlApplicationContext实现了接口ApplicationContext,ApplicationContext实现了BeanFactory。其通过jdom进行XML配置文件的读取,并构建实例化Bean,放入容器内。
public interface BeanFactory {
public Object getBean(String id);
}
//实现类ClassPathXmlApplicationContext
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
public class ClassPathXmlApplicationContext implements BeanFactory {
private Map<String , Object> beans = new HashMap<String, Object>();
//(IOC:Inverse of Control/DI:Dependency Injection)
public ClassPathXmlApplicationContext() throws Exception {
SAXBuilder sb=new SAXBuilder();
Document doc=sb.build(this.getClass().getClassLoader().getResourceAsStream("beans.xml")); //构造文档对象
Element root=doc.getRootElement(); //获取根元素HD
List list=root.getChildren("bean");//取名字为disk的所有元素
for(int i=0;i<list.size();i++){
Element element=(Element)list.get(i);
String id=element.getAttributeValue("id");
String clazz=element.getAttributeValue("class");
Object o = Class.forName(clazz).newInstance();
System.out.println(id);
System.out.println(clazz);
beans.put(id, o);
for(Element propertyElement : (List<Element>)element.getChildren("property")) {
String name = propertyElement.getAttributeValue("name"); //userDAO
String bean = propertyElement.getAttributeValue("bean"); //u
Object beanObject = beans.get(bean);//UserDAOImpl instance
String methodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
System.out.println("method name = " + methodName);
Method m = o.getClass().getMethod(methodName, beanObject.getClass().getInterfaces()[0]);
m.invoke(o, beanObject);
}
}
}
public Object getBean(String id) {
return beans.get(id);
}
}
BeanFactory是一个很根的接口,ApplicationContext和ClassPathXmlApplicationContext都实现了接口BeanFactory,所以也可以这么写:
ApplicationContext context = new ClassPathXmlApplicationContext("bean_config.xml");
HelloBean helloBean = (HelloBean)context.getBean("helloBean");
BeanFactory factory= new ClassPathXmlApplicationContext("bean_config.xml");
HelloBean helloBean = (HelloBean)factory.getBean("helloBean");
ClassPathXmlApplicationContext层级关系如下:
2.利用FileSystemResource读取
Resource rs = new FileSystemResource("D:/software/tomcat/webapps/springWebDemo/WEB-INF/classes/bean_config.xml");
BeanFactory factory = new XmlBeanFactory(rs);
HelloBean helloBean = (HelloBean)factory.getBean("helloBean");
System.out.println(helloBean.getHelloWorld());
注意:利用FileSystemResource,则配置文件必须放在project直接目录下,或者写明绝对路径,否则就会抛出找不到文件的异常。
Spring读取properties配置文件
介绍两种技术:利用spring读取properties 文件和利用java.util.Properties读取:
一.利用spring读取properties 文件
还利用上面的HelloBean.java文件,构造如下bean_config.properties文件:
helloBean.class=springdemo.HelloBean helloBean.helloWorld=Hello!HelloWorld!
属性文件中的"helloBean"名称即是Bean的别名设定,.class用于指定类来源。
然后利用org.springframework.beans.factory.support.PropertiesBeanDefinitionReader来读取属性文件。
BeanDefinitionRegistry reg = new DefaultListableBeanFactory();
PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(reg);
reader.loadBeanDefinitions(new ClassPathResource("bean_config.properties"));
BeanFactory factory = (BeanFactory)reg;
HelloBean helloBean = (HelloBean)factory.getBean("helloBean");
System.out.println(helloBean.getHelloWorld());
二.利用java.util.Properties读取属性文件
比如,我们构造一个ip_config.properties来保存服务器ip地址和端口,如:
ip=192.168.0.1
port=8080
我们可以用如下程序来获得服务器配置信息:
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("ip_config.properties");
Properties p = new Properties();
try {
p.load(inputStream);
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println("ip:"+p.getProperty("ip")+",port:"+p.getProperty("port"));
三.用接口类WebApplicationContext来取。
private WebApplicationContext wac;
wac =WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
JdbcTemplate jdbcTemplate = (JdbcTemplate)ctx.getBean("jdbcTemplate");
其中,jdbcTemplate为spring配置文件中的一个bean的id值。
这种用法比较灵活,spring配置文件在web中配置启动后,该类会自动去找对应的bean,而不用再去指定配置文件的具体位置。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# spring
# java读取配置
# 读取配置文件
# 读取配置
# Java Spring读取和存储详细操作
# Java Spring框架创建项目与Bean的存储与读取详解
# java springboot中如何读取配置文件的属性
# java(包括springboot)读取resources下文件方式实现
# Java(springboot) 读取txt文本内容代码实例
# Java之Spring简单的读取和存储对象
# 配置文件
# 实现了
# 是一个
# 就会
# 放在
# 找不到
# 两种
# 可以用
# 去找
# 再去
# 即是
# 抛出
# 大家多多
# 新建一个
# 服务器配置
# 构建一个
# 容器内
# 取名字
# 文档
# 目录下
相关文章:
长沙做网站要多少钱,长沙国安网络怎么样?
相册网站制作软件,图片上的网址怎么复制?
Python路径拼接规范_跨平台处理说明【指导】
网站制作外包价格怎么算,招聘网站上写的“外包”是什么意思?
建站之星如何一键生成手机站?
齐河建站公司:营销型网站建设与SEO优化双核驱动策略
黑客如何利用漏洞与弱口令入侵网站服务器?
魔方云NAT建站如何实现端口转发?
成都品牌网站制作公司,成都营业执照年报网上怎么办理?
如何通过建站之星自助学习解决操作问题?
,网页ppt怎么弄成自己的ppt?
网站建设设计制作营销公司南阳,如何策划设计和建设网站?
非常酷的网站设计制作软件,酷培ai教育官方网站?
视频网站app制作软件,有什么好的视频聊天网站或者软件?
如何通过虚拟机搭建网站?详细步骤解析
建站主机解析:虚拟主机配置与服务器选择指南
建站之星伪静态规则如何正确配置?
深圳网站制作案例,网页的相关名词有哪些?
建站之星代理费用多少?最新价格详情介绍
深圳网站制作的公司有哪些,dido官方网站?
如何实现建站之星域名转发设置?
网站制作培训多少钱一个月,网站优化seo培训课程有哪些?
免费视频制作网站,更新又快又好的免费电影网站?
javascript基本数据类型及类型检测常用方法小结
建站之星后台管理系统如何操作?
怀化网站制作公司,怀化新生儿上户网上办理流程?
建站之星导航如何优化提升用户体验?
在线ppt制作网站有哪些,请推荐几个好的课件下载的网站?
网站制作大概多少钱一个,做一个平台网站大概多少钱?
最好的网站制作公司,网购哪个网站口碑最好,推荐几个?谢谢?
c# await 一个已经完成的Task会发生什么
如何破解联通资金短缺导致的基站建设难题?
如何在Tomcat中配置并部署网站项目?
如何快速搭建高效WAP手机网站吸引移动用户?
如何自定义建站之星模板颜色并下载新样式?
建站主机选购指南:核心配置优化与品牌推荐方案
如何用IIS7快速搭建并优化网站站点?
如何快速搭建安全的FTP站点?
建站之星云端配置指南:模板选择与SEO优化一键生成
如何通过服务器快速搭建网站?完整步骤解析
网站制作与设计教程,如何制作一个企业网站,建设网站的基本步骤有哪些?
宝塔面板创建网站无法访问?如何快速排查修复?
青岛网站建设如何选择本地服务器?
如何获取开源自助建站系统免费下载链接?
代购小票制作网站有哪些,购物小票的简要说明?
建站之星五站合一营销型网站搭建攻略,流量入口全覆盖优化指南
c# 在高并发场景下,委托和接口调用的性能对比
*服务器网站为何频现安全漏洞?
如何快速生成高效建站系统源代码?
金*站制作公司有哪些,金华教育集团官网?
*请认真填写需求信息,我们会在24小时内与您取得联系。