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>{
}
To answer your first question: yes, Spring MVC controllers are singletons by default. An object field will be shared and visible for all requests and all sessions forever. However without any synchronization you might run into all sorts of concurrency issues (race conditions, visibility).
A repository is a mechanism for encapsulating storage, retrieval, and search behavior which emulates a collection of objects. It is a specialization of the @Component annotation allowing for implementation classes to be autodetected through classpath scanning.
@Repository Annotation is used to indicate that the class provides the mechanism for storage, retrieval, update, delete and search operation on objects. @Controller annotation indicates that a particular class serves the role of a controller. @Service Annotation is a specialization of @Component Annotation.
The services and DAOs should be stateless. This would allow you to configure them as Spring singletons. I hope this is what you mean when you say "singleton".
All threading concerns, including the thread pooling, will be taken care of by the infrastructure: your web/Java EE server and Spring DI.
Annotate your service class with @Service and Dao class with @Repository .
<context:component-scan base-package="x.y.z.service, x.y.z.dao" />
it will automatically creates singleton bean for your class
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With