开发者社区 问答 正文

spring可以用什么方式来注入properties文件呢?

spring可以用什么方式来注入properties文件呢?

展开
收起
游客4qo4tkog2emrs 2022-03-31 23:16:10 583 分享 版权
1 条回答
写回答
取消 提交回答
  • 通过<context:property-placeholder />标签
    <?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">
    
        <context:property-placeholder location="classpath:mysql.properties" ignore-unresolvable="true"/>
    
        <!-- 配置数据源 -->
        <bean abstract="true" name="parentDatasource" class="com.alibaba.druid.pool.DruidDataSource">
            <property name="driverClassName" value="${ds1.jdbc.driverClassName}" />
            <!-- 初始化连接大小 -->
            <property name="initialSize" value="1" />
            <!-- 连接池最大使用连接数量 -->
            <property name="maxActive" value="100" />
            <!-- 连接池最小空闲 -->
            <property name="minIdle" value="20" />
            <!-- 获取连接最大等待时间 -->
            <property name="maxWait" value="30000" />
            <!-- <property name="poolPreparedStatements" value="true" /> -->
            <!-- <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> -->
            <property name="validationQuery" value="SELECT 1" />
            <property name="testOnBorrow" value="true" />
            <property name="testOnReturn" value="true" />
            <property name="testWhileIdle" value="true" />
            <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
            <property name="timeBetweenEvictionRunsMillis" value="60000" />
            <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
            <property name="minEvictableIdleTimeMillis" value="25200000" />
            <!-- 打开removeAbandoned功能 -->
            <property name="removeAbandoned" value="true" />
            <!-- 1800秒,也就是30分钟 -->
            <property name="removeAbandonedTimeout" value="1800" />
            <!-- 关闭abanded连接时输出错误日志 -->
            <property name="logAbandoned" value="true" />
            <!-- 监控数据库 -->
            <!-- <property name="filters" value="stat" /> -->
            <property name="filters" value="mergeStat" />
        </bean>
    
        <!-- 配置数据源 -->
        <bean name="dataSource1" init-method="init" destroy-method="close" parent="parentDatasource">
            <property name="url" value="${ds1.jdbc.url}" />
            <property name="username" value="${ds1.jdbc.username}" />
            <property name="password" value="${ds1.jdbc.password}" />
        </bean>
    
    
        <!-- 配置数据源 -->
        <bean name="dataSource2" init-method="init" destroy-method="close" parent="parentDatasource">
            <property name="url" value="${ds2.jdbc.url}" />
            <property name="username" value="${ds2.jdbc.username}" />
            <property name="password" value="${ds2.jdbc.password}" />
        </bean>
    
        <!-- 配置事务管理器 -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource1" />
        </bean>
    
        <!-- 注解方式配置事物 -->
        <tx:annotation-driven transaction-manager="transactionManager" />
    
    </beans>
    
    2022-03-31 23:17:24
    赞同 展开评论