2-lab assignment spring boot framework-assignment
2-lab assignment spring boot framework-assignment
Create BookStore application by following 3 tier architecture with following dao server layer
com.training.model.persistance
@Repository
public class BookDaoImp implements BookDao {
private static Map<Integer, Book> booksMap = new HashMap<Integer, Book>();
private static int counter = 0;
static {
booksMap.put(++counter, new Book(counter, "ABC123", "Head first Java" , "Katthy",
600));
booksMap.put(++counter, new Book(counter, "ABC723", "Servlet jsp Java" , "Katthy",
700));
}
@Override
public List<Book> getAllBooks() {
return new ArrayList<Book>(booksMap.values());
}
@Override
public Book addBook(Book book) {
book.setId(++counter);
booksMap.put(counter, book);
return booksMap.get(counter);
}
@Override
public void deleteBook(int id) {
booksMap.remove(id);
}
@Override
public void updateBook(int id, Book book) {
booksMap.put(id, book);
}
@Override
public Book getBookById(int id) {
return booksMap.get(id);
}
com.training.model.service
@Service
public class BookServiceImp implements BookService {
@Autowire
private BookDao dao;
@Override
public List<Book> getAllBooks() {
return dao.getAllBooks();
}
@Override
public Book addBook(Book book) {
return dao.addBook(book);
}
@Override
public void deleteBook(int id) {
dao.deleteBook(id);
}
@Override
public void updateBook(int id, Book book) {
dao.updateBook(id, book);
}
@Override
public Book getBookById(int id) {
return dao.getBookById(id);
}
In lab DI you have created book application, now we require to log information while somebody deleted
book
Improve the application using AOP to applying logging, You can create an custom annotation such as
@Loggable and put it one deleteBook method. Now if book is deleted information about deleted book
should added to log file in aop way.
You can use code snippet mentioned below.
@Service
public interface BookService implements BookService {
@Autowire
private BookDao dao;
public List<Book> getAllBooks(){
}
public Book addBook(Book book){}
Custom annotation
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Loggable {
@Around("@annotation(Loggable)")
public Object around(ProceedingJoinPoint point) throws Throwable {
long start = System.currentTimeMillis();
Object result = point.proceed();
logger.info("start "+MethodSignature.class.cast(" Method takes " +(System.currentTimeMillis() - start));
return result;
}
}
Spring REST
In this lab you need to support REST endpoint for book application.
Take the help of given code snippet. Note use of @RestController and @ RequestMapping annotation.
In case validation fail user must get correct status code 400 with proper validation failure message
Validation message must not be hard coded
Spring JDBC
In lab AOP you have created book application, now we require to replace persistance layer with jdbc. Note
that service layer should
remain uneffected by the change.
@Service
public interface BookService implements BookService {
@Autowire
private BookDao dao;
Implement crud method using jdbc, inject datasource in persistane layer, using xml and java configuration
Spring Hibernate/JPA
In lab Spring Jdbc you have created book application using JDBC, now we require to replace persistance
layer with Hibernate. Note that service layer should remain uneffected by the change.
@Service
public interface BookService implements BookService {
@Autowire
private SessionFactory factory;
}
public void updateBook(int id, Book book){}
public Book getBookById(int id){}
}
1. Implement crud method using Hibernate, inject factory in persistane layer, using xml and java
configuration
2. Implement crud method using spring-Hibernate ( reduce boilerplat code)
3. Apply declerative transaction management using @Transactinal annotation
4. Do experiments with different optionals available with @Transactinal annotation
Spring boot is higly customized framework, although spring boot automatically configure datasource,
entitymanagerfactory etc but we can configure our bean along with transaction management, In this lab write
custom configuration of these beans
Spring boot profile help us to use different configuration depending on dev,test and prod profile, using
different database for different profile and switch profile without changing the code
To integrate JSP with Spring Boot, you need to add the following dependencies to your project:spring-boot-
starter-web, tomcat-embed-jasper, and jstl.
Spring Boot provides a number of utilities and annotations to help when testing your application. Test
support is provided by two modules: spring-boot-test contains core items, and spring-boot-test-autoconfigure
supports auto-configuration for tests.
Most developers use the spring-boot-starter-test “Starter”, which imports both Spring Boot test modules as
well as JUnit, AssertJ, Hamcrest, and a number of other useful libraries.
OpenFeign is a declarative web service client. It makes writing web service clients easier. When calling
other services using Feign, we don't need to write any code. We just need to create an interface and annotate
it. It has support for OpenFeign and Jakarta JAX-RS annotations.
RabbitMQ is an open-source messaging broker that allows communication between different software
systems, services, and devices. It uses the Advanced Message Queuing Protocol (AMQP), a standardized
communication protocol for scalable messaging. RabbitMQ is made up of broker processes that host
exchanges for publishing messages and queues for consuming messages. It's often compared to a post office
for applications and messages.