How do you provide configuration metadata to the Spring Container?
How do you provide configuration metadata to the Spring Container?
To provide configuration metadata to the Spring container, you can use several methods, each suitable for different scenarios and preferences. Here are the primary ways to supply configuration metadata:
<beans>
<bean id="myBean" class="com.example.MyBean">
<property name="dependency" ref="myDependency"/>
</bean>
</beans>
This method is suitable for applications where you want to keep your configuration separate from your Java code[1][3][5][6][8].
@Component
, @Service
, @Repository
, and @Controller
. Dependencies can be injected using @Autowired
. You can also use @Configuration
classes with @Bean
methods to define beans:@Configuration
public class AppConfig {
@Bean
public MyBean myBean() {
return new MyBean();
}
}
This approach is more modern and is preferred when you want to keep configuration close to the code it configures[1][3][5].
@Configuration
annotated classes along with @Bean
annotations to define your beans and their dependencies:@Configuration
public class MyConfig {
@Bean
public MyBean myBean() {
return new MyBean();
}
}
This method is type-safe and refactoring-friendly, as it leverages the Java compiler and IDE capabilities[3][5][6][9][10].
@ConfigurationProperties
...junior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào