广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Spring注解@Value及属性加载配置文件方式
  • 385
分享到

Spring注解@Value及属性加载配置文件方式

2024-04-02 19:04:59 385人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

spring中使用@Value注解给bean加载属性的配置文件有两种使用方式 第一种:使用@Value("#{configProperties['WEBsit.msgname']}"

spring中使用@Value注解给bean加载属性的配置文件有两种使用方式

第一种:使用@Value("#{configProperties['WEBsit.msgname']}")

spring中配置属性加载文件的配置方式


<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>classpath:/properties/websit.properties</value>
            </list>
        </property>
</bean>

注意

1.这里使用的configProperties必须要和定义的bean名称一致。

2.websit用来指定msgname来源于那个配置文件

3.配置的加载属性bean名称为org.springframework.beans.factory.config.PropertiesFactoryBean

第二种:使用@Value("${websit.msgname}");

使用这种方式,又可以有两种配置方式

方式一


<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
        <property name="properties" ref="configProperties"/>
</bean>
 
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>classpath:/properties/websit.properties</value>
            </list>
        </property>
</bean>

方式二


<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
     <property name="locations">
     <list>
         <value>classpath:properties/websit.properties</value>
     </list>
        </property>
</bean>

当使用@Value注解bean属性时,如果没有在配置文件中配置,这时启动spring就会抛出异常。@Value提供了一种默认值的设置方式,如果在属性文件中没有配置则可以使用默认值。

形式如下


@Value("${avg.age:22}")  
private int userAge; 

如果使用@Value注解后,数据不能正常的被注入则需要在xml的配置文件中加入下列代码


<context:annotation-config/>

SpringBoot使用注解(@value)读取properties(yml)文件中 配置信息

为了简化读取properties文件中的配置值,spring支持@value注解的方式来获取,这种方式大大简化了项目配置,提高业务中的灵活性。

1. 两种使用方法

1)@Value("#{configProperties['key']}")

2)@Value("${key}")

2. 配置文件示例

ftp:
ftplp: 10.2.23.89
ftpPort: 21
ftpUser: uftp
ftpPwd: 12345678
ftpRemotePath: /home

说明:以上是配置文件中的信息,主要是一些账号密码等信息。

3. 读取yml配置文件的工具


package com.dbright.dataprediction.entity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource("classpath:ftpconfig.yml")
@ConfigurationProperties(prefix = "ftp")
public class FtpProperties {

    @Value("${ftplp}")
    public String ftplp;
    @Value("${ftpPort}")
    public String ftpPort;
    @Value("${ftpUser}")
    public String ftpUser;
    @Value("${ftpPwd}")
    public String ftpPwd;
    @Value("${ftpRemotePath}")
    public String ftpRemotePath;

    public String getFtplp() {
        return ftplp;
    }

    public void setFtplp(String ftplp) {
        this.ftplp = ftplp;
    }

    public String getFtpPort() {
        return ftpPort;
    }

    public void setFtpPort(String ftpPort) {
        this.ftpPort = ftpPort;
    }

    public String getFtpUser() {
        return ftpUser;
    }

    public void setFtpUser(String ftpUser) {
        this.ftpUser = ftpUser;
    }

    public String getFtpPwd() {
        return ftpPwd;
    }

    public void setFtpPwd(String ftpPwd) {
        this.ftpPwd = ftpPwd;
    }

    public String getFtpRemotePath() {
        return ftpRemotePath;
    }

    public void setFtpRemotePath(String ftpRemotePath) {
        this.ftpRemotePath = ftpRemotePath;
    }
}

说明:以上是使用@value注解来读取yml配置文件的代码示例

1)@component —— 把普通pojo实例化到spring容器中,相当于配置文件中的`<bean id="" class=""/>`

2) @PropertySource("classpath:ftpconfig.yml") —— 设置yml文件的路径,方便扫描到。一般我们配置文件都是放在resources包下。所以我们只需要 classpath+所需要读取的配置文件名称。

3)@ConfigurationProperties(prefix = "ftp") —— 这个不需要解释太多,配置文件里面内容的前缀,我们读取的是ftp下的信息。

4)@Value("${ftplp}") —— 这是读取我们所需的配置信息,美元符号+{字段名}即可制定

5)下面定义字符串来接收所读取到的配置信息。

6)写set和get方法,方便外部类调用。

4. 演示:效果图如下

可以看到,我们成功取到了我们想要的值。

5. 一开始说的第二种和这个差不多

把{}外的 $ 变成 # 号,然后里面指定配置文件的信息+字段而已。大同小异,我就不贴代码上来了。

今天的内容就到这里啦,以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网!

--结束END--

本文标题: Spring注解@Value及属性加载配置文件方式

本文链接: https://www.lsjlt.com/news/129900.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作