java获取spring配置文件_Java中spring读取配置文件的几种方法

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

一.新建一个Java Bean:

packagespringdemo;public classHelloBean {privateString helloWorld;publicString getHelloWorld() {returnhelloWorld;

}public voidsetHelloWorld(String helloWorld) {this.helloWorld =helloWorld;

}

}

二.构建一个配置文件bean_config.xml:

Hello!chb!

三.读取配置文件:

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 interfaceBeanFactory {publicObject getBean(String id);

}

//实现类ClassPathXmlApplicationContextimportjava.lang.reflect.Method;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importorg.jdom.Document;importorg.jdom.Element;importorg.jdom.input.SAXBuilder;public class ClassPathXmlApplicationContext implementsBeanFactory {private Map beans = new HashMap();//(IOC:Inverse of Control/DI:Dependency Injection)

public ClassPathXmlApplicationContext() throwsException {

SAXBuilder sb=newSAXBuilder();

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

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.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);

}

}

}publicObject getBean(String id) {returnbeans.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层级关系如下:

18eb443fc560699ab4299cc2f04cdd51.png

2.利用FileSystemResource读取

Resource rs = new FileSystemResource("D:/software/tomcat/webapps/springWebDemo/WEB-INF/classes/bean_config.xml");

BeanFactory factory= newXmlBeanFactory(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 = newDefaultListableBeanFactory();

PropertiesBeanDefinitionReader reader= newPropertiesBeanDefinitionReader(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= newProperties();try{

p.load(inputStream);

}catch(IOException e1) {

e1.printStackTrace();

}

System.out.println("ip:"+p.getProperty("ip")+",port:"+p.getProperty("port"));

三.用接口类WebApplicationContext来取。

privateWebApplicationContext wac;

wac=WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());

wac=WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());

JdbcTemplate jdbcTemplate= (JdbcTemplate)ctx.getBean("jdbcTemplate");

其中,jdbcTemplate为spring配置文件中的一个bean的id值。

这种用法比较灵活,spring配置文件在web中配置启动后,该类会自动去找对应的bean,而不用再去指定配置文件的具体位置。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值