ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

spring中bean配置文件引入外部文件

2021-05-04 18:00:38  阅读:11  来源: 互联网

标签:jdbc 配置文件 spring c3p0 bean context import com 连接池


spring中bean配置文件引入外部文件

数据库连接池为单实例是,一个项目就一个连接池,连接池管理很多连接,连接直接从连接池中拿 ,可以让spring帮我们管理连接池(创建连接池对象),以c3p0为例:

准备:需要导入相关jar包

  • ​ c3p0-0.9.2.1.jar
  • mchange-commons-java-0.2.3.4.jar
  • mysql-connector-java-8.0.23.jar
  1. 资源文件

    jdbc.jdbcUrl=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8
    jdbc.driverClass=com.mysql.jdbc.Driver
    jdbc.user=root
    jdbc.password=root
    
  2. 配置文件编写

    <?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: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-4.0.xsd">
    
    
    	<!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 
    		<property name="user" value="root"></property> <property name="password" 
    		value="root"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property> 
    		<property name="driverClass" value="com.mysql.jdbc.Driver"></property> </bean> -->
    
    	<!-- 导入context名称空间既可以使用外部资源文件
    		classpath:类路径下,固定写法:classpath:dbconfig.properties(资源名)
    		${}:可以从配置文件中动态取出相应配置
    		注意:username是spring中的关键字,不能被使用
    	 -->
    	 <!-- 引用外部属性文件 -->
    	<context:property-placeholder location="classpath:dbconfig.properties" />
    	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
    		<property name="driverClass" value="${jdbc.driverClass}"></property>
    		<property name="user" value="${jdbc.user}"></property>
    		<property name="password" value="${jdbc.password}"></property>
    	</bean>
    </beans>
    
    
  3. 测试

    package com.atguigu.test;
    
    import java.sql.SQLException;
    
    import javax.sql.DataSource;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.mchange.v2.c3p0.ComboPooledDataSource;
    
    public class PoolTest {
    
    	@Test
    	public void test() throws SQLException {
    		ApplicationContext context = new ClassPathXmlApplicationContext("bean_ref.xml");
    		// 从容器中拿到连接池对象
    		DataSource dataSource = context.getBean("dataSource", DataSource.class);
    		System.out.println(dataSource.getConnection());
            //com.mchange.v2.c3p0.impl.NewProxyConnection@8e0379d
    	}
    
    }
    
    

标签:jdbc,配置文件,spring,c3p0,bean,context,import,com,连接池
来源: https://blog.csdn.net/weixin_46245201/article/details/116402884

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有