Hi Friends,
In this post, I'm highlighting some important concepts used in Spring.
For interview questions and answers , you can go through these posts:
- Microservices Interview-2
- Java 8 interview Questions - Part - I
- Java Interview @ Aricent - Part - 2
- Java Interview @ Gemini Solutions
- Java Interview @ OLX
- Java Interview @ SAP
DTO: DTO is an abbreviation for Data Transfer Object. So, it is used to transfer the data between classes and modules of your application.
Few points to remember while using DTO:
- DTO should contain only private fields for the data , setters , getters and constructors.
- DTO is not recommended to add business logic methods to such classes , but it is ok to add some util methods.
Example of DTO:
interface StudentDTO{
String getName();
void setName(String name);
}
DAO: DAO is an abbreviation for Data Access Object, so it should contain logic for retrieving, saving and updating data in your data storage.
interface StudentDAO{
StudentDTO findById(long id);
void save(StudentDTO student);
}
VO: VO is an abbreviation for Value Object. A Value object is an object such as java.lang.Integer that hold values (hence value objects).
POJO: POJO meant for Plain Old Java Object. Means it is an ordinary light weight object and not a special object and in particular not an enterprise java bean.
Java Bean : A java bean is a class that follows the JavaBeans conventions as defined by Sun.
They are used to encapsulate many objects into a single object (the bean), so that they can be passed around as a single bean object instead of as multiple individual objects.
A javabean is a java object that is serializable and allows access to properties using getter and setter methods.
In order to function as a javabean class, an object class must obey certain conventions about method naming, construction and behavior.
The required conventions are:
- The class must have a public default constructor. This allows easy instantiation.
- The class properties must be accessible using get , set and other methods.
- The class should be serializable. This allows applications and frameworks to reliably save, store and restore the bean's state.
Spring Bean:
A spring bean is basically an object managed by Spring. More specifically, it is an object that is instantiated, configured and otherwise managed by a Spring framework container.
Spring beans are defined in Spring configuration files (or more recently with annotations) instantiated by spring containers and then injected into applications.
@Transactional annotation:
It describes a transaction attribute on an individual method or on a class.
It describes a transaction attribute on an individual method or on a class.
@Transactional annotation defines the scope of a single database transaction. It provides declarative transaction management.
JPA doesn't provide any declarative transaction management.
When using JPA outside of a dependency injection container, transactions need to be handled programmatically by the developer.
UserTransaction utx = entityManager.getTransaction();
try{
utx.begin();
businessLogic();
utx.commit();
}
catch(Exception ex){
utx.rollback();
throw ex;
}
JPA doesn't provide any declarative transaction management.
When using JPA outside of a dependency injection container, transactions need to be handled programmatically by the developer.
UserTransaction utx = entityManager.getTransaction();
try{
utx.begin();
businessLogic();
utx.commit();
}
catch(Exception ex){
utx.rollback();
throw ex;
}
This way of managing transactions makes the scope of transaction very clear in the code, but it has several disadvantages:
- It's repetitive and error prone
- Decreases the readability of the code base
But using @Transactional:
@Transactional
public void businessLogic(){
// use entity manager inside a transaction
}
By using @Transactional, many important aspects such as transaction propagation are handled automatically. In this case, if another transactional method is called by businessLogic(), that method will have the option of joining the ongoing transaction.
What does @Transactional mean?
There are 2 concepts in this:
- Persistence Context
- Database Transaction
The database transaction happens inside the persistence context.
The persistence context is in JPA the EntityManager, implemented internally using an Hibernate Session(when using Hibernate as the persistence provider).
The persistence context is just a synchronizer object that tracks the state of a limited set of java objects and makes sure that changes on those objects are eventually persisted back into the database.
@Transactional annotation should be used @ Service layer. It is the one that knows about units of work and use cases.
A @Service is a contract and modification in presentation layer or repository layer should not require a rewrite of @Service code.
Generally , DAO layer should be as light as possible and should exist solely to provide a connection to the DB, sometimes abstracted so different DB backends can be used together.
Service Layer:
This layer provides code modularity , the business logic and rules are specifies in the service layer which in turn calls DAO layer , the DAO layer then responsible only to interacting with DB.
Service layer provides loose coupling between Controller and DAO layer.
Suppose Controller has 50 methods and they call 20 methods of DAO layer. And if we change these DAO methods , then we need to change the 50 methods of Controller.
But if we take 20 methods in Service layer to call 20 methods in DAO layer, then we just need to change these 20 methods of Service layer.
Repository Layer:
This layer gives additional level of abstraction over data access. Repository layer exposes basic CRUD operations.
A Repository is a data access pattern in which data transfer objects are passed into a repository object that manages CRUD operations.
That's all for this post.
Thanks for reading !!
A Repository is a data access pattern in which data transfer objects are passed into a repository object that manages CRUD operations.
That's all for this post.
Thanks for reading !!
No comments:
Post a Comment