Week 9 Flashcards
What is Spring Framework?
Spring Framework is a comprehensive Java-based framework that provides infrastructure support for developing Java applications. It simplifies the development of enterprise-level applications by offering features like dependency injection, aspect-oriented programming, and transaction management.
What are the core features of Spring?
Inversion of Control (IoC) / Dependency Injection (DI)
Aspect-Oriented Programming (AOP)
Transaction management
Data access with JDBC support
MVC (Model-View-Controller) web framework
Integration with various frameworks (Hibernate, JPA, etc.)
What is the primary purpose of the Spring Framework?
To simplify enterprise Java development by managing dependencies, providing a flexible architecture, and supporting various components such as transaction management, web frameworks, and data access.
What is the Spring IoC (Inversion of Control) container?
The IoC container in Spring is responsible for managing the lifecycle and configuration of application objects (beans). It allows objects to be created, injected, and managed without tight coupling between them.
What are the two types of IoC containers in Spring?
BeanFactory
ApplicationContext
What is Inversion of Control (IoC)?
IoC is a design principle where the control of creating objects and managing dependencies is given to the framework instead of being handled by the application code.
How does IoC improve application modularity?
By decoupling the creation of objects from their use, IoC makes components more reusable, testable, and easier to maintain.
What is Dependency Injection (DI)?
DI is a technique in which an object receives its dependencies (other objects) from an external source, typically a container or framework, rather than creating them itself.
What are the benefits of Dependency Injection?
Decoupling components
Easier testing and mocking
Clear dependency management
Improved code modularity
What are the three types of Dependency Injection?
Constructor Injection
Setter Injection
Field Injection
What is Constructor Injection?
Constructor Injection involves passing dependencies to an object through its constructor.
What is Setter Injection?
Setter Injection involves passing dependencies through setter methods after the object is created.
What is Field Injection, and when is it used?
Field Injection involves injecting dependencies directly into fields of a class using annotations like @Autowired. It is less preferred because it hides dependencies and is harder to test.
How do you define a bean in Spring using XML configuration?
A bean can be defined in an XML file using the <bean> tag, specifying the class name and optionally providing constructor arguments or property values.</bean>
<bean>
<property></property>
</bean>
How do you inject dependencies using XML configuration?
By using the <property> tag for setter injection and the <constructor-arg> tag for constructor injection.</constructor-arg></property>
What is the purpose of the <constructor-arg> element in XML configuration?</constructor-arg>
It defines constructor arguments to be passed when a bean is instantiated using constructor injection.
What is Java-based configuration in Spring? .
Java-based configuration is an alternative to XML configuration where beans and dependencies are defined using @Configuration and @Bean annotations in Java classes
How do you define a bean using Java-based configuration?
You use the @Bean annotation inside a class annotated with @Configuration.
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyServiceImpl();
}
}
How do you inject dependencies in Spring using Java-based configuration?
You define beans in a @Configuration class and Spring will manage the injection based on the @Bean methods or @Autowired annotation.
What is annotation-based configuration in Spring?
Annotation-based configuration uses annotations like @Component, @Autowired, and @Configuration to configure beans and inject dependencies without XML.
How is @Autowired used for Dependency Injection?
The @Autowired annotation marks a field, setter, or constructor to indicate that Spring should automatically inject the required dependency.
What is the purpose of @ComponentScan in annotation-based configuration?
@ComponentScan is used to specify the packages that Spring should scan to find and register beans automatically based on annotations like @Component, @Service, and @Controller.
What is component scanning in Spring?
Component scanning is the process by which Spring automatically detects and registers beans defined by annotations like @Component.
How do you enable component scanning in a Spring application?
You enable component scanning by using the @ComponentScan annotation or by specifying a <context:component-scan> element in XML configuration.</context:component-scan>