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

本文实例讲述了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();
  }
}

四 测试结果

更多关于java相关内容感兴趣的读者可查看本站专题:《Spring框架入门与进阶教程》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》

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

(0)

相关推荐

  • Spring Boot环境属性占位符解析及类型转换详解

    前提 前面写过一篇关于Environment属性加载的源码分析和扩展,里面提到属性的占位符解析和类型转换是相对复杂的,这篇文章就是要分析和解读这两个复杂的问题.关于这两个问题,选用一个比较复杂的参数处理方法PropertySourcesPropertyResolver#getProperty,解析占位符的时候依赖到 PropertySourcesPropertyResolver#getPropertyAsRawString: protected String getPropertyAsRawSt

  • 详解SpringBoot配置连接池

    内置的连接池 目前spring Boot中默认支持的连接池有dbcp,dbcp2, tomcat, hikari三种连接池. 数据库连接可以使用DataSource池进行自动配置. 由于Tomcat数据源连接池的性能和并发,在tomcat可用时,我们总是优先使用它. 如果HikariCP可用,我们将使用它. 如果Commons DBCP可用,我们将使用它,但在生产环境不推荐使用它. 最后,如果Commons DBCP2可用,我们将使用它. 以上的几种连接池,可以通过在配置application文

  • 解决Spring国际化文案占位符失效问题的方法

    写在前面:接下来很长一段时间的文章主要会记录一些项目中实际遇到的问题及对应的解决方案,在相应代码分析时会直指问题所在,不会将无关的流程代码贴出,感兴趣的读者可以自行跟踪.同时希望大家能够将心得体会在评论区分享出来,让大家共同进步! 环境或版本:Spring 3.2.3 现象:利用Spring自带的MessageSource来处理国际化文案,us状态下的文案有部分占位符未被替换,cn状态下的正常.文案如下: tms.pallet.order.box.qty=The total palletized

  • Spring实战之获取其他Bean的属性值操作示例

    本文实例讲述了Spring实战之获取其他Bean的属性值操作.分享给大家供大家参考,具体如下: 一 配置 <?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xml

  • Spring实战之设置普通属性值的方法示例

    本文实例讲述了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:sch

  • SpringBoot没有主清单属性的解决方法

    解决 SpringBoot 没有主清单属性 问题:SpringBoot打包成jar后运行提示没有主清单属性 解决:补全maven中的bulid信息 <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${spring.version}</version>

  • Spring MVC 框架搭建配置方法及详解

    现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过要想灵活运用Spring MVC来应对大多数的Web开发,就必须要掌握它的配置及原理. 一.Spring MVC环境搭建:(Spring 2.5.6 + Hibernate 3.2.0) 1. jar包引入 Spring 2.5.6:spring.jar.spring-webmvc.jar.comm

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

    本文实例讲述了Spring实战之属性占位符配置器用法.分享给大家供大家参考,具体如下: 一 配置文件 <?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns

  • spring为类的静态属性实现注入实例方法

    我们知道,正常情况下,spring的一个bean要依赖其他资源,如properties或其他bean,直接利用@Value或@Autowired就可以了.这两个注解就相当于spring application context xml文件定义bean时的property节点.相当于调用了每个属性的set方法. <bean id="person" class="com.myapp.core.spel.xml.Person"> <property nam

  • spring boot Logging的配置以及使用详解

    前言:该篇文章基本上是翻译的官方文档! spring boot使用Commons Logging作为内部的日志系统,并且给Java Util Logging,Log4J2以及Logback都提供了默认的配置.如果使用了spring boot的Starters,那么默认会使用Logback用于记录日志. 一.Log format spring boot中默认的日志输出格式如下: 2014-03-05 10:57:51.112 INFO 45469 --- [ main] org.apache.ca

随机推荐