switched to beanutil to get beans outside spring handled classes

This commit is contained in:
Rohan Sircar 2019-11-19 11:51:51 +05:30
parent 1ecbc91e0e
commit 264bc9efde
2 changed files with 40 additions and 26 deletions

View File

@ -0,0 +1,28 @@
package org.ros.chatto.logged;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
@Service
public class BeanUtil implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}
public static <T> T getBean(Class<T> beanClass) {
return context.getBean(beanClass);
}
}

View File

@ -27,17 +27,19 @@ public class LoggedUser implements HttpSessionBindingListener {
private String username; private String username;
public LoggedUser(String username) public LoggedUser(String username) {
{
this.username = username; this.username = username;
} }
public LoggedUser() { public LoggedUser() {
} }
@Override @Override
public void valueBound(HttpSessionBindingEvent event) { public void valueBound(HttpSessionBindingEvent event) {
UserService userService = getUserService(event); // UserService userService = getUserService(event);
UserSessionRepository userSessionRepository = getUserSessionRepository(event); UserService userService = BeanUtil.getBean(UserService.class);
// UserSessionRepository userSessionRepository = getUserSessionRepository(event);
UserSessionRepository userSessionRepository = BeanUtil.getBean(UserSessionRepository.class);
LoggedUser user = (LoggedUser) event.getValue(); LoggedUser user = (LoggedUser) event.getValue();
Instant instant = Instant.now(); Instant instant = Instant.now();
@ -59,14 +61,11 @@ public class LoggedUser implements HttpSessionBindingListener {
@Override @Override
public void valueUnbound(HttpSessionBindingEvent event) { public void valueUnbound(HttpSessionBindingEvent event) {
LoggedUser user = (LoggedUser) event.getValue(); LoggedUser user = (LoggedUser) event.getValue();
UserService userService = getUserService(event); UserService userService = BeanUtil.getBean(UserService.class);;
UserSessionRepository userSessionRepository = getUserSessionRepository(event); UserSessionRepository userSessionRepository = BeanUtil.getBean(UserSessionRepository.class);;
Instant instant = Instant.now(); Instant instant = Instant.now();
ChatUser chatUser = userService.findByUserName(user.getUsername()); ChatUser chatUser = userService.findByUserName(user.getUsername());
UserSession userSession = userSessionRepository.findByUserName(user.getUsername()); UserSession userSession = userSessionRepository.findByUserName(user.getUsername());
@ -75,11 +74,10 @@ public class LoggedUser implements HttpSessionBindingListener {
// userSession = new UserSession(); // userSession = new UserSession();
throw new UsernameNotFoundException("User session not found"); throw new UsernameNotFoundException("User session not found");
} }
int numSessions = userSession.getNumSessions(); int numSessions = userSession.getNumSessions();
if(--numSessions == 0) if (--numSessions == 0) {
{
userSession.setOnline(false); userSession.setOnline(false);
} }
@ -88,16 +86,4 @@ public class LoggedUser implements HttpSessionBindingListener {
userSession.setNumSessions(numSessions); userSession.setNumSessions(numSessions);
userSessionRepository.save(userSession); userSessionRepository.save(userSession);
} }
private UserService getUserService(HttpSessionEvent se) {
WebApplicationContext context = WebApplicationContextUtils
.getWebApplicationContext(se.getSession().getServletContext());
return (UserService) context.getBean(UserService.class);
}
private UserSessionRepository getUserSessionRepository(HttpSessionEvent se) {
WebApplicationContext context = WebApplicationContextUtils
.getWebApplicationContext(se.getSession().getServletContext());
return (UserSessionRepository) context.getBean(UserSessionRepository.class);
}
} }