I am creating a web application using spring ,hibernate. Suppose multiple users wants to register.I will create a registration bean(prototype or request or session scoped) and autowire it in Controller.
Now I am passing this bean to Registration Service(annotated with "@transactional" annotation) which is also autowired in the controler. This service will pass recieved registeration bean object to DAO(This DAO is autowired in the service)If service and DAO are singelton would'nt the requests be mixed up for multiple users?
Here is what I have done :I have created the scope of service and DAO as "request". Is this the right approach? or what else could I do to make the service and DAO singelton?
My logic behind request scoped: The reason for making service and DAO as request scoped is if multiple users call registerationService.registerUser(bean); from the controller at same time and scope is singelton then there would be no consistency coz there one object's methods are called with different inputs.
LET ME KNOW WHERE I AM WRONG.
Registeration Bean
@Component(value="registerBean")
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS, value = "request")
public class RegisterBean {
@NotNull
private String userName;
private String lastName;
@NotNull
private String firstName;
String email_address;
String password;
String confirmPassword;
String gender;
//getters and setters
}
Controller
package com.ClickToShop.controllers;
@Controller
@SessionAttributes("user_info")
public class LoginPageController {
RegisterBean registerBean;//used
RegisterationService registerationService;//used
@Autowired
@Qualifier("registerationService")
public void setRegisterationService(RegisterationService registerationService) {
this.registerationService = registerationService;
}
@Autowired
@Qualifier("registerBean")
public void setRegisterBean(RegisterBean registerBean) {
this.registerBean = registerBean;
}
@ModelAttribute(value = "registerBean")
RegisterBean returnModelAttribute() {
return registerBean;
}
@RequestMapping(value = "/login-page.html")
public String showLoginPage() {
System.out.println("Showing login page");
System.out.println(registerBean);
return "login-page";
}
@RequestMapping(value = "/newuser-register", method = RequestMethod.POST)
public String registernewuser( @ModelAttribute("registerBean") @Valid RegisterBean bean, BindingResult result,final RedirectAttributes redirectAttr)
throws NoSuchAlgorithmException, UnsupportedEncodingException {
//some validation code
registerationService.registerUser(bean);
return "redirect:successRegisteration";
}
}
}
Service Layer
@Service("registerationService")
@Transactional
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS,value="request")
public class UserServiceImpl implements RegisterationService {
private User_Details_Pojo userToRegisterPojo;
private AbstractHibernateDAO UserDAO;
@Autowired
public void setUserDAO(AbstractHibernateDAO userDAO) {
UserDAO = userDAO;
}
@Autowired
@Qualifier("userToRegisterPojo")
public void setUserToRegisterPojo(User_Details_Pojo userToRegisterPojo) {
this.userToRegisterPojo = userToRegisterPojo;
}
//main implementation code starts here
@Override
public void registerUser(Object userBean) {
RegisterBean bean=(RegisterBean) userBean;
//bean or model is converted to pojo
UserDAO.save(userToRegisterPojo);//calling DAO with specified pojo
}
}
DAO:
public abstract class AbstractHibernateDAO<T extends Serializable> {
public Class<T> clazz;//class object reference
protected SessionFactory mysessionFactory;
@Autowired
public void setMysessionFactory(SessionFactory mysessionFactory) {
this.mysessionFactory = mysessionFactory;
}
public T findOneByName(final String name){
return (T) getCurrentSession().createQuery("from "+clazz.getName()).uniqueResult();
}
public void setClazz(final Class<T> clazzToSet) {
this.clazz = clazzToSet;
}
public T findOne(final Long id) {
return (T) getCurrentSession().get(clazz, id);
}
@SuppressWarnings("unchecked")
public List<T> findAll() {
return getCurrentSession().createQuery("from " + clazz.getName()).list();
}
public void save(final T entity) {
getCurrentSession().merge(entity);
}
public void update(final T entity) {
getCurrentSession().update(entity);
}
public void delete(final T entity) {
getCurrentSession().delete(entity);
}
public void deleteById(final Long entityId) {
final T entity = findOne(entityId);
delete(entity);
}
protected Session getCurrentSession() {
return mysessionFactory.getCurrentSession();
}
}
Concrete DAO
@Repository
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS,value="request")
public class UserDAO extends AbstractHibernateDAO<User_Details_Pojo>{
}