国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

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

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

瀏覽:2日期: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
相關文章:
主站蜘蛛池模板: 都昌县| 澎湖县| 芮城县| 高雄县| 东乡| 济阳县| 沙田区| 宜都市| 会泽县| 昌宁县| 彭山县| 沧州市| 吴堡县| 聂拉木县| 阿合奇县| 马山县| 巴塘县| 长寿区| 来安县| 日喀则市| 莱西市| 舟曲县| 集安市| 治多县| 铅山县| 通化县| 客服| 容城县| 长丰县| 商城县| 施甸县| 石台县| 邳州市| 铁岭县| 乌兰浩特市| 大邑县| 孝义市| 灯塔市| 松滋市| 秭归县| 抚顺市|