Spring Annotations Java Configuration Example
The Spring Framework implements configuration through annotations, simplifying component scanning, Bean definition, and configuration management. Common annotations include:
- @Configuration: Marks a class as a configuration source; Spring scans it to register Beans within the class.
- @ComponentScan: Specifies package paths for scanning, automatically registering components found in those packages.
- @Bean: Defines factory methods for creating Beans dynamically.
- @Value: Injects property values into fields or methods.
- @Autowired: Automatically injects dependencies, supporting injection by type (
byType) or by name (byName).
Spring Annotation Configuration Examples
1. Configuration Class (@Configuration)
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyService();
}
}
Spring automatically scans the AppConfig class and registers the MyService Bean.
2. Component Scanning (@ComponentScan)
@ComponentScan(basePackages = "com.example.demo")
public class DemoApp {
// No additional configuration required; Spring will auto-scan components
}
By specifying a scan path with @ComponentScan, Beans do not need to be registered manually.
3. Custom Annotation (@CustomAnnotation)
@CustomAnnotation(value = "Demo")
public class DemoClass {
// The annotation field can be used directly here
}
Custom annotations can mark classes or methods; Spring processes them as configuration information.
4. Property Injection (@Value)
@Component
public class MyComponent {
@Value("${app.name}")
private String appName;
// The 'appName' field is automatically injected with the value from the properties file
}
The @Value annotation binds configuration attributes to fields, supporting placeholders (e.g., ${app.name}).
5. Dependency Injection (@Autowired)
@Component
public class MyService {
@Autowired
private MyRepository repository;
// The 'repository' is automatically injected, supporting injection by type or name
}
Spring matches dependencies based on the annotation type (byType/byName) to achieve automatic wiring.
Common Issues
Issue 1: How can Spring be configured to auto-scan components?
Answer: Use the @ComponentScan annotation to specify a scan path, for example:
@ComponentScan(basePackages = "com.example.demo")
public class DemoApp {
// No additional configuration required; Spring will auto-scan components
}
Ensure that the target package paths exist and that component classes are marked with @Component, @Service, or similar annotations.
Issue 2: How can properties from a configuration file be injected?
Answer: Bind attributes using the @Value annotation, for example:
@Component
public class MyComponent {
@Value("${app.theme}")
private String theme;
// The 'theme' field is automatically injected with the value from the properties file
}
The configuration file must contain a key like app.theme; Spring will inject its corresponding value into the field.
Issue 3: How can custom annotations be defined and used?
Answer: Define a custom annotation class, such as:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface CustomAnnotation {
String value() default "Default";
}
Apply it to classes or methods like this:
@CustomAnnotation(value = "Demo")
public class DemoClass {
// The annotation field can be used directly here
}
Spring processes the annotation as configuration information, supporting runtime inspection.