Spring实战之属性覆盖占位符配置器用法示例

作者:cakincqm 时间:2023-02-02 00:05:30 

本文实例讲述了Spring实战之属性覆盖占位符配置器用法。分享给大家供大家参考,具体如下:

一 配置文件


<?xml version="1.0" encoding="GBK"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://www.springframework.org/schema/beans"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
  <!-- PropertyOverrideConfigurer是一个容器后处理器,它会读取
  属性文件信息,并用这些信息设置覆盖Spring配置文件的数据 -->
  <bean class=
  "org.springframework.beans.factory.config.PropertyOverrideConfigurer">
     <property name="locations">
       <list>
          <value>dbconn.properties</value>
          <!-- 如果有多个属性文件,依次在下面列出来 -->
       </list>
     </property>
  </bean>
  <!-- 定义数据源Bean,使用C3P0数据源实现,
     配置该Bean时没有指定任何信息,但Properties文件里的
     信息将会直接覆盖该Bean的属性值 -->
  <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
     destroy-method="close"/>
</beans>

二 属性文件


dataSource.driverClass=com.mysql.jdbc.Driver
dataSource.jdbcUrl=jdbc:mysql://localhost:3306/spring
dataSource.user=root
dataSource.password=32147

三 测试类


package lee;
import javax.sql.DataSource;
import java.sql.*;
import org.springframework.context.*;
import org.springframework.context.support.*;
public class BeanTest
{
 public static void main(String[] args)throws Exception
 {
   ApplicationContext ctx = new
     ClassPathXmlApplicationContext("beans.xml");
   DataSource ds = (DataSource)ctx.getBean("dataSource");
   Connection conn = ds.getConnection();
   PreparedStatement pstmt = conn.prepareStatement(
     "insert into news_inf value(null , ? , ?)");
   pstmt.setString(1 , "疯狂Java讲义3");
   pstmt.setString(2 , "疯狂iOS讲义3");
   pstmt.executeUpdate();
   pstmt.close();
   conn.close();
 }
}

四 测试结果

Spring实战之属性覆盖占位符配置器用法示例

希望本文所述对大家java程序设计有所帮助。

来源:https://blog.csdn.net/chengqiuming/article/details/101168124

标签:Spring,属性覆盖占位符,配置器
0
投稿

猜你喜欢

  • Java数据结构之链表的增删查改详解

    2023-08-13 07:48:40
  • idea右键没有java class选项问题解决方案

    2023-05-29 23:38:40
  • SpringMVC 参数绑定之视图传参到控制器的实现代码

    2021-12-29 11:54:17
  • Intellij IDEA 2018配置Java运行环境的方法步骤

    2023-06-08 09:56:50
  • 浅谈Java线程并发知识点

    2021-10-20 13:11:13
  • 一文搞懂Java中的注解和反射

    2022-04-04 20:55:31
  • 深入Android SQLite 事务处理详解

    2023-07-17 08:32:17
  • 零基础学习教程之Linux下搭建android开发环境

    2021-12-08 02:13:24
  • idea中MavenWeb项目不能创建Servlet的解决方案

    2022-07-09 19:26:11
  • Java 中的弱引用是什么

    2021-08-21 08:10:46
  • android主线程和子线程之间消息传递详解

    2021-11-06 05:06:23
  • android指定DatePickerDialog样式并不显示年的实现代码

    2022-07-13 05:23:08
  • SpringBoot实现任意位置获取HttpServletRequest对象

    2023-07-07 10:26:40
  • Java中数组转List的三种方法与对比分析

    2023-03-27 08:51:03
  • C语言与C++中关于字符串使用的比较

    2022-01-22 01:30:37
  • mybatis如何使用Criteria的and和or进行联合查询

    2023-02-23 00:44:13
  • Spring createBeanInstance实例化Bean

    2023-06-17 17:26:27
  • C#虚方法的声明和使用实例教程

    2022-09-26 16:07:55
  • SpringCloud feign无法注入接口的问题

    2021-09-04 03:26:29
  • SpringCloud实现Redis在各个微服务的Session共享问题

    2022-07-14 06:33:19
  • asp之家 软件编程 m.aspxhome.com