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.
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教程》.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
