开发者

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/XML编程客栈Schema-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&UgIncHnjUEgt;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.Configuratwww.devze.comionProperties;
/**
 * 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.aupythontoconfigure.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内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新开发

开发排行榜