Spring Boot面试每日10问(3)

文章讨论了SpringBoot中JDBC连接池的作用,SpringBoot使用JDBC的优势,以及与Hibernate的对比。还介绍了Spring框架的核心注解,如@Required、@Autowired、@Configuration、@ComponentScan和@Bean的用途。同时提到了@Component和@Controller在SpringBoot中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 JDBCSpring 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?

JDBCHibernate
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 the bean 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 provides annotation-based auto-wiring by providing @Autowired annotation.
  • It is used to autowire spring bean on setter methods, instance variable, and constructor.
  • 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 by Spring 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;  
	}  
}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值