Spring:引入properties属性文件

文章目录

  • 一、Druid连接池
    • 1.1 druid.properties文件
    • 1.2 方式一
    • 1.3 方式二
    • 1.4 方式三
  • 二、C3P0连接池
    • 2.1 c3p0.properties文件
    • 2.2 方式一
    • 2.3 方式二
    • 2.4 方式三

一、Druid连接池

1.1 druid.properties文件

druid.driverClassName=com.mysql.cj.jdbc.Driver
druid.url=jdbc:mysql://localhost:3306/mybatis?userUnicode=true&characterEncoding=utf8&useSSL=true&serverTime=GMT%2B8
druid.username=root
druid.password=root

1.2 方式一

引入单个


<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:druid.properties"/>
bean>

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="${druid.driverClassName}"/>
    <property name="url" value="${druid.url}"/>
    <property name="username" value="${druid.username}"/>
    <property name="password" value="${druid.password}"/>
bean>

引入多个


<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	<property name="locations">
		<list>
			<value>classpath:c3p0.propertiesvalue>
			<value>classpath:druid.propertiesvalue>
		list>
	property>
bean>

1.3 方式二


<context:property-placeholder location="classpath:druid.properties"/>

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="${druid.driverClassName}"/>
    <property name="url" value="${druid.url}"/>
    <property name="username" value="${druid.username}"/>
    <property name="password" value="${druid.password}"/>
bean>

1.4 方式三


<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
	<property name="location" value="classpath:druid.properties"/>
bean>

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
	<property name="driverClassName">
        <value>#{configProperties['druid.driverClassName']}value>
    property>
    <property name="url">
        <value>#{configProperties['druid.url']}value>
    property>
    <property name="username">
        <value>#{configProperties['druid.username']}value>
    property>
    <property name="password">
        <value>#{configProperties['druid.password']}value>
    property>
bean>

二、C3P0连接池

2.1 c3p0.properties文件

c3p0.driverClassName=com.mysql.cj.jdbc.Driver
c3p0.url=jdbc:mysql://localhost:3306/mybatis?userUnicode=true&characterEncoding=utf8&useSSL=true&serverTime=GMT%2B8
c3p0.username=root
c3p0.password=root

2.2 方式一

引入单个


<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:c3p0.properties"/>
bean>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${c3p0.driverClassName}">property>
    <property name="jdbcUrl" value="${c3p0.url}">property>
    <property name="user" value="${c3p0.username}">property>
    <property name="password" value="${c3p0.password}">property>
bean>

引入多个


<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	<property name="locations">
		<list>
			<value>classpath:c3p0.propertiesvalue>
			<value>classpath:druid.propertiesvalue>
		list>
	property>
bean>

2.3 方式二


<context:property-placeholder location="classpath:c3p0.properties"/>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${c3p0.driverClassName}">property>
    <property name="jdbcUrl" value="${c3p0.url}">property>
    <property name="user" value="${c3p0.username}">property>
    <property name="password" value="${c3p0.password}">property>
bean>

2.4 方式三


<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="location" value="classpath:c3p0.properties"/>
bean>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass">
        <value>#{configProperties['c3p0.driverClassName']}value>
    property>
    <property name="jdbcUrl">
        <value>#{configProperties['c3p0.url']}value>
    property>
    <property name="user">
        <value>#{configProperties['c3p0.username']}value>
    property>
    <property name="password">
        <value>#{configProperties['c3p0.password']}value>
    property>
bean>

你可能感兴趣的:(Spring,spring)