src\dayday\conn.java

package dayday;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; /**
* Created by I am master on 2016/11/29.
*/
public class conn {
private String user;
private String password;
private String driveClass;
private String jdbcUrl;
private Connection connection;
public void setUser(String user){
this.user=user;
}
public void setPassword(String password){
this.password=password;
}
public void setDriveClass(String driveClass){
this.driveClass=driveClass;
}
public void setJdbcUrl(String jdbcUrl){
this.jdbcUrl=jdbcUrl;
}
public Connection getConnection() throws SQLException, ClassNotFoundException {
Class.forName(driveClass);
connection= DriverManager.getConnection(jdbcUrl,user,password);
return connection;
}
}

src\dayday\Main.java

package dayday;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import java.net.SocketTimeoutException;
import java.sql.SQLException; /**
* Created by I am master on 2016/11/28.
*/
public class Main {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
conn con=ctx.getBean("conn",conn.class);
System.out.println(con.getConnection());
}
}

src\db.properties

src\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" xmlns:p="http://www.springframework.org/schema/p"
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">
<!--原来的配置方式-->
<!--<bean id="conn" class="dayday.conn">
<property name="driveClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/bank"></property>
</bean>-->
<!--通过外部配置文件进行配-->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="conn" class="dayday.conn">
<property name="driveClass" value="${driveClass}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
</bean>
</beans> 运行结果:
com.mysql.jdbc.JDBC4Connection@17942a1
通过<context:property-placeholder location="classpath:db.properties"/>加入外部配置文件
通过${var}引用外部的属性

Spring中配置文件中引用外部文件的更多相关文章

  1. Spring 后置处理器 PropertyPlaceholderConfigurer 类(引用外部文件)

    一.PropertyPlaceholderConfigurer类的作用 PropertyPlaceholderConfigurer 是 BeanFactory 后置处理器的实现,也是 BeanFact ...

  2. spring boot中配置文件中变量的引用

    配置文件中 变量的自身引用 ${名称} java文件中引用:非静态变量 之间在变量上面注释@Value("${名称}")  静态变量 在set方法上注释@Value("$ ...

  3. js/css在html文档中的引用外部文件方式对比

    包含css样式表和js脚本的最好方式是使用外部文件,因为css/js和html标记文档可以清晰地分离. css的外部引用写在<head></head>中: <head&g ...

  4. Spring(二)--FactoryBean、bean的后置处理器、数据库连接池、引用外部文件、使用注解配置bean等

    实验1:配置通过静态工厂方法创建的bean  [通过静态方法提供实例对象,工厂类本身不需要实例化!] 1.创建静态工厂类 public class StaticFactory { private st ...

  5. Spring Boot 配置文件中的花样,看这一篇足矣!

    在快速入门一节中,我们轻松的实现了一个简单的RESTful API应用,体验了一下Spring Boot给我们带来的诸多优点,我们用非常少的代码量就成功的实现了一个Web应用,这是传统的Spring应 ...

  6. Spring Boot 配置文件中的花样

    原文:https://www.cnblogs.com/didispace/p/11002732.html 在快速入门一节中,我们轻松的实现了一个简单的RESTful API应用,体验了一下Spring ...

  7. 日志配置文件读取spring boot配置文件中的属性

    如果是读取 application.properties 这种spring boot的默认配置文件时 其中 scope固定为context  指明从上下文中获取, name 根据自己的意思给, sou ...

  8. spring boot配置文件中 spring.mvc.static-path-pattern 配置项

    spring boot项目中的静态资源文件存放在static文件下面,当通过浏览器访问这些静态文件时,发现必须要添加static作为前缀才能访问,折腾了一番后发现,这个前缀跟 spring.mvc.s ...

  9. Spring Boot 配置文件中使用变量、使用随机数

    参数引用 在application.properties中的各个参数之间可以直接通过是使用placeHolder的方式进行引用,如: book.author=Clark book.name=C++ b ...

随机推荐

  1. 【转】 memcmp源码实现

    原型: int memcmp(void *buf1, void *buf2, unsigned int count); 用法:#include <string.h> 功能:比较内存区域bu ...

  2. Linux乱码问题解决方案

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...

  3. hibernate注解随笔—10月8日

    hibernate注解(herbinate4 jar包注解可用,使用hibernate3.3注解失败) 如果javabean与数据库中表名一致(不区分大小写),则注解不用写@Table(name=&q ...

  4. 【转】Python 日期和时间

    本文转自:http://www.runoob.com/python/python-date-time.html Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能. Pytho ...

  5. jeesite部署到Tomcat后,无法访问,cannot be resolved in either web.xml or the jar files deployed with this application

    HTTP Status 500 - /WEB-INF/views/modules/sys/sysLogin.jsp (line: 3, column: 0) The absolute uri: htt ...

  6. Java内存模型

    1. 概述 多任务和高并发是衡量一台计算机处理器的能力重要指标之一.一般衡量一个服务器性能的高低好坏,使用每秒事务处理数(Transactions Per Second,TPS)这个指标比较能说明问题 ...

  7. 关于classList的API

    <body class="a b c"> ...... </body> document.body.classList可以取得body的classList属 ...

  8. HEAD FIRST HTML & CSS学习笔记

    CSS部分 1. border-bottom属性控制元素下边框的外观.   eg: border-bottom:1px solid maroon;  P265 下划线 text-decoration: ...

  9. dom事件与event对象总结

    1 事件:就是文档或浏览器窗口中发生的一些特定的交互瞬间.    tips:js和xhtml的交互是通过当用户或浏览器操作网页时发生的事件来处理的. 1.1 事件流:即事件的顺序.        事件 ...

  10. [高斯消元] POJ 2345 Central heating

    Central heating Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 614   Accepted: 286 Des ...