zoukankan      html  css  js  c++  java
  • Spring中配置文件中引用外部文件

    srcdaydayconn.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;
    }
    }

    srcdaydayMain.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());
    }
    }

    srcdb.properties

    srceans.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}引用外部的属性

  • 相关阅读:
    Web APP开发技巧总结
    2015年 移动开发都有哪些热点?
    手机APP UI设计尺寸基础知识
    最新Android & iOS设计尺寸规范
    给iOS开发者的Android开发建议
    Android 应用开发推荐书单
    Android 你应该知道的学习资源 进阶之路贵在坚持
    一篇文章让你读懂iOS和Android的历史起源
    Android平台上最好的几款免费的代码编辑器
    Google 发布的15个 Android 性能优化典范
  • 原文地址:https://www.cnblogs.com/Hdaydayup/p/6114415.html
Copyright © 2011-2022 走看看