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. IntelliJ IDEA中Maven项目的默认JDK版本

    在IntelliJ IDEA 15中使用Maven时,IDEA将默认的编译版本.源码版本设置为jdk5.编译项目的时候出现警告:"Warning:Java: 源值1.5已过时, 将在未来所有 ...

  2. Excel数据批量导入到数据库2

    1.导包(共3个) 2.jsp <s:form action="ReadExcel.action" method="post" enctype=" ...

  3. Windows 2008 R2+iis7.5环境下Discuz!X3论坛伪静态设置方法

    2008R2不是那么的普及,加上X3版新出不久,所以伪静态的设置教程比较少,今天搞出来了,其实很简单,那么下面给大家简要说明一下.因为iis7.5集成了url重写,那就就方便多了,首先安装好你的dz论 ...

  4. 前端常用的几个js判断(二)

    10.鼠标悬停(hover)切换 class 属性假如当用户鼠标悬停在一个可点击的元素上时,你希望改变其效果,下面这段代码可以在其悬停在元素上时添加 class 属性,当用户鼠标离开时,则自动取消该 ...

  5. guava学习--Function、Predicate

    Function用于同步转换. Predicate用于过滤. import java.util.Collection; import java.util.Iterator; import java.u ...

  6. GOLDENGATE 配置文档,各类参数--转发

    1       GoldenGate简要说明 GoldenGate现在是业内成熟的数据容灾与复制产品,经过多年的发展与完善,现在已经成为业内事实上的标准之一. GoldenGate软件是一种基于日志的 ...

  7. 常用的web功能测试方法

    功能测试就是对产品各功能进行验证,根据功能测试用例,逐项测试,检查产品是否达到用户要求功能,即是否满足需求.常用的测试方法如下: 1.页面连接检查:每一个连接是否都有对应的页面,并且页面之间切换正确. ...

  8. SVN使用教程

    SVN简介: 为什么要使用SVN? 程序员在编写程序的过程中,每个程序员都会生成很多不同的版本,这就需要程序员有效的管理代码,在需要的时候可以迅速,准确取出相应的版本. Subversion是什么? ...

  9. winform flash

    1.开发工具:VS2013 2.开发环境:win 10 64位 3.添加控件: 打开VS,点击工具箱--常规--右键--“选择项”---“Com组件”--MacroMedia Flash Factor ...

  10. Install Sublime Text 3

    $ sudo add-apt-repository ppa:webupd8team/sublime-text-3 $ sudo apt-get update $ sudo apt-get instal ...