Java中spring读取配置⽂件的⼏种⽅法⽰例
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;
}
}
⼆.构建⼀个配置⽂件l:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "/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("l");
//这种⽤法不够灵活,不建议使⽤。
HelloBean helloBean = (Bean("helloBean");
System.out.HelloWorld());
ClassPathXmlApplicationContext实现了接⼝ApplicationContext,ApplicationContext实现了BeanFactory。其通过jdom进⾏XML配置⽂件的读取,并构建实例化Bean,放⼊容器内。
public interface BeanFactory {
public Object getBean(String id);
}
//实现类ClassPathXmlApplicationContext
import flect.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.Class().getClassLoader().getResourceAsStream("l")); //构造⽂档对象
Element RootElement(); //获取根元素HD
List Children("bean");//取名字为disk的所有元素
for(int i=0;i<list.size();i++){
Element element=((i);
String AttributeValue("id");
String AttributeValue("class");莫小棋胸围
Object o = Class.forName(clazz).newInstance();
System.out.println(id);
System.out.println(clazz);
beans.put(id, o);
for(Element propertyElement : (List<Element>)Children("property")) {
String name = AttributeValue("name"); //userDAO
String bean = AttributeValue("bean"); //u
Object beanObject = (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, Class().getInterfaces()[0]);
m.invoke(o, beanObject);
}
}
}
public Object getBean(String id) {
(id);
}
}
BeanFactory是⼀个很根的接⼝,ApplicationContext和ClassPathXmlApplicationContext都实现了接⼝BeanFactory,所以也可以这么写:ApplicationContext context = new ClassPathXmlApplicationContext("l");
HelloBean helloBean = (Bean("helloBean");
BeanFactory factory= new ClassPathXmlApplicationContext("l");
HelloBean helloBean = (Bean("helloBean");
ClassPathXmlApplicationContext层级关系如下:
2.利⽤FileSystemResource读取
Resource rs = new FileSystemResource("D:/software/tomcat/webapps/springWebDemo/WEB-INF/classes/l");
BeanFactory factory = new XmlBeanFactory(rs);
HelloBean helloBean = (Bean("helloBean");
System.out.HelloWorld());
注意:利⽤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 = (Bean("helloBean");
System.out.HelloWorld());
⼆.利⽤java.util.Properties读取属性⽂件
⽐如,我们构造⼀个ip_config.properties来保存服务器ip地址和端⼝,如:
ip=192.168.0.1
port=8080
我们可以⽤如下程序来获得服务器配置信息:
InputStream inputStream = Class().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 =ServletContext());
wac = ServletContext());
JdbcTemplate jdbcTemplate = (Bean("jdbcTemplate");
其中,jdbcTemplate为spring配置⽂件中的⼀个bean的id值。
这种⽤法⽐较灵活,spring配置⽂件在web中配置启动后,该类会⾃动去对应的bean,⽽不⽤再去指定配置⽂件的具体位置。以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。