• spring4笔记----PropertyPlaceholderConfigurer 属性占位符配置器


    driverClassName=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/spring
    username=root
    password=123456
    <!-- 
    --------PropertyOverrideConfigurer ------------------
    dataSource.driverClass=com.mysql.jdbc.Driver
    dataSource.jdbcUrl=jdbc:mysql://localhost:3306/spring
    dataSource.user=root
    dataSource.password=123456
    
    
    
     -->
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
    <!--<bean class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">  -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
    <list>
    <value>dataSource.properties</value>
    </list>
    </property>
    </bean>
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"
     p:driverClass="${driverClassName}" p:jdbcUrl="${url}" p:user="${username}" p:password="${password}"
    />
    </beans>

    package com.ij34.bean;
    import java.sql.*;
    import javax.sql.DataSource;
    
    import org.springframework.context.*;
    import org.springframework.context.support.*;
    
    public class test {
    
    public static void main(String[] args) throws Exception {
       @SuppressWarnings("resource")
    ApplicationContext  ac=new ClassPathXmlApplicationContext("beans.xml");
       DataSource ds=(DataSource) ac.getBean("dataSource");
       Connection conn=ds.getConnection();
       PreparedStatement ps= conn.prepareStatement("insert into news_inf value(null,?,?)");
       ps.setString(1, "加油2017");
       ps.setString(2, "2017,这是挑战与机遇的一年!");
       ps.executeUpdate();
       ps.close();
       conn.close();
    }
    }
  • 相关阅读:
    Leetcode95. Unique Binary Search Trees II不同的二叉搜索树2
    Leetcode134. Gas Station加油站
    Leetcode152. Maximum Product Subarray乘积的最大子序列
    C#扩展方法
    Lua语言入门
    Docker命令
    SpringBoot-配置文件属性注入-3种方式
    从零开始学Linux系统(五)用户管理和权限管理
    MyCat学习笔记
    kafka-zk-安装测试初体验
  • 原文地址:https://www.cnblogs.com/tk55/p/6507454.html
Copyright © 2020-2023  润新知