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.
@Configuration
class Config1 {
@Bean
@Profile("prod")
public ProdComponent prod() {
return new ProdComponent();
}
@Bean
@Profile("dev")
public DevComponent dev() {
return new DevComponent();
}
}Activate profiles via GenericApplicationContext or JVM arguments.
// method 1
try (GenericApplicationContext context = new GenericApplicationContext()) {
ConfigurableEnvironment env = context.getEnvironment();
env.setActiveProfiles("dev");
}
// method 2
-Dspring.profiles.active="prod"2. Activating Multiple Profiles
Separate multiple profiles with commas.
-Dspring.profiles.active=prod,pack,...3. PropertySource
PropertySource allows custom property sources; Spring’s StandardEnvironment includes System properties and environment variables.
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));
}4. @PropertySource Annotation
Declaratively adds a property file to the Spring environment.
@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"));
}
}Placeholders can be used in the path; missing resources cause an exception.
@Configuration
@PropertySource("classpath:com/pack/${pack.path:}/app.properties")
public class AppConfig { }5. Resource Loading
ResourceLoader (implemented by ApplicationContext) provides convenient access to classpath, file system, or HTTP resources.
@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");
}
}ResourceLoaderAware can be used to obtain the loader directly.
@Component
public class PackComponent implements ResourceLoaderAware {
private ResourceLoader loader;
@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
this.loader = resourceLoader;
}
}Resources can also be injected with @Value.
@Component
public class PackComponent {
private final Resource template;
public PackComponent(@Value("${pack.path}") Resource template) {
this.template = template;
}
}6. Bean Validation
Use JSR‑380 annotations to validate method parameters.
public class User {
@NotNull
@Size(max = 64)
private String name;
@Min(0)
private int age;
}In a controller, apply @Validated and inspect BindingResult.
@RestController
public class UserController {
public void save(@RequestBody @Validated User user, BindingResult error) {
System.out.println(user);
System.out.println(error.getAllErrors());
}
}7. Automatic Proxy Creation
BeanNameAutoProxyCreator creates proxies for beans matching name patterns.
@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;
}
}DefaultAdvisorAutoProxyCreator applies all matching Advisors automatically.
@Bean
public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
return new DefaultAdvisorAutoProxyCreator();
}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.
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.
