Spring讀取bean.xml參數文件

1、下載最新spring包
http://repo.spring.io/release/org/springframework/spring/5.0.6.RELEASE/
解壓後將spring-framework-5.0.6.RELEASE\libs下的jar放到工程lib下,並build path。

2、創建get/set類文件(jdbcMap.java),將所需參數加上get set方法。

3、創建beans.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean id="jdbcMap" class="cn.kq3.main.jdbcMap">
       <property name="jdbc" value="jdbc:sqlserver://10.10.1.15:1433"/>
   </bean>

</beans>

注意bean id對應類名,class對應類的位置

4、創建主程序mainApp,

public class test {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext context = 
                 new ClassPathXmlApplicationContext("beans.xml");
          jdbcMap obj = (jdbcMap) context.getBean("jdbcMap");
          System.out.println(obj.getJdbc());
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章