0

我想知道弹簧范围相当于窗口范围吗?根据这里的文档 http://static.springsource.org/spring/docs/3.0.0.M3/spring-framework-reference/html/ch04s04.html

我找不到任何东西,所以如果有自定义范围等价物,谁能告诉我,谢谢。

编辑:JSF 中的窗口范围示例

http://icefaces-showcase.icesoft.org/showcase.jsf?grp=compatMenu&exp=popup

4

1 回答 1

0

一种可能的方法是拥有一个包含所有窗口会话的会话范围 bean,以及一个通过该会话范围 bean 访问相应 bean 的窗口自定义范围。

所需的类是:

窗口会话

public class WindowSession {
    private Map<String, Map<String,Object>> scopeMap = new HashMap<String, Map<String,Object>>();
    private Map<String, Boolean> validSessions = new HashMap<String, Boolean>();

    public WindowSession() {
        super();
    }

    public Map<String, Map<String, Object>> getScopeMap() {
        return scopeMap;
    }

    public void setScopeMap(Map<String, Map<String, Object>> scopeMap) {
        this.scopeMap = scopeMap;
    }

    public Map<String, Boolean> getValidSessions() {
        return validSessions;
    }

    public void setValidSessions(Map<String, Boolean> validSessions) {
        this.validSessions = validSessions;
    }

}

WindowIdContext

@Service
@Scope(value="request", proxyMode=ScopedProxyMode.TARGET_CLASS)
public class WindowIdContext {

    public static final String DEFAULT_SESSION_ID = "";

    private String id = DEFAULT_SESSION_ID;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getQueryParamWithSessionId() {
        if (DEFAULT_SESSION_ID.equals(id)) {
            return "";
        }
        return "?" + WindowScope.WINDOW_ID_PARAM + "=" + id;
    }
}

窗口会话上下文

@Service
@Scope(value="session", proxyMode=ScopedProxyMode.TARGET_CLASS)
public class WindowSessionContext {

    private static SecureRandom random = new SecureRandom();

    private WindowSession windowSession = new WindowSession();

    @Autowired
    private WindowIdContext windowIdContext;

    public WindowSessionContext() {
        windowSession.getValidSessions().put(WindowIdContext.DEFAULT_SESSION_ID, false);
    }

    public Object getObject(String name, ObjectFactory<?> objectFactory) {
        String sessionId = windowIdContext.getId();
        synchronized(this) {
            if (!windowSession.getValidSessions().containsKey(sessionId)) {
                windowSession.getValidSessions().put(sessionId, false);
            }
            Map<String,Object> sessionMap = windowSession.getScopeMap().get(sessionId);
            if (sessionMap == null) {
                sessionMap = new HashMap<String,Object>();
                windowSession.getScopeMap().put(sessionId, sessionMap);
            }
            Object object = sessionMap.get(name);
            if (object == null) {
                object = objectFactory.getObject();
                sessionMap.put(name, object);
            }
            return object;
        }
    }

    public Object removeObject(String name) {
        String sessionId = windowIdContext.getId();
        synchronized(this) {
            Map<String,Object> sessionMap = windowSession.getScopeMap().get(sessionId);
            if (sessionMap == null) {
                return null;
            }
            Object object = sessionMap.remove(name);
            return object;
        }
    }

    public String addSession() {
        synchronized(this) {
            String sessionId;
            do {
                sessionId = new BigInteger(130, random).toString(32);
            } while (windowSession.getValidSessions().containsKey(sessionId));
            windowSession.getValidSessions().put(sessionId, false);
            return sessionId;
        }
    }

    public void removeSession() {
        String sessionId = windowIdContext.getId();
        synchronized(this) {
            windowSession.getScopeMap().remove(sessionId);
            windowSession.getValidSessions().remove(sessionId);
        }
    }

    public boolean isSessionValid(String sessionId) {
        Boolean inUse = windowSession.getValidSessions().get(sessionId);
        return inUse != null;
    }

    public boolean isSessionInUse(String sessionId) {
        Boolean inUse = windowSession.getValidSessions().get(sessionId);
        return inUse == true;
    }

    public synchronized boolean invalidateSession(String sessionId) {
        Boolean valid = windowSession.getValidSessions().get(sessionId);
        if (valid == null) {
            return false;
        }
        if (sessionId.equals(WindowIdContext.DEFAULT_SESSION_ID)) {
            windowSession.getValidSessions().put(sessionId, false);
        } else {
            windowSession.getValidSessions().remove(sessionId);
        }
        windowSession.getScopeMap().remove(sessionId);
        for (Entry<String,Boolean> validSession : windowSession.getValidSessions().entrySet()) {
            if (validSession.getValue() == true) {
                return false;
            }
        }
        return true;
    }

    public synchronized void setSessionInUse(String sessionId) {
        windowSession.getValidSessions().put(sessionId, true);
    }

    public int getCount() {
        return windowSession.getValidSessions().size();
    }
}

视窗镜

public class WindowScope implements Scope {

    private final int scope;
    private static final int WINDOW_SCOPE = 40;
    public static final String NAME = "window";
    public static final String WINDOW_ID_PARAM = "windowid";

    private ServletContext servletContext = null;

    public WindowScope(ServletContext sc) {
        servletContext = sc;
        this.scope = WINDOW_SCOPE;
    }

    @Override
    public Object get(String name, ObjectFactory<?> objectFactory) {
        ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        WindowSessionContext windowSessionContext = applicationContext.getBean(WindowSessionContext.class);
        return windowSessionContext.getObject(name, objectFactory);
    }

    @Override
    public Object remove(String name) {
        ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        WindowSessionContext windowSessionContext = applicationContext.getBean(WindowSessionContext.class);
        return windowSessionContext.removeObject(name);
    }

    @Override
    public String getConversationId() {
        return RequestContextHolder.currentRequestAttributes().getSessionId();
    }

    @Override
    public void registerDestructionCallback(String arg0, Runnable arg1) {
    }

    @Override
    public Object resolveContextualObject(String key) {
        return null;
    }

    protected int getScope() {
        return this.scope;
    }

}

WindowScopeFilter

@Component("windowScopeFilter")
public class WindowScopeFilter implements Filter {

    @Autowired
    private WindowSessionContext windowSessionContext;

    @Autowired
    private WindowIdContext windowIdContext;

    public void destroy() {
    }

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest)request;
        HttpServletResponse httpResponse = (HttpServletResponse)response;
        String sessionId = httpRequest.getParameter(WindowScope.WINDOW_ID_PARAM);
        if (sessionId != null) {
            if (windowSessionContext.isSessionValid(sessionId)) {
                windowIdContext.setId(sessionId);
            } else {
                httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
            }
        } else {
            windowSessionContext.setSessionInUse(WindowIdContext.DEFAULT_SESSION_ID);
        }
        chain.doFilter(request, response);
    }

    public void init(FilterConfig config) throws ServletException {
    }

}

WindowScopeListener

@Component
public class WindowScopeListener implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        WebApplicationContext applicationContext = (WebApplicationContext)event.getApplicationContext();
        Scope windowScope = new WindowScope(applicationContext.getServletContext());
        ConfigurableBeanFactory beanFactory = (ConfigurableBeanFactory)applicationContext.getAutowireCapableBeanFactory();
        beanFactory.registerScope("window", windowScope);
    }

}

带有完整源代码的源代码:http: //notwithoutmycode.com/post/window/scope/in/spring

于 2014-01-11T16:56:13.970 回答