一文详解Spring加载properties文件的方式

目录
  • 一、druid的资源配置管理
  • 二、c3p0资源配置管理
  • 三、加载properties文件
    • 不加载系统属性
    • 加载多个properties文件
    • 加载所有properties文件
    • 加载properties文件标准格式
    • 从类路径或jar包中搜索并加载properties文件

spring第三方资源配置管理

  • DruidDataSource
  • ComboPooledDataSource

一、druid的资源配置管理

导入druid的坐标:

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.16</version>
        </dependency>

App运行输出druid:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.sql.DataSource;

public class App {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        DataSource dataSource = (DataSource) ctx.getBean("dataSource");
        System.out.println(dataSource);

    }
}

applicationContext.xml配置:

配置数据源对象作为spring管理的bean

<!--    管理DruidDataSource对象-->
   <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
           <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
           <property name="url" value="jdbc:mysql://localhost:3306/spring_db"/>
           <property name="username" value="root"/>
           <property name="password" value="root"/>
   </bean>

执行结果:

二、c3p0资源配置管理

maven远程仓库中找:

导入c3p0的坐标:

        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>

c3p0还需要mysql的驱动,导入mysql的坐标:

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

App运行输出与上面的一样。

applicationContext.xml配置:

  <!--c3p0连接池对象-->
       <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
           <property name="driverClass" value="com.mysql.jdbc.Driver"/>
           <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring_db"/>
           <property name="user" value="root"/>
           <property name="password" value="root"/>
           <property name="maxPoolSize" value="1000"/>
       </bean>

也可以配置最大连接对象和其他需要配置数据。

执行结果:

三、加载properties文件

1、开启context命名空间,总共5处标红的地方需要修改为context。

<?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.xsd">

2、使用context命名空间,加载指定properties文件

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

properties配置文件,配置时要加jdbc,不然会和系统环境变量冲突,系统优先级高:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/spring_db
jdbc.username=root
jdbc.password=root

3、使用${ }读取加载的properties文件中的属性值

说明:idea自动识别${ }加载的属性值,需要手工点击才可以查阅原始书写格式

<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>

不加载系统属性

可通过此种方法不加载系统属性,就不会和系统属性冲突:

system-properties-mode属性:是否加载系统属性

<context:property-placeholder location="jdbc.properties" system-properties-mode="NEVER"/>

加载多个properties文件

用逗号分隔可加载多个properties文件:

<context:property-placeholder location="jdbc.properties,jdbc2.properties"/>

加载所有properties文件

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

加载properties文件标准格式

classpath:*.properties:设置加载当前工程类路径中的所有properties文件

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

从类路径或jar包中搜索并加载properties文件

classpath*:*.properties:设置加载当前工程类路径和当前工程所依赖的所有jar包中的所有properties文件

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

以上就是一文详解Spring加载properties文件的方式的详细内容,更多关于Spring加载properties文件的资料请关注我们其它相关文章!

(0)

