配置bean之你使用外部属性
2016-09-26 08:25
288 查看
beans-properties.xml:
在上面代码中,属性的赋值用到了.properties文件中的内容。
db.properties:
.properties文件都是以键值对的形式存在。
main.java:
<?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-4.0.xsd"> <!-- 导入属性文件 --> <context:property-placeholder location="classpath:db.properties"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 使用外部化文件的属性 有点像el表达式--> <property name="user" value="${user}"></property> <property name="password" value="${password}"></property> <property name="driverClass" value="${driverclass}"></property> <property name="jdbcUrl" value="${jdbcurl}"></property> </bean> </beans>
在上面代码中,属性的赋值用到了.properties文件中的内容。
db.properties:
user=root password=1230 driverclass=com.mysql.jdbc.Driver jdbcurl=jdbc:mysql///test
.properties文件都是以键值对的形式存在。
main.java:
package com.atguigu.spring.beans.properties;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) throws SQLException {
ApplicationContext c=new ClassPathXmlApplicationContext("beans-properties.xml");
DataSource dataSource=(DataSource)c.getBean("dataSource");
System.out.println(dataSource.getConnection());
}
}
相关文章推荐
- MapReduce实例之PageRank
- 【OpenCV入门指南】第四篇 图像的二值化
- 指定ubuntu下的Python的运行版本
- 你不知道的Kettle学习笔记5--日志
- Win10主题打不开,自动弹出桌面图标设置
- MySQL安装问题小结
- excell 画图
- Android 源码系列之<九>从源码的角度深入理解Activity的launchModel特性
- 【OpenCV入门指南】第三篇Canny边缘检测
- 浅谈Android的消息机制
- java基础接口练习
- Java和C#下的参数验证方法
- 类似气泡的样式demo
- iOS 9 地图定位崩溃
- Android Studio下对资源进行分包
- 有关移动端的开发
- 如何设置伪静态
- @media
- JavaWeb配置详解(结合框架SpringMVC)
- RadioGroup+ViewPager组合切换监听