react 前端框架如何驱动企业数字化转型与创新发展
713
2023-03-21
Spring导入properties配置文件代码示例
将外部属性文件的数据配置到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() {
rethttp://urn 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配置文件
xmlns:xsihttp://="http://w3.org/2001/XMLSchemahttp://-instance" xmlns:context="http://springframework.org/schema/context" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd">
xmlns:xsihttp://="http://w3.org/2001/XMLSchemahttp://-instance"
xmlns:context="http://springframework.org/schema/context"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context.xsd">
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);
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~