Backend Development 8 min read

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.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Master Spring Boot Configuration: @ConfigurationProperties, @Value, AOP & Lazy Init

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.

<code>@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties({PackProperties.class, ...})
public class PackConfiguration {
}

@ConfigurationProperties(prefix = "pack")
public class PackProperties {
    private String title;
    private String version;
}
</code>

Alternatively, scan packages directly with @ConfigurationPropertiesScan :

<code>@SpringBootApplication
@ConfigurationPropertiesScan({"com.pack.component", ...})
public class PackApplication {
    // TODO
}
</code>

Beans created from these classes follow the naming pattern &lt;prefix&gt;-&lt;fqn&gt; , where &lt;prefix&gt; is the value set in @ConfigurationProperties and &lt;fqn&gt; is the fully‑qualified class name.

<code>ConfigurableApplicationContext context = ...;
context.getBean("pack-com.pack.properties.PackProperties");
</code>

2. Binding Environment Variables

Configuration properties can be supplied via command‑line arguments, e.g.:

<code>java -jar app-1.0.0.jar --server.port=7000
</code>

To map properties to system environment variables, replace dots with underscores and remove any dashes:

<code># server.port=7000  →  SERVER_PORT=7000
# pack-info.version=1.0.0  →  PACKINFO_VERSION=1.0.0
</code>

Example 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.

<code>@ConfigurationProperties("pack")
@Validated
public class PackProperties {
    @NotNull
    private InetAddress address;
    // getters/setters...
}
</code>

Validation can also be applied to a bean‑producing method:

<code>@Bean
@ConfigurationProperties(prefix = "custom")
@Validated
CustomProperties customProperties() {
    return new CustomProperties();
}
</code>

If validation fails, Spring Boot throws an exception during startup.

4. Using @Value

The @Value annotation binds a property value to a field.

<code>@Value("${pack.title}")
private String title;
</code>

Provide a default value to avoid exceptions when the property is missing:

<code>@Value("${pack.title:'xxxooo'}")
private String title;
</code>

SpEL expressions allow more complex assignments:

<code>@Value("#{systemProperties['user.catalog'] + 'Catalog'}")
String catalog;

@Value("${server.error.path:${error.path:/error}}")
private String errorPath;

@Value("#{userService}")
private UserService userService;
</code>

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:

<code>server:
  port: 0
# or
server:
  port: ${random.int[5000,10000]}
</code>

In test environments, use the RANDOM_PORT web environment:

<code>@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class AppTest {}
</code>

6. AOP Proxy Configuration

Spring Boot defaults to CGLIB proxies. Switch to JDK proxies with:

<code>spring:
  aop:
    proxy-target-class: false
</code>

If 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:

<code>spring:
  main:
    lazy-initialization: true
</code>

Disable lazy initialization for specific beans with @Lazy(false) :

<code>@Bean
@Lazy(false)
public UserService userService() {
    return new UserService();
}
</code>

8. Circular Dependencies

Recent Spring Boot versions disable circular dependencies by default. Example of a circular reference:

<code>@Component
public class A {
    @Resource
    private B b;
}

@Component
public class B {
    @Resource
    private A a;
}
</code>

Enable circular references via configuration:

<code>spring:
  main:
    allow-circular-references: true
</code>
JavaAOPbackend developmentValidationSpring BootConfigurationPropertiesLazy Initialization
Spring Full-Stack Practical Cases
Written by

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.

0 followers
Reader feedback

How this landed with the community

login 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.