What is difference between @RestController and @Controller?
@Controller
is used to mark classes as Spring MVC Controller.
@RestController
is a convenience annotation that does nothing more than adding the @Controller
and @ResponseBody
annotations
So the following two controller definitions should do the same
@Controller
@ResponseBody
public class MyController { }
@RestController
public class MyRestController { }
@Controller
is used to mark classes as Spring MVC Controller.@RestController
is a convenience annotation that does nothing more than adding the @Controller
and @ResponseBody
annotations
So the following two controller definitions should do the same
@Controller
@ResponseBody
public class MyController { }
@RestController
public class MyRestController { }
Which Spring Modules have you worked on?
Some mostly used spring modules are,
- Spring MVC
- Spring AOP
- Spring Security
- Spring JDBC
- Spring Core
- Spring Web-MVC
- Spring Boot
You may refer spring.io for more details of each module.
What benefits spring JDBC provides over jdbc?
The JDBC abstraction framework provided by Spring consists of four different packages:
- The
core
package contains theJdbcTemplate
. This class is one of the fundamental classes provided by and used by the Spring framework's JDBC support. - The
datasource
package is a great fit for unit testing database access code. ItsDriverManagerDataSource
can be used in a way that is similar to what you are already used to from JDBC: just create a newDriverManagerDataSource
and call the setter methods to setDriverClassName
,Url
,Username
, andPassword
. - The
object
package contains classes that represent RDBMS queries, updates, and stored procedures as thread safe, reusable objects. - The
support
package is where you can find theSQLException
translation functionality and utility classes.
The Template Design Pattern
Spring JDBC implements the Template design pattern, meaning the repetitive plumbing parts of the code are implemented in template classes. This approach simplifies the use of JDBC, since it handles the creation and release of resources. This helps to avoid common errors like forgetting to close the connection. It executes the core JDBC workflow tasks like statement creation and execution, leaving application code to provide SQL and extract results.
Spring JDBC Exception Handling
The Spring framework addresses the problems faced in traditional JDBC programming with the following solutions:
- Spring provides an abstract exception layer, moving verbose and error-prone exception handling out of application code into the framework. The framework takes care of all exception handling; application code can concentrate on extracting results by using appropriate SQL.
- Spring provides a significant exception hierarchy for your application code to work with in place of
SQLException
.
With the help of an abstract exception layer, we achieve database independence without having to change the exception handling. For example, if you change your database from PostgreSQL to Oracle, you do not have to change the exception handling from OracleDataException
to PostgresDataException
. Spring catches the application-server-specific Exception
and throws a Spring data exception.
When dealing with exceptions, Spring examines the metadata available from a database connection to determine the database product. It uses this knowledge to map SQLException
to the correct exception in its own hierarchy. So, we need not worry about proprietary SQL state or error codes; Spring's data access exceptions are not JDBC-specific, so your DAOs are not necessarily tied to JDBC because of the exceptions they may throw.
SQLException
.OracleDataException
to PostgresDataException
. Spring catches the application-server-specific Exception
and throws a Spring data exception.SQLException
to the correct exception in its own hierarchy. So, we need not worry about proprietary SQL state or error codes; Spring's data access exceptions are not JDBC-specific, so your DAOs are not necessarily tied to JDBC because of the exceptions they may throw.What is Spring MVC and how it is useful?
Model - holds data eg. Bean
View - Contains view code like JSP, HTML page.
Controller - Controller holds business logic. Binds View and Models together.
Mainly MVC allows different components to be modified with no or very less side effects.
What features does spring boot provide?
- convention over configuration, get you up and running quickly, stand-alone production grade apps
- Create stand-alone Spring applications
- Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
- Provide opinionated 'starter' POMs to simplify your Maven configuration
- Automatically configure Spring whenever possible
- Provide production-ready features such as metrics, health checks and externalized configuration
- Absolutely no code generation and no requirement for XML configuration
No comments:
Post a Comment