当前位置: 移动技术网 > IT编程>开发语言>Java > Spring框架中引入外部配置文件的属性值

Spring框架中引入外部配置文件的属性值

2020年08月01日  | 移动技术网IT编程  | 我要评论
Spring框架中引入外部属性值以连接池的配置文件为例1.在Spring的xml配置文件中写入需要管理的类Bean4 用来封装配置信息public class Bean4 { Properties properties; public void setProperties(Properties properties) { this.properties = properties; } @Override public String toStr

Spring框架中引入外部配置文件属性值

以连接池的配置文件为例

1.在Spring的xml配置文件中写入

需要管理的类Bean4 用来封装配置信息

public class Bean4 {
    Properties properties;

    public void setProperties(Properties properties) {
        this.properties = properties;
    }
    @Override
    public String toString() {
        return "Bean4{" +
                "properties=" + properties +
                '}';
    }
} 

xml文件配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean name="bean4" class="com.example.di.Bean4">
        <property name="properties">
            <props>
                <prop key="jdbc.driver">com.mysql.jabc.Driver</prop>
                <prop key="jdbc.url">jdbc:mysql:///jp</prop>
                <prop key="jdbc.username">root</prop>
                <prop key="jdbc.password">root</prop>
            </props>
        </property>
    </bean>
</beans> 

Demo4类进行获取

public class Demo4 {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context=null;
        context = new ClassPathXmlApplicationContext("com/example/demo1.xml");
        Bean4 bean4 = context.getBean("bean4", Bean4.class);
        System.out.println(bean4.toString());
    }
} 

打印结果:
在这里插入图片描述
当然这里全部都是配置在xml中不合适,下面进行外部properties加载

2.注入外部properties配置文件中的内容

2.1 方式一

定义一个Spring提供的特殊的类作为bean,类是 PropertyPlaceholderConfigurer
它的使用方式,是通过设置它的 Location 或 Locations 属性,指定 properties 文件的位置。

db-config.properties文件:

jdbc.driver=com.mysql.jabc.Driver
jdbc.url=jdbc:mysql:///jp
jdbc.username=root
jdbc.password=root 

demo.xml

<?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:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--拿到properties文件源-->
    <bean name="configSource" class="org.springframework.core.io.ClassPathResource" c:path="com/example/db-config.properties"></bean>
    <!--配置Spring自带PropertyPlaceholderConfigurer类-->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" ref="configSource"></property>
    </bean>

    <bean name="bean1" class="com.example.Bean1">
        <property name="properties">
            <props>
                
                <prop key="jdbc.driver">${jdbc.driver}</prop>
                <prop key="jdbc.url">${jdbc.url}</prop>
                <prop key="jdbc.username">${jdbc.username}</prop>
                <prop key="jdbc.password">${jdbc.password}</prop>
            </props>
        </property>
   	 </bean>
    </beans> 

Bean1类代码:

import java.util.Properties;

public class Bean1 {
    Properties properties;


    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    @Override
    public String toString() {
        return "Bean1{" +
                "properties=" + properties +
                '}';
    }
} 

Demo1类

public class Demo1 {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context=null;
        context= new ClassPathXmlApplicationContext("com/example/demo.xml");
        Bean1 bean1 = context.getBean("bean1", Bean1.class);
        System.out.println(bean1.toString());
    }

} 

显示结果:
在这里插入图片描述

2.1.1简化资源获取方式:

对获取properties文件资源的方式Spring也有简化方式
修改位置:
在这里插入图片描述

demo.xml文件修改后:

<?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:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--配置Spring自带PropertyPlaceholderConfigurer类-->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:com/example/db-config.properties"></property>
    </bean>

    <bean name="bean1" class="com.example.Bean1">
        <property name="properties">
            <props>
               
                <prop key="jdbc.driver">${jdbc.driver}</prop>
                <prop key="jdbc.url">${jdbc.url}</prop>
                <prop key="jdbc.username">${jdbc.username}</prop>
                <prop key="jdbc.password">${jdbc.password}</prop>
            </props>
        </property>
    </bean>
</beans> 

显示结果一致:
在这里插入图片描述

2.2 方式二

使用<context:property-placeholder>标签
<context:property-placeholder location="classpath:com/example/db-config.properties"></context:property-placeholder>
简化后的demo.xml文件

<?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:c="http://www.springframework.org/schema/c"
       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 https://www.springframework.org/schema/context/spring-context.xsd">

    <context:property-placeholder location="classpath:com/example/db-config.properties"></context:property-placeholder>

    <bean name="bean1" class="com.example.Bean1">
        <property name="properties">
            <props>
               
                <prop key="jdbc.driver">${jdbc.driver}</prop>
                <prop key="jdbc.url">${jdbc.url}</prop>
                <prop key="jdbc.username">${jdbc.username}</prop>
                <prop key="jdbc.password">${jdbc.password}</prop>
            </props>
        </property>
    </bean>
</beans> 

显示结果一致

本文地址:https://blog.csdn.net/Guesshat/article/details/108244880

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网