springboot 自定义属性与加载@value

在使用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})。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虾米大王

有你的支持,我会更有动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值