相关推荐

  • 详解利用Spring加载Properties配置文件

    记得之前写Web项目的时候配置文件的读取都是用Properties这个类完成的,当时为了项目的代码的统一也就没做什么改动.但事后一直在琢磨SpringMVC会不会都配置的注解功能了?经过最近的研究返现SpringMVC确实带有这一项功能,Spring确实很强大. 因为代码很简单,我就贴上我测试的代码,按照步骤做就可以实现了. 新建配置文件jdbc.properties username=root password=root 新建并配置文件spring-properties <?xml versi

  • Spring加载properties文件的方法

    在项目中如果有些参数经常需要修改,或者后期可能需要修改,那我们最好把这些参数放到properties文件中,源代码中读取properties里面的配置,这样后期只需要改动properties文件即可,不需要修改源代码,这样更加方便.在Spring中也可以这么做,而且Spring有两种加载properties文件的方式:基于xml方式和基于注解方式. 下面分别讨论下这两种方式. 1. 通过xml方式加载properties文件         我们以Spring实例化dataSource为例,我们

  • Spring加载配置和读取多个Properties文件的讲解

    一个系统中通常会存在如下一些以Properties形式存在的配置文件 1.数据库配置文件demo-db.properties: database.url=jdbc:mysql://localhost/smaple database.driver=com.mysql.jdbc.Driver database.user=root database.password=123 2.消息服务配置文件demo-mq.properties: #congfig of ActiveMQ mq.java.namin

  • 详解Spring加载Properties配置文件的四种方式

    一.通过 context:property-placeholder 标签实现配置文件加载 1.用法示例: 在spring.xml配置文件中添加标签 复制代码 代码如下: <context:property-placeholder ignore-unresolvable="true" location="classpath:redis-key.properties"/> 2.在 spring.xml 中使用配置文件属性: <!-- 基本属性 url.

  • 详解SpringMVC加载配置Properties文件的几种方式

    最近开发的项目使用了SpringMVC的框架,用下来感觉SpringMVC的代码实现的非常优雅,功能也非常强大, 网上介绍Controller参数绑定.URL映射的文章都很多了,写这篇博客主要总结一下SpringMVC加载配置Properties文件的几种方式 1.通过context:property-placeholde实现配置文件加载 1.1.在spring.xml中加入context相关引用 <?xml version="1.0" encoding="UTF-8&

  • Spring加载properties文件的两种方式实例详解

    在项目中如果有些参数经常需要修改,或者后期可能需要修改,那我们最好把这些参数放到properties文件中,源代码中读取properties里面的配置,这样后期只需要改动properties文件即可,不需要修改源代码,这样更加方便.在Spring中也可以这么做,而且Spring有两种加载properties文件的方式:基于xml方式和基于注解方式.下面分别讨论下这两种方式. 1. 通过xml方式加载properties文件 我们以Spring实例化dataSource为例,我们一般会在beans

  • 一文详解Spring加载properties文件的方式

    目录 一.druid的资源配置管理 二.c3p0资源配置管理 三.加载properties文件 不加载系统属性 加载多个properties文件 加载所有properties文件 加载properties文件标准格式 从类路径或jar包中搜索并加载properties文件 spring第三方资源配置管理 DruidDataSource ComboPooledDataSource 一.druid的资源配置管理 导入druid的坐标: <dependency> <groupId>com

  • Java加载properties文件实现方式详解

    java加载properties文件的方式主要分为两大类:一种是通过import java.util.Properties类中的load(InputStream in)方法加载: 另一种是通过import java.util.ResourceBundle类的getBundle(String baseName)方法加载. 注意:一定要区分路径格式 实现代码如下: package com.util; import java.io.FileInputStream; import java.io.Fil

  • java加载properties文件的六种方法总结

    java加载properties文件的六种方法总结 java加载properties文件的六中基本方式实现 java加载properties文件的方式主要分为两大类:一种是通过import java.util.Properties类中的load(InputStream in)方法加载: 另一种是通过import java.util.ResourceBundle类的getBundle(String baseName)方法加载. 注意:一定要区分路径格式 实现代码如下: package com.ut

  • 一文详解Spring如何控制Bean注入的顺序

    目录 简介 构造方法依赖(推荐) @DependsOn(不推荐) BeanPostProcessor(不推荐) 简介 说明 本文介绍Spring如何控制Bean注入的顺序. 首先需要说明的是:在Bean上加@Order(xxx)是无法控制bean注入的顺序的! 控制bean的加载顺序的方法 1.构造方法依赖 2.@DependsOn 注解 3.BeanPostProcessor 扩展 Bean初始化顺序与类加载顺序基本一致:静态变量/语句块=> 实例变量或初始化语句块=> 构造方法=>

  • 一文详解Spring是怎么读取配置Xml文件的

    目录 Spring读取配置文件Document Element DocumentDefaultsDefinition Spring读取配置文件Document 在XmlBeanDefinitionReader.doLoadBeanDefinitions(InputSource inputSource, Resource resource)方法中将Xml文件转换成Document对象; Document doc = doLoadDocument(inputSource, resource); El

  • 一文详解Spring Security的基本用法

    目录 1.引入依赖 2.用户名和密码在哪里设置 3.UserDetailsService接口详解 3.1JdbcDaoImpl实现类 3.2InMemoryUserDetailsManager实现类 3.3自定义实现类实现UserDetailsService接口 4.如何修改登录页面 Spring Security是一个功能强大且高度可定制的身份验证和访问控制框架, 提供了完善的认证机制和方法级的授权功能.是一款非常优秀的权限管理框架.它的核心是一组过滤器链,不同的功能经由不同的过滤器. 今天通

  • 一文详解Spring的Enablexxx注解使用实例

    目录 引言 @Enable 注解 @Import 注解 为什么要使用 @Import 注解呢 总结 引言 layout: post categories: Java title: 一文带你了解 Spring 的@Enablexxx 注解 tagline: by 子悠 tags: - 子悠 前面的文章给大家介绍 Spring 的重试机制的时候有提到过 Spring 有很多 @Enable 开头的注解,平时在使用的时候也没有注意过为什么会有这些注解,今天就给大家介绍一下. @Enable 注解 首先

随机推荐