switched to beanutil to get beans outside spring handled classes
This commit is contained in:
parent
1ecbc91e0e
commit
264bc9efde
28
chatto/src/main/java/org/ros/chatto/logged/BeanUtil.java
Normal file
28
chatto/src/main/java/org/ros/chatto/logged/BeanUtil.java
Normal 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);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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();
|
||||||
@ -60,13 +62,10 @@ public class LoggedUser implements HttpSessionBindingListener {
|
|||||||
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());
|
||||||
@ -78,8 +77,7 @@ public class LoggedUser implements HttpSessionBindingListener {
|
|||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user