当前位置: 代码网 > it编程>编程语言>Java > springboot 自定义属性与加载@value示例详解

springboot 自定义属性与加载@value示例详解

2024年10月06日 Java 我要评论
在使用spring boot的时候,通常需要自定义一些属性,可以按如下方式直接定义。在src/main/resources/application.properties配置文件中加入:server.p

在使用spring boot的时候,通常需要自定义一些属性,可以按如下方式直接定义。在src/main/resources/application.properties配置文件中加入:

server.port=8089
com.av.book.name = my spring boot
com.av.book.author = av

然后通过@value("${属性名}")注解来加载对应的配置属性,具体代码如下:

package com.shrimpking;
import lombok.data;
import org.springframework.beans.factory.annotation.value;
import org.springframework.stereotype.component;
/**
 * created by intellij idea.
 *
 * @author : shrimpking
 * @create 2024/1/13 20:35
 */
@component
@data
public class bookproperties
{
    @value("${com.av.book.name}")
    private string bookname;
    @value("${com.av.book.author}")
    private string author;
}

最后,通过单元测试验证bookproperties属性是否已经根据配置文件加载配置:

package com.shrimpking;
import org.junit.test;
import org.junit.runner.runwith;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.boot.test.context.springboottest;
import org.springframework.test.context.junit4.springrunner;
import javax.annotation.resource;
/**
 * created by intellij idea.
 *
 * @author : shrimpking
 * @create 2024/1/13 20:37
 */
@springboottest
@runwith(springrunner.class)
public class mytest
{
    @resource
    private bookproperties bookproperties;
    @test
    public void test(){
        system.out.println("book name:" + bookproperties.getbookname());
        system.out.println("book author:" + bookproperties.getauthor());
    }
}

不过我们并不推荐使用这种方式,下面给出更优雅的实现方式。首先引入spring boot提供的配置依赖:

        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-configuration-processor</artifactid>
            <optional>true</optional>
        </dependency>
<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
         xsi:schemalocation="http://maven.apache.org/pom/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelversion>4.0.0</modelversion>
    <parent>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-starter-parent</artifactid>
        <version>2.1.6.release</version>
        <relativepath/> <!-- lookup parent from repository -->
    </parent>
    <groupid>com.shrimpking</groupid>
    <artifactid>demo9</artifactid>
    <version>0.0.1-snapshot</version>
    <name>demo9</name>
    <description>demo project for spring boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-web</artifactid>
        </dependency>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-configuration-processor</artifactid>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupid>org.projectlombok</groupid>
            <artifactid>lombok</artifactid>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-test</artifactid>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupid>org.springframework.boot</groupid>
                <artifactid>spring-boot-maven-plugin</artifactid>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupid>org.projectlombok</groupid>
                            <artifactid>lombok</artifactid>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

使用@configurationproperties注解进行编码,修改bookproperties为:

package com.shrimpking;
import lombok.data;
import org.springframework.boot.context.properties.configurationproperties;
/**
 * created by intellij idea.
 *
 * @author : shrimpking
 * @create 2024/1/13 20:39
 */
@data
@configurationproperties(prefix = "com.av.book")
public class otherproperties
{
    private string name;
    private string author;
}

@configurationproperties(prefix="com.av.book"):在application.properties配置的属性前缀。在类中的属性就不用使用@value进行注入了。

package com.shrimpking;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.boot.context.properties.enableconfigurationproperties;
@springbootapplication
@enableconfigurationproperties({otherproperties.class})
public class demo9application
{
    public static void main(string[] args)
    {
        springapplication.run(demo9application.class, args);
    }
}

最后,在启动类中添加@enableconfigurationproperties({otherproperties.class})。

到此这篇关于springboot 自定义属性与加载@value的文章就介绍到这了,更多相关springboot 加载@value内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com