Spring加载Properties配置文件

本文介绍了Spring加载Properties配置文件的多种方式,包括@PropertySource注解、context:property-placeholder标签、util:properties标签、PropertyPlaceholderConfigurer类以及PropertiesFactoryBean类。每个方法都提供了详细的配置示例和在XML及Java中取值的实例,并通过TestController测试类和postman进行验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

相关文章链接:

IDEA使用Maven搭建SSM框架Web项目

观前提示:

IDEA版本为ultimate 2019.1,JDK版本为1.8.0_141,Tomcat版本为9.0.12,postman版本为v7.27.1。

标题2.3.4.5测试用配置文件 jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useSSL=false
jdbc.username=root
jdbc.password=root

jdbc.pool.initialPoolSize=10
jdbc.pool.minPoolSize=5
jdbc.pool.maxPoolSize=40
jdbc.pool.maxIdleTime=20
jdbc.pool.checkoutTimeout=10000

1.@PropertySource注解

配置文件 test.properties

id=01
name=zhangsan
age=18

配置文件加载类 TestProperties .java

package com.example.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@PropertySource(value = {
   "classpath:config/test.properties"})
public class TestProperties {
   
    @Value("id")
    private String id;

    @Value("name")
    private String name;

    @Value("age")
    private String age;

    public String getId() {
   
        return id;
    }

    public void setId(String id) {
   
        this.id = id;
    }

    public String getName() {
   
        return name;
    }

    public void setName(String name) {
   
        this.name = name;
    }

    public String getAge() {
   
        return age;
    }

    public void setAge(String age) {
   
        this.age = age;
    }

    @Override
    public String toString() {
   
        return "TestProperties{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age='" + age + '\'' +
                '}';
    }
}

测试文件 TestController .java

package com.example.controller;

import com.example.config.TestProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/test")
public class TestController {
   

    @Autowired
    private TestProperties testProperties;

    @RequestMapping(value = "/testProperties", method = RequestMethod.GET)
    @ResponseBody
    public String testProperties(){
   
        return testProperties.toString();
    }
}

使用postman发送请求,测试结果为
在这里插入图片描述

2.context:property-placeholder标签

2.1 参考配置

xml文件中配置参考如下

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

注意:取值时使用 ${}

2.2 例子

2.2.1 在xml中取值

<property name="driverClassNa
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值