Spring Boot JDBC
Interview Questions
1. What is JDBC Connection Pooling?
-
JDBC connection pooling
is a mechanism that manages multiple database connection requests. -
It facilitates connection reuse, a memory cache of database connections, called a connection pool.
-
JDBC Connection Pooling increases the speed of data access and reduces the number of database connections for an application.
-
It also improves the performance of an application.
2. Why should we use Spring Boot JDBC?
Spring Boot JDBC | Spring JDBC |
---|---|
There is only a spring-boot-starter-jdbc dependency is required. | In Spring JDBC, multiple dependencies need to be configured like spring-jdbc and spring-context . |
It automatically configures Datasource bean, if not maintain explicitly. If we do not want to use the bean, we can set a property spring.datasource.initialize to false . | In Spring JDBC, it is necessary to create a database bean either using XML or javaconfig . |
We do not need to register Template beans because Spring Boot automatically registers beans. | The Template beans such as PlatformTransactionManager , JDBCTemplate , NamedParameterJdbcTemplate must be registered. |
Any db initialization scripts stored in .sql file gets executed automatically. | If any db initialization scripts like dropping or creation of tables are created in SQL file, this info needs to be given explicitly in the configuration. |
3. Where is difference between JDBC and Hibernate?
JDBC | Hibernate |
---|---|
JDBC is a technology . | Hibernate is an ORM framework. |
In JDBC, the user is responsible for creating and closing the connections. | In Hibernate, the run time system takes care of creating and closing the connections. |
It does not support lazy loading. | It supports lazy loading that offers better performance. |
It does not support associations (the connection between two separate classes). | It supports associations. |
Core Spring
Framework Annotations
4.What is @Required annotation in Spring Boot?
@Required
: It applies to thebean setter method
.- It indicates that the annotated bean must be populated at configuration time with the required property, else it throws an exception
BeanInitilizationException
.
public class Machine {
private Integer cost;
@Required
public void setCost(Integer cost){
this.cost = cost;
}
public Integer getCost(){
return cost;
}
}
5.What is @Autowired in Spring Boot?
@Autowired
: Spring providesannotation-based auto-wiring
by providing @Autowired annotation.- It is used to autowire spring bean on
setter methods
,instance variable
, andconstructor
. - When we use @Autowired annotation, the spring container auto-wires the bean by matching data-type.
@Component
public class Customer {
private Person person;
@Autowired
public Customer(Person person) {
this.person=person;
}
}
6.What is @Configuration in Spring Boot?
- It is a
class-level
annotation. - The class annotated with
@Configuration
used bySpring Containers
as a source of bean definitions.
@Configuration
public class Vehicle {
@BeanVehicle engine() {
return new Vehicle();
}
}
7.What is @ComponentScan in Spring Boot?
- It is used when we want to
scan a package for beans
. - It is used with the annotation @Configuration.
- We can also specify the base packages to scan for
Spring Components
@ComponentScan(basePackages = "com.javatpoint")
@Configuration
public class ScanComponent
{
// ...
}
8.What is @Bean in Spring Boot?
- It is a method-level annotation.
- It is an alternative of XML tag.
- It tells the method to produce a bean to be managed by Spring Container.
@Bean
public BeanExample beanExample() {
return new BeanExample ();
}
Spring Framework Stereotype Annotations
9.What is @Component in Spring Boot?
- It is a
class-level
annotation. - It is used to mark a Java class as a bean.
- A Java class annotated with @Component is found during the classpath.
- The Spring Framework pick it up and configure it in the application context as a Spring Bean.
@Component
public class Student
{
.......
}
10.What is @Controller in Spring Boot?
- The @Controller is a
class-level
annotation. - It is a specialization of @Component.
- It marks a class as a web request handler. It is often used to serve web pages.
- By default, it returns a string that indicates which route to redirect. It is mostly used with @RequestMapping annotation.
@Controller
@RequestMapping("books")
public class BooksController
{
@RequestMapping(value = "/{name}", method = RequestMethod.GET)
public Employee getBooksByName()
{
return booksTemplate;
}
}