久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

Spring導入properties配置文件代碼示例

瀏覽:3日期:2023-08-06 09:21:30

將外部屬性文件的數據配置到bean的配置文件,依賴于context標簽下的property-placeholder標簽

1、準備properties文件

url=jdbc:mysql://localhost:3306/hibernate_db username=root password=1111

2、編寫對應實體類

package com.yl.bean;public class DataSource { private String url; private String username; private String password; public DataSource() { } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return 'DataSource{' +'url=’' + url + ’’’ +', username=’' + username + ’’’ +', password=’' + password + ’’’ +’}’; }}

3、spring配置文件

<?xml version='1.0' encoding='UTF-8'?><beans xmlns='http://www.springframework.org/schema/beans' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:context='http://www.springframework.org/schema/context' xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd'> <!--導入配置文件--> <context:property-placeholder location='classpath:db.properties'></context:property-placeholder> <!--DataSource--> <bean class='com.yl.bean.DataSource'> <property name='password' value='${password}'></property> <property name='username' value='${username}'></property> <property name='url' value='${url}'></property> </bean></beans>

4、測試

package com.yl;import com.yl.bean.DataSource;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainTest { public static void main(String[] args) { ApplicationContext applicationContext=new ClassPathXmlApplicationContext('bean1.xml'); DataSource dataSource= (DataSource) applicationContext.getBean('dataSource'); System.out.println(dataSource); }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 鞍山市| 珠海市| 大足县| 永新县| 德昌县| 临洮县| 尖扎县| 大庆市| 福海县| 泸西县| 佛冈县| 靖宇县| 奇台县| 广昌县| 清徐县| 岗巴县| 高密市| 盐津县| 太和县| 阳春市| 手游| 仙游县| 宾阳县| 托克逊县| 迁安市| 五大连池市| 富顺县| 吴堡县| 中卫市| 共和县| 辰溪县| 西畴县| 枣庄市| 五莲县| 巢湖市| 新和县| 襄樊市| 灯塔市| 石棉县| 常山县| 财经|