Statistics
78
Views
0
Downloads
0
Donations
Support
Share
Uploader

高宏飞

Shared on 2025-11-23

AuthorAmit Himani

Welcome to this book on Spring interview questions! If you are reading this, chances are you are gearing up for a job interview that requires knowledge of the Spring framework. Congratulations on taking this step towards advancing your career! Whether you are just starting your career or are a seasoned professional, having a strong understanding of Spring can help you land your dream job or excel in your current role. This book aims to provide you with a comprehensive set of interview questions and answers related to Spring, one of the most widely used Java frameworks today. I cover a broad range of topics, including Core Spring concepts, Spring Boot, Spring MVC, Spring Security, and many more. Each question has been carefully crafted to test your understanding of the framework and your ability to apply it in real-world scenarios. However, it's important to note that Spring is a vast framework with numerous ossibilities, and this book cannot possibly cover everything.

Tags
No tags
Publish Year: 2023
Language: 中文
File Format: PDF
File Size: 8.5 MB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

(This page has no text content)
1 Preface ...........................................................................................................2 Acknowledgement .........................................................................................3 1. Spring Core ................................................................................................4 2. Spring MVC ..............................................................................................30 3. Spring Boot ...............................................................................................41 4. Spring Data ..............................................................................................52 5. Spring Security .........................................................................................64 6. Spring Cloud .............................................................................................81 7. Spring Batch .............................................................................................89 8. Spring Integration ..................................................................................101 9. Spring AOP .............................................................................................107 10. Reactive Spring .....................................................................................117 11. Testing & Troubleshooting in Spring ......................................................123 Parting Thoughts ........................................................................................140 My Tweets Let's Connect Ещё больше книг по Java в нашем телеграм канале:                                        https://t.me/javalib
2 P R E FA C E Welcome to this book on Spring interview questions! If you are reading this, chances are you are gearing up for a job interview that requires knowledge of the Spring framework. Congratulations on taking this step towards advancing your career! Whether you are just starting your career or are a seasoned professional, having a strong understanding of Spring can help you land your dream job or excel in your current role. This book aims to provide you with a comprehensive set of interview questions and answers related to Spring, one of the most widely used Java frameworks today. We have tried to cover a broad range of topics, including core Spring concepts, Spring Boot, Spring MVC, Spring Security, and many more. Each question has been carefully crafted to test your understanding of the framework and your ability to apply it in real-world scenarios. However, it's important to note that Spring is a vast framework with numerous possibilities, and this book cannot possibly cover everything. Therefore, we encourage you to use this book as a supplement to your preparation and not rely solely on it. It's essential to gain practical experience with the framework by working on projects and experimenting with different features. We hope that this book will help you feel confident and well-prepared for your Spring-related job interview. Good luck! keep learning, keep practicing, and all the best in your interview preparation! My Tweets Let's Connect
3 A C K N O W L E D G E M E N T I couldn't have written this book on Spring interview questions without the help and support of so many people. Firstly, a huge thank you to Nilofar Makhani for her invaluable contribution to this book. Nilofar's tireless effort in editing and proofreading the manuscript has resulted in a book that is clear, concise and easy to understand. Her attention to detail and commitment to excellence is truly inspiring, and I am grateful to have worked with her on this project. I also want to express my gratitude to my parents, who have always encouraged and supported me in all my endeavours. Their unwavering love and belief in me has been a constant source of motivation throughout my life. I would also like to thank the Spring community for their incredible contributions to the open-source documentation. The Spring framework is a vast and complex ecosystem, and the countless hours put into documenting the various features and components have been invaluable in providing insights and understanding. Finally, I would like to acknowledge all the developers and engineers who have contributed to the Spring framework over the years. Their hard work and dedication have made Spring one of the most widely used Java frameworks, and this book is a small tribute to their incredible efforts. Thank you all for your support and encouragement. This book wouldn't have been possible without you. My Tweets Let's Connect
4 1 . S P R I N G C O R E 1. What is Spring Core? Spring Core is the fundamental module of the Spring Framework that provides the essential components and features for building enterprise Java applications. It includes a lightweight container for managing Java objects, also known as the Inversion of Control (IoC) container or the Spring container. The container manages the configuration of application components and the dependencies between them, allowing developers to focus on writing business logic rather than managing object creation and wiring. Spring Core also provides support for aspects, events, and resources such as internationalisation, validation, and data binding. Additionally, it includes various utility classes and interfaces for common tasks such as logging, data access, and exception handling. Overall, Spring Core provides the foundation for other modules in the Spring Framework, such as Spring MVC, Spring Data, Spring Security, and many more. 2. What is Dependency Injection (DI)? Dependency Injection (DI) is a design pattern that is used to reduce the coupling between objects in a Java application. In Spring Core, DI is achieved through the use of inversion of control (IOC) containers that manage the dependencies between objects. 3. What is Inversion of Control (IOC)? Inversion of Control (IOC) is a design principle that dictates that the control of a program should be handed over to a framework or container, rather than being controlled by the application itself. In Spring Core, IOC is achieved through the use of IOC containers that manage the lifecycle of objects and their dependencies. My Tweets Let's Connect
5 4. Compare Dependency Injection with Inversion of Control: Criteria Dependency Injection (DI) Inversion of Control (IoC) Definition A technique for managing object dependencies by passing them to an object instead of creating or looking them up itself A broader design pattern that involves transferring control of objects or flow of control to a container or framework Focus Managing object dependencies Managing control flow or overall architecture of an application Implementati on A subset of IoC pattern A broader design pattern that includes DI Purpose Reduce tight coupling and improve code maintainability, testability, and scalability Facilitate separation of concerns, improve code modular ity, and simplify application architecture Dependency direction Dependencies are passed to an object Control of objects or flow of control is transferred to a container or framework Types Constructor injection, setter injection, interface injection Constructor-based IoC, setter-based IoC, and interface-based IoC Benefits Promotes loose coupling, improves testability and maintainability, and facilitates modularity Simplifies application architecture, promotes modular design, and facilitates separation of concerns Examples Spring framework, Guice, Ninject Spring framework, Microsoft Unity, Google Guice My Tweets Let's Connect
6 5. What is the difference between BeanFactory and ApplicationContext? In summary, BeanFactory provides the basic features for managing Spring beans, while ApplicationContext builds on top of BeanFactory and provides more advanced features such as internationalisation, event handling, web support, and environment awareness. Therefore, ApplicationContext is generally preferred over BeanFactory in most Spring applications. 6. What are the different types of Dependency Injection in Spring? • Constructor Injection: In this type of DI, the dependencies are injected through the constructor of a class. The Spring container creates an instance of the class and passes the required dependencies to the constructor at the time of instantiation. This approach is commonly used when the class has mandatory dependencies. • Setter Injection: In this type of DI, the dependencies are injected through the setter methods of a class. The Spring container creates an instance of the class and then calls the setter methods to inject the required dependencies. This approach is commonly used when the class has optional dependencies. • Field Injection: In this type of DI, the dependencies are injected directly into the fields of a class using Java reflection. This approach is less common and less preferred than constructor or setter injection, as it can make the code less testable and harder to maintain. Feature BeanFactory ApplicationContext Bean instantiation Lazily initialized Can be eagerly initialized Bean post-processing Supported Supported Bean validation Not supported Supported Message resource handling Not supported Supported AOP features Basic support Full support Event handling Not supported Supported Web-specific features Not supported Supported Environment awareness Limited support Full support Customizable PropertyEditors Supported Supported Hierarchical support Supported Supported My Tweets Let's Connect
7 7. What is the purpose of a BeanPostProcessor in Spring? The BeanPostProcessor interface in Spring framework provides hooks that allow developers to customise the bean creation and initialisation process. The interface d e fi n e s t w o m e t h o d s : p o s t P r o c e s s B e f o r e I n i t i a l i z a t i o n a n d postProcessAfterInitialization, which are called before and after the initialisation of a bean respectively. By implementing the BeanPostProcessor interface, developers can add custom logic to the Spring container that modifies the behaviour or properties of beans during the initialisation process. This can be useful for performing operations such as validation, security checks, or adding additional functionality to the bean instances. For example, a developer may implement a BeanPostProcessor that performs security checks on the beans before they are initialised or add custom annotations to the bean instances. Another example is a BeanPostProcessor that performs validation on the properties of the bean instance. 8. What is the purpose of a BeanFactoryPostProcessor in Spring? The purpose of a BeanFactoryPostProcessor in Spring is to modify the Spring ApplicationContext's configuration metadata before the creation of any bean instances. It is an extension point that allows developers to customise the configuration of bean definitions, such as changing property values, adding new properties, or removing existing ones. The BeanFactor yPostProce sso r inte rface define s a s ing le metho d , postProcessBeanFactory, which is called by the Spring container during its startup phase. This method receives a ConfigurableListableBeanFactory as an argument, which allows developers to modify the bean definitions or configuration metadata. One of the primary use cases of a BeanFactoryPostProcessor is to modify property values of beans defined in the Spring configuration files. For example, a developer may use a BeanFactoryPostProcessor to dynamically set the value of a property based on the environment, such as a database URL or a configuration file path. Another use case is to add new beans to the Spring container. For instance, a BeanFactoryPostProcessor can create new bean definitions and register them with the container, which can be useful for dynamic configuration of beans. In summary, the BeanFactoryPostProcessor is a Spring interface that provides an extension point for customising the configuration of the Spring ApplicationContext. My Tweets Let's Connect
8 9. Describe usage of Spring Expression Language (SpEL) module  • Providing a syntax for creating and evaluating expressions • Supporting dynamic evaluation of expressions at runtime • Enabling configuration of the application context at runtime, which provides greater flexibility and configurability • Providing a way to access application-specific beans, properties, and methods within expressions • Supporting conditional expressions, iteration, and other control structures • Enabling integration with other Spring modules such as Spring Security for access control decisions and Spring Integration for message routing.In summary, the Spring Expression Language module provides a powerful and flexible way to configure and manipulate objects within a Spring application context. 10. What is the Spring Container? The Spring Container is the core component of the Spring Framework, which manages the lifecycle of the Spring beans and provides the infrastructure for dependency injection. It is responsible for instantiating, configuring, and managing the lifecycle of objects created by Spring Framework. The Spring container is an implementation of the Inversion of Control (IoC) design pattern, which means that it manages the creation and wiring of objects based on the configuration metadata provided to it, rather than requiring the objects to manage their dependencies themselves. 11. What is the difference between the Spring container and other Java containers? The main difference between the Spring container and other Java containers, such as Java EE application servers or Java SE containers, is that the Spring container provides a lightweight and modular approach to building enterprise applications, without requiring a full-fledged Java EE application server. Here are some key differences between the Spring container and other Java containers: • Lightweight and modular: The Spring container is lightweight and modular, which means that you can choose only the required modules and libraries for your application, rather than having to use a full Java EE stack. • POJO-based programming: The Spring container is based on Plain Old Java Objects (POJOs), which are easy to develop, test and maintain, as they do not require any special annotations or interfaces. My Tweets Let's Connect
9 • Dependency Injection: The Spring container provides built-in support for Dependency Injection (DI), which allows you to easily manage the dependencies between different components in your application. • AOP support: The Spring container provides built-in support for Aspect-Oriented Programming (AOP), which allows you to separate cross-cutting concerns, such as logging or security, from the main business logic of your application. • Integration with other frameworks: The Spring container provides seamless integration with other popular Java frameworks, such as Hibernate, Struts, and JSF, making it easier to build complex enterprise applications. • Standalone deployment: The Spring container can be deployed as a standalone Java application, which makes it easier to manage and deploy your application without requiring a full Java EE application server. 12. When to use the BeanFactory vs. ApplicationContext? • If you are using small scale, light weight spring based application, prefer using BeanFactory container as it takes less memory to work. In other words, if there is a memory limitation in place, prefer using BeanFactory container. For example, mobile apps, embedded system apps, IOT apps etc. • In all other heavy weight apps where memory limitation is not in place, such as web apps, enterprise apps, distributed apps, desktop apps etc., prefer using ApplicationContext container. • In general practice, we should prefer using ApplicationContext container wherever it is possible to use. You must have a genuine reason of not using ApplicationContext Container. 13. What is a configuration class in Spring Core? In Spring Core, a configuration class is a Java class that is used to define the Spring bean definitions and their configuration. It is an alternative to XML-based configuration that allows developers to configure their Spring applications using Java code. A configuration class is typically annotated with the @Configuration annotation, which indicates to the Spring container that the class contains bean definitions. The class may also contain methods annotated with @Bean annotations, which are used to define individual bean definitions. Configuration classes may also use other annotations to provide additional functionality, such as: My Tweets Let's Connect
1 0 • @ComponentScan: This annotation is used to specify the packages that should be scanned by the Spring container for bean definitions. • @PropertySource: This annotation is used to specify the properties files that should be loaded by the Spring container. • @Profile: This annotation is used to specify the profiles that should be activated for the current Spring application. • @Import: This annotation is used to import other configuration classes into the current configuration class. Using configuration classes provides several advantages over XML-based configuration, such as: • Type safety: Configuration classes are written in Java, which provides compile-time type safety and eliminates the need for string-based configuration. • Refactoring: Since configuration classes are written in Java, they can be easily refactored using standard Java tools. • Debugging: Debugging configuration classes is easier than debugging XML configuration files since the Java code is more expressive. In summary, a configuration class in Spring Core is a Java class that is used to define bean definitions and their configuration. It is an alternative to XML-based configuration that provides several advantages such as type safety, refactoring, and debugging. 14. What is a bean scope in Spring Core? In Spring Core, a bean scope refers to the lifecycle and visibility of a bean instance in the Spring container. The scope determines how long the bean instance will be available and how many instances of the bean will be created by the container. Spring provides several bean scopes, including: • Singleton: This is the default scope for Spring beans. A singleton bean is created once per container and is shared among all objects that reference it. Any modifications to the bean will be visible to all objects that use the bean. • Prototype: A prototype bean is created each time it is requested by an object, and a new instance is returned. Any modifications made to the prototype bean will not affect other objects that use the bean. My Tweets Let's Connect
1 1 • Request: A request-scoped bean is created once per HTTP request and is available only within that request. Once the request is complete, the bean is destroyed. • Session: A session-scoped bean is created once per HTTP session and is available only within that session. Once the session is invalidated, the bean is destroyed. • Global session: This scope is similar to session scope, but it is used in a Portlet context where multiple portals share a single HTTP session. • Application: An application-scoped bean is created once per ServletContext and is available to all objects within that context. • Websocket: A Websocket-scoped bean is created once per WebSocket session and is available only within that session. By default, beans are singleton-scoped, but you can change the scope of a bean by using the @Scope annotation or by specifying the scope attribute in the bean definition. The choice of scope depends on the use case and requirements of the bean. 15. What is a singleton bean scope and best practices around it? In Spring Framework, a singleton bean scope means that the Spring container creates only one instance of the bean and shares it among all objects that request it. Once the bean is created, the same instance is returned to all objects that need it. Any modifications made to the singleton bean are visible to all objects that use it, as they all share the same instance. By default, all beans in Spring Framework are singleton-scoped, unless otherwise specified. You can specify the scope of a bean explicitly by using the @Scope annotation or by defining the scope attribute in the bean configuration XML file. The singleton bean scope is suitable for stateless objects that are thread-safe and immutable, such as utility classes and service objects. It is not recommended to use the singleton scope for stateful objects or objects that maintain a conversational state, as this can lead to concurrency issues and unexpected behaviour. 16. What is a prototype bean scope? Spring Framework, a prototype bean scope means that the Spring container creates a new instance of the bean every time it is requested by an object. Unlike singleton beans, prototype beans are not shared among objects and each object gets its own instance of the bean. Any modifications made to the prototype bean do not affect other instances of the bean. My Tweets Let's Connect
1 2 You can define a prototype bean scope by using the @Scope annotation or by defining the scope attribute in the bean configuration XML file. The prototype bean scope is suitable for stateful objects or objects that need to maintain their own state, such as user session objects or objects that maintain a conversational state. However, it is important to note that creating a new instance of the bean for each request can have performance implications and may not be suitable for high traffic applications or objects that are expensive to create. It is also worth noting that because prototype-scoped beans are not managed by the Spring container once they are created, they can lead to memory leaks if not properly cleaned up. Therefore, it is important to be mindful of the lifecycle of prototype beans and to properly manage their creation and destruction. 17. What is a request bean scope? In Spring Framework, a request bean scope means that the Spring container creates a new instance of the bean for each HTTP request that is received by the application. This means that each instance of the bean is specific to a single HTTP request and is not shared among other requests or objects. The request bean scope is typically used for objects that need to maintain state across multiple HTTP requests, such as web controllers or handlers that process incoming requests. By creating a new instance of the bean for each request, the Spring container ensures that each request is handled by a separate instance of the bean, thereby preventing any concurrency issues or interference between requests. You can define a request bean scope by using the @Scope annotation or by defining the scope attribute in the bean configuration XML file. It is worth noting that the request bean scope is only available in web applications that use the Servlet API. It is also important to properly manage the lifecycle of request-scoped beans to avoid any memory leaks or performance issues, such as cleaning up any resources associated with the bean after the request has been processed. 18. What is a session bean scope? In Spring Framework, a session bean scope means that the Spring container creates a new instance of the bean for each HTTP session that is created by the application. This means that each instance of the bean is specific to a single HTTP session and is not shared among other sessions or objects. The session bean scope is typically used for objects that need to maintain state across multiple HTTP requests within a single session, such as shopping cart or user My Tweets Let's Connect
1 3 preferences. By creating a new instance of the bean for each session, the Spring container ensures that each session is handled by a separate instance of the bean, thereby preventing any concurrency issues or interference between sessions. Additionally, session-scoped beans should be serializable if the application is running in a clustered environment. 19. What is a global session bean scope? In Spring Framework, a global session bean scope means that the Spring container creates a single instance of the bean for the entire application, but the bean instance is tied to a specific global HTTP session. A global session is a concept in Servlet API that allows for sharing data across multiple browser windows or tabs that belong to the same user. When a user opens a new browser window or tab, a new HTTP session is created, but a global session is maintained across all windows or tabs. By defining a bean with a global session scope, Spring can create a single instance of the bean for the entire application, but the instance is tied to the user's global session. This allows for the bean to be shared across multiple browser windows or tabs while maintaining its state. You can define a global session bean scope in Spring by using the @Scope annotation or by defining the scope attribute in the bean configuration XML file with the value "globalSession". Global session beans are useful when you need to maintain a stateful object across multiple browser windows or tabs, such as a chat application or an online game. However, it's important to note that using global session beans can have implications on performance and memory usage, especially if the application has a large number of users. 20. What is bean wiring in Spring Framework? In Spring Framework, bean wiring is the process of connecting different objects or components (i.e., beans) together to form a working application. It's a mechanism by which the Spring container manages the dependencies between beans and ensures that the correct instance of each bean is used when it's needed. Bean wiring involves three steps: • Defining the beans: First, you need to define the beans that make up your application. This can be done using annotations or XML configuration files. My Tweets Let's Connect
1 4 • Declaring dependencies: Once you have defined the beans, you need to declare their dependencies. This can be done using annotations, XML configuration files, or programmatically. • Letting the container do its work: Finally, you let the Spring container take over and wire the beans together. The container uses dependency injection to inject the required dependencies into each bean. There are two main types of bean wiring in Spring: • Constructor injection: In this type of wiring, dependencies are passed to the bean through its constructor. This is useful when a bean has a small number of dependencies. • Setter injection: In this type of wiring, dependencies are set using setter methods. This is useful when a bean has a large number of dependencies or when you want to be able to change the dependencies at runtime. Bean wiring is a key feature of Spring Framework as it allows for loose coupling between components, making the application more modular and easier to maintain. It also makes testing easier since you can easily replace dependencies with mock objects during testing. 21. What is constructor injection? Constructor injection is a type of dependency injection in which dependencies are provided to a class through its constructor. In this approach, the dependencies are declared as constructor parameters, and the Spring container is responsible for creating the instances of these dependencies and passing them to the constructor when the class is instantiated. For example (1.1), consider a class MyClass that depends on two other classes DependencyA and DependencyB. The constructor injection approach would involve My Tweets Let's Connect Code Snippet 1.1
1 5 defining a constructor that takes instances of DependencyA and DependencyB as parameters When an instance of MyClass is created, the Spring container will automatically create instances of DependencyA and DependencyB and pass them to the constructor. Constructor injection has some advantages over other forms of dependency injection. Since all dependencies are provided at object creation time, the object is guaranteed to be in a fully initialised state when it is created. This can simplify object initialisation and improve code readability. It also makes it easier to enforce immutability, since the constructor can be used to set all of an object's final fields. 22. What is setter injection? Setter injection is a type of dependency injection in which dependencies are provided to a class through setter methods. In this approach, the dependencies are declared as private fields in the class, and then setter methods are defined to set the values of these fields. For example (1.2) , consider a class MyClass that depends on two other classes DependencyA and DependencyB. The setter injection approach would involve defining setter methods for DependencyA and DependencyB: My Tweets Let's Connect Code Snippet 1.2
1 6 When an instance of MyClass is created, the Spring container will create instances of DependencyA and DependencyB, and then call the appropriate setter methods to set their values. Setter injection has some advantages over other forms of dependency injection. One advantage is that it can make code more readable, since the class's dependencies are set explicitly and in a clear and understandable way. Another advantage is that it makes it easy to modify the class's dependencies, since new dependencies can be set simply by calling the appropriate setter methods. However, one disadvantage of setter injection is that it can make it harder to ensure that all required dependencies are set before the object is used, since there is no guarantee that the setter methods will be called in the correct order. 23. What is autowiring in Spring? In simpler terms, autowiring allows Spring to automatically inject the required dependencies into a bean when it is created. Spring can determine the type of dependency required by examining the class definition of the bean and then look for a matching bean definition in its container. If a match is found, Spring injects the dependency automatically, without any further configuration. Autowiring can be done by type, by name, by constructor, or by using custom annotations. It simplifies the configuration process and makes it easier to maintain and update the application as the dependencies change. 24. What are the different types of autowiring modes in Spring? In Spring, there are five different types of autowiring mode: • "no": This is the default mode. In this mode, autowiring is not performed, and dependencies must be manually wired using the @Autowired or @Qualifier annotations. • "byName": In this mode, Spring searches for a bean with the same name as the property that needs to be autowired. If a bean with the matching name is found, it is injected into the property. • "byType": In this mode, Spring searches for a bean that is of the same type as the property that needs to be autowired. If a single bean of the matching type is found, it is injected into the property. If multiple beans of the same type are found, an exception is thrown. My Tweets Let's Connect
1 7 • "constructor": In this mode, Spring uses the constructor of the class to autowire the dependencies. It searches for beans with matching types and injects them into the constructor parameters. • "autodetect": In this mode, Spring first attempts to autowire by constructor. If this fails, it falls back to "byType" autowiring. 25. What is the Spring Bean’s life cycle? The life cycle of a Spring bean can be divided into several phases, each of which involves a different set of operations. Here are the main phases of a Spring bean's life cycle: • Instantiation: This is the first phase of the bean life cycle, in which the Spring container creates a new instance of the bean class. This can be done either through a constructor or through a factory method. • Population of properties: After the bean is instantiated, Spring will populate any properties of the bean that are set through dependency injection. This is done using setters, fields or constructor arguments. • Bean post-processing: Spring provides several interfaces to allow developers to perform additional processing of the bean after it has been instantiated and populated with its properties. This includes the BeanPostProcessor interface, which provides methods to manipulate bean instances before and after their initialization. • Initialisation: Once all of the bean's properties have been set, the Spring container will call any init methods specified on the bean, if they exist. This is the last opportunity for any final initialisation before the bean is ready for use. • Usage: At this point, the bean is fully initialised and ready for use by other objects in the application. My Tweets Let's Connect
1 8 • Destruction: When the application context is shut down, the Spring container will destroy all of the beans it created by calling their destroy methods if they exist. This provides a final opportunity for the bean to clean up any resources it might have acquired during its lifetime. It's important to note that not all beans will have all of these phases. For example, some beans may not have any properties that need to be injected, or they may not have any init or destroy methods. However, understanding the overall life cycle of a Spring bean is important for writing effective Spring applications. 26. Does Spring Bean provide thread safety? The default scope of Spring bean is singleton, so there will be only one instance per context. That means that all the having a class level variable that any thread can update will lead to inconsistent data. Hence in default mode spring beans are not thread-safe. However, we can change spring bean scope to request, prototype or session to achieve thread-safety at the cost of performance. It’s a design decision and based on the project requirements. Here is the link for more info. 27. What is the @Autowired annotation? The @Autowired annotation is a Spring Framework annotation that is used to inject dependencies into a Spring-managed bean. It can be used to autowire fields, constructors, and methods. When the @Autowired annotation is used on a field, Spring automatically injects the required dependency into that field. In this example(1.3), Spring will automatically inject the MyRepository dependency into the myRepository field when MyService is instantiated. My Tweets Let's Connect Code Snippet 1.3
1 9 28. What is the @Qualifier annotation? The @Qualifier annotation is a Spring Framework annotation that is used to specify which bean should be autowired when multiple beans of the same type are available. For example (1.4), suppose you have two implementations of a MessageService interface: If you then have a component that requires a MessageService dependency (1.5), but you do not specify which implementation to use, Spring will throw an exception because it does not know which implementation to autowire: To specify which implementation of MessageService to use, you can use the @Qualifier annotation: In this example (1.6), the @Qualifier annotation specifies that the emailService implementation of MessageService should be injected into the messageService field. My Tweets Let's Connect Code Snippet 1.4 Code Snippet 1.5