Câu hỏi phỏng vấn Spring
Câu hỏi

How do you provide configuration metadata to the Spring Container?

Câu trả lời

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:

  1. XML-Based Configuration: This is the traditional method where you define beans and their dependencies in an XML file. The Spring container reads this XML file to instantiate and configure beans. An example of XML-based configuration metadata is as follows:
<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].

  1. Annotation-Based Configuration: With the introduction of annotations, you can define beans directly in your Java classes using annotations like @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].

  1. Java-Based Configuration: This is a type of annotation-based configuration where you use Java classes to define your configuration. You can use @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].

  1. Configuration Properties: For type-safe configuration, you can use @ConfigurationProperties...
junior

junior

Gợi ý câu hỏi phỏng vấn

junior

Do you need spring-mvc.jar in your classpath or is it part of spring-core?

expert

I want to know what actually happens when you annotate a method with @Transactional?

senior

What are the advantages of Spring MVC over Struts MVC ?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào