Master Spring Boot 3: 110 Real-World Cases and Advanced Configuration Tricks
This article presents a curated collection of 110 Spring Boot 3 practical examples, covering @Profile usage, PropertySource configuration, resource loading, bean validation, and automatic proxy creation, providing developers with hands‑on code snippets and clear explanations to enhance backend development skills.
1. @Profile Annotation
Using @Profile you can register a component only when specific profiles are active.
<code>@Configuration
class Config1 {
@Bean
@Profile("prod")
public ProdComponent prod() {
return new ProdComponent();
}
@Bean
@Profile("dev")
public DevComponent dev() {
return new DevComponent();
}
}
</code>Activate profiles via GenericApplicationContext or JVM arguments.
<code>// method 1
try (GenericApplicationContext context = new GenericApplicationContext()) {
ConfigurableEnvironment env = context.getEnvironment();
env.setActiveProfiles("dev");
}
// method 2
-Dspring.profiles.active="prod"
</code>2. Activating Multiple Profiles
Separate multiple profiles with commas.
<code>-Dspring.profiles.active=prod,pack,...
</code>3. PropertySource
PropertySource allows custom property sources; Spring’s StandardEnvironment includes System properties and environment variables.
<code>public class PackPropertySource extends PropertySource<Map<String, Object>> {
public PackPropertySource(String name, Map<String, Object> source) {
super(name, source);
}
@Override
public Object getProperty(String name) {
return this.source.get(name);
}
}
// add to environment
try (GenericApplicationContext context = new GenericApplicationContext()) {
ConfigurableEnvironment env = context.getEnvironment();
Map<String, Object> source = new HashMap<>();
source.put("pack.version", "1.0.0");
env.getPropertySources().addFirst(new PackPropertySource("pack", source));
}
</code>4. @PropertySource Annotation
Declaratively adds a property file to the Spring environment.
<code>@Configuration
@PropertySource("classpath:com/pack/main/app.properties")
public class AppConfig {
@Resource
private Environment env;
@PostConstruct
public void init() {
System.out.println(env.getProperty("pack.version"));
}
}
</code>Placeholders can be used in the path; missing resources cause an exception.
<code>@Configuration
@PropertySource("classpath:com/pack/${pack.path:}/app.properties")
public class AppConfig { }
</code>5. Resource Loading
ResourceLoader (implemented by ApplicationContext) provides convenient access to classpath, file system, or HTTP resources.
<code>@Component
public class PackComponent {
@Resource
private ApplicationContext ctx;
@PostConstruct
public void init() {
Resource classpathRes = ctx.getResource("com/pack/app.properties");
Resource fileRes = ctx.getResource("file:///opt/app.properties");
Resource httpRes = ctx.getResource("http://www.pack.com/pack/app.properties");
}
}
</code>ResourceLoaderAware can be used to obtain the loader directly.
<code>@Component
public class PackComponent implements ResourceLoaderAware {
private ResourceLoader loader;
@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
this.loader = resourceLoader;
}
}
</code>Resources can also be injected with @Value.
<code>@Component
public class PackComponent {
private final Resource template;
public PackComponent(@Value("${pack.path}") Resource template) {
this.template = template;
}
}
</code>6. Bean Validation
Use JSR‑380 annotations to validate method parameters.
<code>public class User {
@NotNull
@Size(max = 64)
private String name;
@Min(0)
private int age;
}
</code>In a controller, apply @Validated and inspect BindingResult.
<code>@RestController
public class UserController {
public void save(@RequestBody @Validated User user, BindingResult error) {
System.out.println(user);
System.out.println(error.getAllErrors());
}
}
</code>7. Automatic Proxy Creation
BeanNameAutoProxyCreator creates proxies for beans matching name patterns.
<code>@Configuration
public class AppConfig {
@Bean
public MethodInterceptor logInterceptor() {
return invocation -> null;
}
@Bean
public BeanNameAutoProxyCreator beanNameAutoProxyCreator() {
BeanNameAutoProxyCreator creator = new BeanNameAutoProxyCreator();
creator.setBeanNames("*Service", "*Component");
creator.setInterceptorNames("logInterceptor");
return creator;
}
}
</code>DefaultAdvisorAutoProxyCreator applies all matching Advisors automatically.
<code>@Bean
public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
return new DefaultAdvisorAutoProxyCreator();
}
</code>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.