- What is SpringBoot ? How it is different from Spring framework ?
Spring Boot is an extension of the Spring Framework that simplifies the development of Java applications by providing auto-configuration, an embedded server, and opinionated defaults.
Basis | Spring | Spring Boot |
---|---|---|
Where it’s used? | Spring framework is a java EE framework that is used to build applications. | Spring Boot framework is mainly used to develop REST API’s |
Key feature | The primary or most important feature of the Spring framework is dependency injection(Dependency Injection (DI) is a design technique that removes dependencies from computer code, making the application easier to maintain and test). | The main or primary feature of the Spring Boot is Autoconfiguration( Simply described, Spring Boot autoconfiguration is a method of automatically configuring a Spring application based on the dependencies found on the classpath.) Autoconfiguration can speed up and simplify development by removing the need to define some beans that are part of the auto-configuration classes. |
Why it’s used | Its goal is to make Java EE (Enterprise Edition) development easier, allowing developers to be more productive. | Spring Boot provides the RAD(Rapid Application Development) feature to the Spring framework for faster application development. |
Type of Application Development | Spring framework helps to create a loosely coupled application. | Spring Boot helps to create a stand-alone application. |
Servers dependency | In the Spring framework to test the Spring Project, we need to set up the servers explicitly. | Spring Boot offers built-in or embedded servers such as Tomcat and jetty. |
In-memory database support | Spring framework does not provide support for the in-memory database. | Spring Boot provides support for the in-memory database such as H2. |
Boilerplate code | Spring framework requires too many lines of code (boilerplate code) even for minimal tasks. | You avoid boilerplate code which reduces time and increases productivity. |
Configurations | In the Spring framework, you have to build configurations manually. | In Spring Boot there are default configurations that allow faster bootstrapping. |
Dependencies | Spring Framework requires a number of dependencies to create a web app. | Spring Boot, on the other hand, can get an application working with just one dependency. There are several more dependencies required during build time that is added to the final archive by default. |
Testing | Testing in Spring Boot is difficult in comparison to Spring Boot due to a large amount of source code. | Testing in Spring Boot is easier due to the reduced amount of source code. |
XML Configuration | In the Spring framework, XML Configuration is required. | No need for XML configuration in Spring Boot. |
CLI Tools | Spring framework does not provide any CLI tool for developing and testing applications. | Spring Boot provides a CLI tool for developing and testing Spring Boot applications. |
Plugins | Spring framework does not provide any plugin for maven, Gradle, etc. like Spring Boot. | Spring Boot provides build tool plugins for Maven and Gradle. The Plugins offer a variety of features, including the packaging of executable jars. |
- What is the purpose of SpringBootApplication Annotation?
@SpringBootApplication comprises of @EnableAutoConfiguration, @ComponentScan and @Configuration. Its typically used to mark the main class of Spring Boot application. It enables Spring Boot's auto-configuration mechanism, which automatically configures the Spring application based on its dependencies and the content of the classpath.
- What are Spring Boot starters?
Spring Boot starters are pre-configured dependency bundles that simplify adding dependencies to a project. They help developers avoid manually specifying multiple dependencies by providing a single starter dependency for common use cases.
- What is Spring Boot Actuator ?
<dependency>
<groupId> org.springframework.boot</groupId>
<artifactId> spring-boot-starter-actuator </artifactId>
</dependency>
- Difference between @Primary and @Qualifier annotation?
In Spring Framework, @Primary and @Qualifier are used for resolving bean conflicts when multiple beans of the same type are available in the Application Context.
- @Primary - Used to mark a bean as the default choice when the multiple beans of the same type exists. Applied at the class level on a bean definition. Works when there is no explicit @Qualifier used in injection.
- @Qualifier - Used to specify the exact bean to be injected when multiple candidates exists. Ovverides @Primary if both are present. Applied at the injection point (constructor, method or field)
- What is Dependency Injection ?
Dependency Injection (DI) is a design pattern used to manage dependencies between objects by injecting them rather than creating them inside a class. It helps in building loosely coupled, testable and maintainable applications.
- Explain bean scopes in Spring Boot ?
In Spring Boot, a bean scope defines how and when a Spring bean is created and shared within the Spring IoC(Inversion of Control) container. Spring provides different bean scopes for different use cases.
- Singleton: The default scope, where only one instance of a bean is created for the entire application.
- Prototype: A new instance is created for each time the bean is requested.
- Request : A new instance is created for each HTTP request.
- Session: A new instance is created for each user session.
- Application: A new instance is created once for the entire application.
- WebSocket: A new instance is created for each WebSocket session.
- Explain @Autowired annotation?
The @Autowired annotation in Spring Boot is used for automatic dependency injection. It tells Spring to inject a bean automatically without manually creating an instance.
- What is IoC Container?
Inversion of Control (IoC) is a design principle where the control of object creation and dependency management is shifted from the application code to a framework or container. In Spring Boot, this is managed by the Spring IoC Container.
Instead of manually creating objects, Spring Boot automatically created and injects dependencies where needed.
IoC Container is responsible for:
- Creating and managing beans (objects in Spring).
- Injecting dependencies into beans (@Autowired)
- Handling bean lifecycle (initialization and destruction).
- What are profiles in Spring boot ?
In Spring Boot, profiles are a way to define and manage different configurations for different environments (e.g., development, testing, production). They allow you to separate configuration
settings that might change depending on the environment where the application is running.
Profiles can be defined in two main ways:
- Application properties: You can specify profiles in the
application.properties
orapplication.yml
files. - Java code: You can use the
@Profile
annotation to specify beans that should only be loaded under certain profiles.
Example: Using
@Profile
Annotation in Java ClassesYou can use the @Profile
annotation in Spring beans to specify that a bean is only available for certain profiles:
- Difference between BeanFactory and ApplicationContext ?
Below are some differences between BeanFactory & ApplicationContext :-
Aspect | BeanFactory | ApplicationContext |
---|---|---|
1. Definition | It is "basic container" that manages beans with minimal features. | Its is "advanced container" extending BeanFactory with extra features. |
2. Usage | It is suitable for small, standalone applications. | It is suitable for web applications, AOP, ORM and large enterprise applications. |
3. Resource Usage | It requires less memory, suitable for lightweight or memory-critical applications. | It uses more memory, providing additional features for enterprise applications. |
4. Initialization | It uses lazy initialization i.e. it creates the beans only when needed. | It uses eager initialization i.e. it loads and creates the beans at container startup. |
5. Bean Scopes Supported | It supports only Singleton and Prototype scopes. | It supports all scopes, including Request and Session, for web contexts. |
6. Annotation Support | It doesn’t support annotations; requires XML configuration. | It supports annotations for simpler configuration. |
7. Internationalization | It does not provide support for internationalization (i18n). | It provides the i18n support for multilingual applications. |
8. Event Handling | It does not support event handling. | It supports event handling via ApplicationEvent and ApplicationListener. |
9. Bean Post Processing | It requires manual registration of BeanPostProcessor and BeanFactoryPostProcessor. | It automatically registers BeanPostProcessor and BeanFactoryPostProcessor at startup. |
- What does @ComponentScan annotation do ?
The
@ComponentScan
annotation in Spring is used to specify the base packages to scan for Spring beans. It tells Spring to look in certain packages for classes annotated with @Component
, @Service
, @Repository
, @Controller
, or other stereotype annotations, and then automatically register those classes as Spring beans in the application context.You typically use
@ComponentScan
in a configuration class annotated with @Configuration
, though it's also commonly used in combination with @SpringBootApplication
in Spring Boot applications (since @SpringBootApplication
includes @ComponentScan
by default).Here’s a basic example:
- How to connect Spring Boot with a database?
Configuring a Spring Boot application to connect to a database is a common task, and Spring Boot provides built-in support for various databases (like MySQL, PostgreSQL, H2, etc.) through the use of Spring Data JPA and Spring JDBC. Here's a step-by-step guide on how to configure Spring Boot with a relational database.
Step 1 - Add Dependencies : Spring Boot makes it easy to connect to databases by including the necessary dependencies in your pom.xml (for Maven) or build.gradle (for Gradle).
For Maven:
If you're using Spring Data JPA and a relational database like MySQL or PostgreSQL, add the following dependencies to your
pom.xml
:
For Gradle:
If you're using Gradle, you can add these dependencies in
build.gradle
:Step 2 - Configure
application.properties
or application.yml
You need to define the database connection properties in
application.properties
or application.yml
.Example for
application.properties
(for MySQL):
No comments:
Post a Comment