欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2017年02月14日 13:59:50   作者:stack_over_flow  
本篇文章中主要介紹了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;
  }
}

二.構(gòu)建一個配置文件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實現(xiàn)了接口ApplicationContext,ApplicationContext實現(xiàn)了BeanFactory。其通過jdom進(jìn)行XML配置文件的讀取,并構(gòu)建實例化Bean,放入容器內(nèi)。

public interface BeanFactory {
  public Object getBean(String id);
}

//實現(xiàn)類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")); //構(gòu)造文檔對象
    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都實現(xiàn)了接口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層級關(guān)系如下:

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配置文件

介紹兩種技術(shù):利用spring讀取properties 文件和利用java.util.Properties讀取:

一.利用spring讀取properties 文件

還利用上面的HelloBean.java文件,構(gòu)造如下bean_config.properties文件:

helloBean.class=springdemo.HelloBean
helloBean.helloWorld=Hello!HelloWorld!

屬性文件中的"helloBean"名稱即是Bean的別名設(shè)定,.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讀取屬性文件

比如,我們構(gòu)造一個ip_config.properties來保存服務(wù)器ip地址和端口,如:

ip=192.168.0.1

port=8080

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

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中配置啟動后,該類會自動去找對應(yīng)的bean,而不用再去指定配置文件的具體位置。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 淺談spring ioc的注入方式及注入不同的數(shù)據(jù)類型

    淺談spring ioc的注入方式及注入不同的數(shù)據(jù)類型

    這篇文章主要介紹了淺談spring ioc的注入方式及注入不同的數(shù)據(jù)類型,具有一定借鑒價值,需要的朋友可以參考下
    2017-12-12
  • 在JPA的@Query注解中使用limit條件(詳解)

    在JPA的@Query注解中使用limit條件(詳解)

    下面小編就為大家?guī)硪黄贘PA的@Query注解中使用limit條件(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • java 使用線程監(jiān)控文件目錄變化的實現(xiàn)方法

    java 使用線程監(jiān)控文件目錄變化的實現(xiàn)方法

    這篇文章主要介紹了java 使用線程監(jiān)控文件目錄變化的實現(xiàn)方法的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-10-10
  • java 獲取用戶的MAC地址多種方法實例詳解

    java 獲取用戶的MAC地址多種方法實例詳解

    這篇文章主要介紹了JAVA實現(xiàn)獲取用戶的MAC地址的多種方法實例,需要的朋友可以參考下
    2017-04-04
  • SpringBoot+devtools實現(xiàn)熱部署的示例代碼

    SpringBoot+devtools實現(xiàn)熱部署的示例代碼

    在軟件項目的開發(fā)過程中,不可避免的會經(jīng)常修改代碼,每次修改代碼,都需要手動停止然后再啟動服務(wù),最后驗證代碼的正確性,今天通過這篇文章,我們一起來學(xué)習(xí)一下如何使用Spring?Boot?+?devtools?輕松搞定熱部署,需要的朋友可以參考下
    2024-08-08
  • springboot整合過濾器實戰(zhàn)步驟

    springboot整合過濾器實戰(zhàn)步驟

    在項目開發(fā)過程中,過濾器或者攔截器幾乎是必用的,他可以很方便的完成類似日志處理、token驗證等一系列操作,區(qū)別于業(yè)務(wù)接口,獨立進(jìn)行處理,感覺就是一種Aop思想。下面模擬請求接口前的token驗證,進(jìn)行過濾器的實戰(zhàn)
    2022-04-04
  • 詳解Mybatis的緩存

    詳解Mybatis的緩存

    這篇文章主要介紹了Mybatis緩存的相關(guān)資料,幫助大家更好的理解和使用Mybatis框架,感興趣的朋友可以了解下
    2021-01-01
  • java中for循環(huán)執(zhí)行的順序圖文詳析

    java中for循環(huán)執(zhí)行的順序圖文詳析

    關(guān)于java的for循環(huán)想必大家非常熟悉,它是java常用的語句之一,這篇文章主要給大家介紹了關(guān)于java中for循環(huán)執(zhí)行順序的相關(guān)資料,需要的朋友可以參考下
    2021-06-06
  • Java實現(xiàn)赫夫曼樹(哈夫曼樹)的創(chuàng)建

    Java實現(xiàn)赫夫曼樹(哈夫曼樹)的創(chuàng)建

    給定N個權(quán)值作為N個葉子結(jié)點,構(gòu)造一棵二叉樹,若該樹的帶權(quán)路徑長度(WPL)達(dá)到最小,稱這樣的二叉樹為最優(yōu)二叉樹,也稱為哈夫曼樹(Huffman Tree)。這篇文章主要就是為大家介紹如何通過Java實現(xiàn)赫夫曼樹,需要的朋友可以參考一下
    2021-12-12
  • 詳解Spring中Bean的作用域與生命周期

    詳解Spring中Bean的作用域與生命周期

    Spring作為當(dāng)前Java最流行、最強大的輕量級框架,受到了程序員的熱烈歡迎。準(zhǔn)確的了解Spring?Bean的作用域與生命周期是非常必要的。這篇文章將問你詳解一下Bean的作用域與生命周期,需要的可以參考一下
    2021-12-12

最新評論