• (II)第十节:Spring引用外部属性文件


    一、引用外部属性文件

             当bean的配置信息逐渐增多时,查找和修改一些bean的配置信息就变得愈加困难。这时可以将一部分信息提取到bean配置文件的外部,以properties格式的属性文件保存起来,同时在bean的配置文件中引用properties属性文件中的内容,从而实现一部分属性值在发生变化时仅修改properties属性文件即可。这种技术多用于连接数据库的基本信息的配置。
     

    二、直接配置

      1、添加依赖

            <dependency>
                <groupId>com.mchange</groupId>
                <artifactId>c3p0</artifactId>
                <version>0.9.5.2</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.47</version>
            </dependency>

      2、配置数据库信息

        <!--
           数据库连接池作为单例是最好的;一个项目就一个连接池,连接池里面管理很多连接。连接是直接从连接池里拿
           可以让Spring帮我们创建连接池对象,(管理连接池)
        -->
    
        <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>

      3、测试

        @Test
        public void test1() throws SQLException {
            ApplicationContext ioc = new ClassPathXmlApplicationContext("ioc4.xml");
    
            //按照类型获取组件,可以获取到这个类型下的所有实现类
            DataSource dataSource = ioc.getBean(DataSource.class);
            System.out.println("dataSource = " + dataSource);
            System.out.println(dataSource.getConnection());
        }

    三、创建 properties 属性文件(以数据库为例)

    db.properties 配置文件

    # key = value
    jdbc.driver = com.mysql.jdbc.Driver
    jdbc.url = jdbc:mysql://localhost:3306/test
    jdbc.username = root
    jdbc.password = root

    四、引用外部配置文件-方式一

      在配置文件中再配置一个 <bean>,然后用 ${} 方式获取 配置文件中的键值对信息

      代码示例:

        <!-- 加载配置文件 -->
        <bean  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location"  value="db.properties"></property>
        </bean>
        <!-- 获取配置文件信息-->
        <bean id="dataSource01" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="user" value="${jdbc.username}"></property>
            <property name="password" value="${jdbc.password}"></property>
            <property name="jdbcUrl" value="${jdbc.url}"></property>
            <property name="driverClass" value="${jdbc.driver}"></property>
        </bean>

      测试代码:

        @Test
        public void test2() throws SQLException {
            ApplicationContext ioc = new ClassPathXmlApplicationContext("ioc4.xml");
    
            DataSource dataSource = ioc.getBean("dataSource01", DataSource.class);
            System.out.println("dataSource = " + dataSource);
            System.out.println(dataSource.getConnection());
        }

    五、引用外部配置文件-方式二

      (1)引入 context 名称空间

        

      (2)使用 <context> 引入配置文件

        <!-- 使用context加载配置文件 -->
        <!-- 指定properties属性文件的位置 -->
        <!-- classpath:xxx 表示属性文件位于类路径下 -->
        <context:property-placeholder location="db.properties"/>

      (3)获取配置文件的信息

        <!-- 从properties属性文件中引入属性值 -->
        <!-- ${key} 胴体取出配置中某个key对应的值-->
        <bean id="dataSource02" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="user" value="${jdbc.username}"></property>
            <property name="password" value="${jdbc.password}"></property>
            <property name="jdbcUrl" value="${jdbc.url}"></property>
            <property name="driverClass" value="${jdbc.driver}"></property>
        </bean>


    注意:username 是Spring的 key 中的一个关键字;为了防止配置文件中与 spring关键字冲突。
     
  • 相关阅读:
    安全编码1
    VPP tips
    VPP概述汇总
    C语言安全编码摘录
    TCP-proxy
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.4. Matplotlib: plotting
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.3. NumPy: creating and manipulating numerical data
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.2. The Python language
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.1. Python scientific computing ecosystem
    25马5跑道,求最快的五匹马的需要比赛的次数
  • 原文地址:https://www.cnblogs.com/niujifei/p/15417131.html
Copyright © 2020-2023  润新知