一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java中spring讀取配置文件的幾種方法示例

Java中spring讀取配置文件的幾種方法示例

2020-08-11 18:52stack_over_flow Java教程

本篇文章中主要介紹了Java中spring讀取配置文件的幾種方法示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下。

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

一.新建一個Java Bean:

?
1
2
3
4
5
6
7
8
9
10
11
package springdemo;
 
public class HelloBean {
  private String helloWorld;
  public String getHelloWorld() {
    return helloWorld;
  }
  public void setHelloWorld(String helloWorld) {
    this.helloWorld = helloWorld;
  }
}

二.構建一個配置文件bean_config.xml:

?
1
2
3
4
5
6
7
8
9
<?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:

?
1
2
3
4
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,放入容器內。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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,所以也可以這么寫:

?
1
2
3
4
5
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層級關系如下:

Java中spring讀取配置文件的幾種方法示例

2.利用FileSystemResource讀取

?
1
2
3
4
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文件:

?
1
2
helloBean.class=springdemo.HelloBean
helloBean.helloWorld=Hello!HelloWorld!

屬性文件中的"helloBean"名稱即是Bean的別名設定,.class用于指定類來源。

然后利用org.springframework.beans.factory.support.PropertiesBeanDefinitionReader來讀取屬性文件。

?
1
2
3
4
5
6
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

我們可以用如下程序來獲得服務器配置信息:

?
1
2
3
4
5
6
7
8
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來取。

?
1
2
3
4
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,而不用再去指定配置文件的具體位置。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://www.cnblogs.com/jing99/p/6395981.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久人妻熟女中文字幕AV蜜芽 | 亚洲欧美精品久久 | 四虎网站网址 | 亚洲另类中文字幕 | 国模大胆一区二区三区 | 久久久GOGO无码啪啪艺术 | 久久精品国产在热亚洲 | 好骚好紧 | freesex性欧美炮机喷潮 | 国产91在线精品 | 国产日本久久久久久久久婷婷 | 欧美va天堂va视频va在线 | 97国产自拍 | 国产亚洲自愉自愉 | 肥胖女性大bbbbbb视频女厕 | 四虎在线最新地址公告 | 天天摸天天爽视频69视频 | 亚洲高清无在码在线电影 | 久久中文骚妇内射 | 息与子中文字幕bd | 贰佰麻豆剧果冻传媒一二三区 | 亚洲国产精品自在在线观看 | 国产成人亚洲综合91精品555 | 午夜欧美精品久久久久久久 | 色网免费观看 | 国产成人高清亚洲一区91 | 日本高清有码视频 | 国产91精品久久久久久 | 免费视屏 | 精品一二三区久久AAA片 | 91久久偷偷做嫩草影院免费看 | 成人免费毛片一区二区三区 | 丰满大乳欲妇三级k8 | 欧美理论片手机在线观看片免费 | 亚州中文字幕 | 国产原创一区二区 | 福利姬 magnet | yjzz视频| 精品国产综合区久久久久久 | 秒播影视 午夜福利毛片 | 涩涩国产精品福利在线观看 |