1. What is IoC in the context of Spring Framework?
Inversion of Control (IoC) in Spring is the principle of delegating object construction and dependency injection to the Spring IoC container.
2. What is the primary purpose of the BeanFactory interface?
BeanFactory provides a configuration framework for managing objects and is capable of handling any object type in Spring.
3. How does ApplicationContext differ from BeanFactory?
ApplicationContext, a sub-interface of BeanFactory, adds enterprise-specific features like AOP integration, message resource handling, event publication, and application-layer contexts.
4. What are Spring beans?
In Spring, beans are objects managed by the IoC container. They are instantiated, assembled, and controlled by the Spring framework.
5. What role does configuration metadata play in Spring IoC?
Configuration metadata in Spring defines the beans and their dependencies, guiding the IoC container in object instantiation and management.
6. What is the primary purpose of the BeanFactory interface?
BeanFactory provides a configuration framework for managing objects and is capable of handling any object type in Spring.
7. What is the role of the ApplicationContext interface in Spring?
The ApplicationContext interface represents the Spring IoC container, responsible for bean instantiation, configuration, and assembly.
8. How is configuration metadata represented in Spring IoC?
Configuration metadata in Spring IoC is represented in XML, Java annotations, or Java code, defining object composition and interdependencies.
9. How can you enable support for Java annotations or code as metadata formats in Spring?
In Spring, you can enable support for Java annotations or code as metadata formats by providing a small XML configuration.
10. What is the purpose of the @Bean annotation in Spring's Java configuration?
The @Bean annotation signifies a method that instantiates, configures,
and initializes an object managed by the Spring IoC container, equivalent to the
11. How is the @Configuration annotation used in Java-based Spring configuration?
Annotating a class with @Configuration indicates that it serves as a source of bean definitions. It allows inter-bean dependencies to be defined by calling other @Bean methods in the same class.
12. What is the difference between full @Configuration and "lite" @Bean mode in Spring?
In full @Configuration, beans and inter-bean dependencies are declared within the annotated class. In "lite" @Bean mode, the methods are considered factory methods and operate on the component's internal state without declaring inter-bean dependencies.
1. What is Spring AOP?
Spring AOP (Aspect-Oriented Programming) is a programming paradigm that allows separating cross-cutting concerns from the core business code, enhancing modularity and maintainability.
2. What are cross-cutting concerns?
Cross-cutting concerns are common aspects that span multiple modules of an application, such as logging, security, transactions, and exception handling.
3. How does an aspect work in Spring AOP?
An aspect in Spring AOP is represented by one or more join points and a set of advice that is executed at these join points. Aspects can be configured to run before, after, or around the join points.
4. What is a join point in the context of Spring AOP?
A join point represents a specific point of execution within the application where advice can be applied. For example, a method execution or a method call.
5. What are the three types of advice in Spring AOP?
The three types of advice are: 1. Before advice: Runs before a join point. 2. After returning advice: Runs after a join point successfully completes. 3. After throwing advice: Runs after a join point throws an exception.
6. What is a pointcut in Spring AOP?
A pointcut defines a set of join points in an application. It specifies the places where advice should be applied.
7. How is an aspect configured in Spring AOP?
An aspect in Spring AOP is configured using the @Aspect annotation and defining the associated advice and pointcuts.
8. Major Annotations in Spring AOP: @Aspect
Marks a class as an aspect. It contains advice and pointcut definitions.
9. Major Annotations in Spring AOP: @Before
Specifies that a method is a "before" advice, which runs before a join point.
10. Major Annotations in Spring AOP: @AfterReturning
Indicates that a method is an "after returning" advice, executed after a join point successfully completes.
11. Major Annotations in Spring AOP: @AfterThrowing
Marks a method as an "after throwing" advice, executed after a join point throws an exception.
12. Major Annotations in Spring AOP: @Around
Declares a method as an "around" advice, allowing customization of the join point's behavior.
13. What is a Pointcut in Spring AOP?
A Pointcut is a set of one or more join points where advice should be executed. It defines a specific condition or set of conditions that matches join points.
14. What is a Pointcut expression?
A Pointcut expression is a string-based expression language used to define Pointcuts. It specifies the criteria for matching join points.
15. Example Pointcut Expression
An example of a Pointcut expression: @Pointcut("execution(* com.example.service.*.*(..))"). This expression matches any method execution in the specified package.
16. Basic Pointcut Expressions
Basic Pointcut Expressions include: - `execution`: Matches method execution join points. - `within`: Matches join points within specified types or packages. - `this`: Matches join points where the bean reference is an instance of a specific type. - `target`: Matches join points where the target object is an instance of a specific type. - `args`: Matches join points where the arguments are instances of specified types. - `@annotation`: Matches join points where the subject has a specific annotation.
17. Combining Pointcut Expressions
Pointcut expressions can be combined using logical operators: @Pointcut("execution(* com.example.service.*.*(..)) && within(com.example..*)").
18. Dynamic Pointcut with @Pointcut
You can use the @Pointcut annotation to define a reusable pointcut expression and refer to it in advice annotations.
19. AspectJ-Style Pointcut Expressions
Spring AOP supports AspectJ-style pointcut expressions for advanced matching. Example: @Pointcut("execution(* com.example..*.*(..)) && @annotation(org.springframework.transaction.annotation.Transactional)").
20. What is Weaving in Spring AOP?
Weaving is the process of integrating aspects into the application's code at specified join points. It can occur at different times during the application's lifecycle.
21. Compile-Time Weaving (CTW)
- Definition: A type of weaving where aspects are integrated at compile-time. - Pros: Early integration, potentially better performance. - Cons: Requires a special build process, less dynamic.
22. Runtime Weaving (RTW)
- Definition: A type of weaving where aspects are integrated at runtime. - Pros: More flexible, supports dynamic changes. - Cons: Slightly more runtime overhead.
23. What is a Proxy in the context of Spring AOP?
A proxy is an object that acts as a placeholder for another object, controlling access to it. In Spring AOP, proxies are used to implement aspect-oriented programming.
24. JDK Dynamic Proxy
- Type: Java standard library feature. - Usage: Interface-based proxying, requires target objects to implement interfaces. - Creation: Created using the java.lang.reflect.Proxy class.
25. CGLIB Proxy
- Type: Third-party library (CGLIB). - Usage: Class-based proxying, works with classes and interfaces. - Creation: Generated dynamically at runtime by subclassing the target class.
26. When does Spring use JDK Dynamic Proxy vs. CGLIB Proxy?
- JDK Dynamic Proxy: Used when the target object implements at least one interface. - CGLIB Proxy: Used when the target object doesn't implement any interfaces.
27. Proxy Advantages in Spring AOP
- Loose Coupling: Proxies allow for non-intrusive weaving, promoting loose coupling. - Runtime Flexibility: Proxies support runtime decisions on advice application.