Master Spring Boot Configuration: @ConfigurationProperties, @Value, AOP & Lazy Init
This tutorial walks through Spring Boot configuration techniques—including property classes, environment variable binding, validation, @Value usage, random ports, AOP proxy settings, lazy initialization, and handling circular dependencies—providing practical code examples for each feature.
1. Property Configuration Classes
When a class annotated with @ConfigurationProperties is not suitable for component scanning, use @EnableConfigurationProperties to specify the types to process. This can be done in any @Configuration class.
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties({PackProperties.class, ...})
public class PackConfiguration {
}
@ConfigurationProperties(prefix = "pack")
public class PackProperties {
private String title;
private String version;
}Alternatively, scan packages directly with @ConfigurationPropertiesScan:
@SpringBootApplication
@ConfigurationPropertiesScan({"com.pack.component", ...})
public class PackApplication {
// TODO
}Beans created from these classes follow the naming pattern <prefix>-<fqn>, where <prefix> is the value set in @ConfigurationProperties and <fqn> is the fully‑qualified class name.
ConfigurableApplicationContext context = ...;
context.getBean("pack-com.pack.properties.PackProperties");2. Binding Environment Variables
Configuration properties can be supplied via command‑line arguments, e.g.:
java -jar app-1.0.0.jar --server.port=7000To map properties to system environment variables, replace dots with underscores and remove any dashes:
# server.port=7000 → SERVER_PORT=7000
# pack-info.version=1.0.0 → PACKINFO_VERSION=1.0.0Example of environment‑variable configuration:
3. Validating Configuration Properties
Annotate a @ConfigurationProperties class with @Validated to trigger JSR‑303 validation. Add constraint annotations to fields as needed.
@ConfigurationProperties("pack")
@Validated
public class PackProperties {
@NotNull
private InetAddress address;
// getters/setters...
}Validation can also be applied to a bean‑producing method:
@Bean
@ConfigurationProperties(prefix = "custom")
@Validated
CustomProperties customProperties() {
return new CustomProperties();
}If validation fails, Spring Boot throws an exception during startup.
4. Using @Value
The @Value annotation binds a property value to a field.
@Value("${pack.title}")
private String title;Provide a default value to avoid exceptions when the property is missing:
@Value("${pack.title:'xxxooo'}")
private String title;SpEL expressions allow more complex assignments:
@Value("#{systemProperties['user.catalog'] + 'Catalog'}")
String catalog;
@Value("${server.error.path:${error.path:/error}}")
private String errorPath;
@Value("#{userService}")
private UserService userService;Note: In Spring Boot, missing properties cause an exception; plain Spring does not.
5. Random Web Server Port
Configure Spring Boot to use a random port or a specific range:
server:
port: 0
# or
server:
port: ${random.int[5000,10000]}In test environments, use the RANDOM_PORT web environment:
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class AppTest {}6. AOP Proxy Configuration
Spring Boot defaults to CGLIB proxies. Switch to JDK proxies with:
spring:
aop:
proxy-target-class: falseIf AspectJ is on the classpath, Spring Boot automatically enables AspectJ auto‑proxying, so @EnableAspectJAutoProxy is not required.
7. Lazy Initialization
Enable lazy initialization globally to create beans only when needed, reducing startup time:
spring:
main:
lazy-initialization: trueDisable lazy initialization for specific beans with @Lazy(false):
@Bean
@Lazy(false)
public UserService userService() {
return new UserService();
}8. Circular Dependencies
Recent Spring Boot versions disable circular dependencies by default. Example of a circular reference:
@Component
public class A {
@Resource
private B b;
}
@Component
public class B {
@Resource
private A a;
}Enable circular references via configuration:
spring:
main:
allow-circular-references: trueSigned-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.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
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.
