Master Spring Boot Property Configuration: Custom Props, Random Values, and Multi‑Profile Setup

This guide explains how to define custom Spring Boot properties, load them with @Value, reference other properties, generate random values, override settings via command‑line arguments, and manage multiple environment profiles, complete with code samples and testing tips.

Programmer DD
Programmer DD
Programmer DD
Master Spring Boot Property Configuration: Custom Props, Random Values, and Multi‑Profile Setup

Custom Properties and Loading

Spring Boot lets you define your own configuration keys directly in application.properties. For example:

com.didispace.blog.name=程序猿DD
com.didispace.blog.title=SpringBoot教程

These values can be injected into a component using the @Value("${property.name}") annotation:

@Component
public class BlogProperties {
    @Value("${com.didispace.blog.name}")
    private String name;

    @Value("${com.didispace.blog.title}")
    private String title;
    // getters and setters omitted
}

A simple unit test can verify that the properties are correctly bound:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
public class ApplicationTests {
    @Autowired
    private BlogProperties blogProperties;

    @Test
    public void getHello() throws Exception {
        Assert.assertEquals("程序猿DD", blogProperties.getName());
        Assert.assertEquals("SpringBoot教程", blogProperties.getTitle());
    }
}

Parameter References

Properties can reference each other using the ${…} syntax. For instance, defining a description that reuses name and title:

com.didispace.blog.desc=${com.didispace.blog.name}正在努力写《${com.didispace.blog.title}》

The resolved value becomes 程序猿DD正在努力写《SpringBoot教程》.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

testingConfigurationSpring BootCustom PropertiesProfilesRandom Values
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.