Lab Assignment Spring Framework
Lab Assignment Spring Framework
com.training.model.persistance
@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
@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 {
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class MethodLogger {
private static final Logger logger=LoggerFactory.getLogger(MethodLogger.class);
@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 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;
}
public void updateBook(int id, Book book){}
public Book getBookById(int id){}
}
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 shouldremain 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 MVC
In lab Spring Hibernate you have created book application with persistance layer using spring-
Hibernate,
now we require to integrate that application using Spring MVC
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.
@Autowired
private BookDao dao;
dao.updateBook(id, book);
dao.deleteBook(id);
Spring Security
In lab Spring MVC you have created book application with following design:
Web layer<======> Service layer <=======> Persistance layer <=======> DB