小编Fra*_*ski的帖子

Spring:根据上下文(会话/ Web或本地线程/后台进程)注入bean

是否可以创建一个工厂或代理来决定线程是在(Web)请求还是后台进程(即调度程序)中运行,然后根据该信息创建会话bean或原型bean?

示例(伪Spring配置:)

<bean id="userInfoSession" scope="session" />
<bean id="userInfoStatic" scope="prototype" />

<bean id="currentUserInfoFactory" />

<bean id="someService" class="...">
    <property name="userInfo" ref="currentUserInfoFactory.getCurrentUserInfo()" />
</bean>
Run Code Online (Sandbox Code Playgroud)

我希望这会让我的问题更容易理解......


我的解决方案

更新自己的问题永远不会迟到;).我用两个不同的客户端会话实例,一个SessionScoped客户端会话和一个SingletonScoped会话解决了它.两者都是正常豆类.

<bean id="sessionScopedClientSession" class="com.company.product.session.SessionScopedClientSession" scope="session">
    <aop:scoped-proxy />
</bean>

<bean id="singletonScopedClientSession" class="com.company.product.session.SingletonScopedClientSession" />

<bean id="clientSession" class="com.company.product.session.ClientSession">
    <property name="sessionScopedClientSessionBeanName" value="sessionScopedClientSession" />
    <property name="singletonScopedClientSessionBeanName" value="singletonScopedClientSession" />
</bean>
Run Code Online (Sandbox Code Playgroud)

然后ClientSession将决定单例或会话范围:

private IClientSession getSessionAwareClientData() {
    String beanName = (isInSessionContext() ? sessionScopedClientSessionBeanName : singletonScopedClientSessionBeanName);
    return (IClientSession) ApplicationContextProvider.getApplicationContext().getBean(beanName);
}
Run Code Online (Sandbox Code Playgroud)

可以通过以下方式收集会话类型:

private boolean isInSessionContext() {
    return RequestContextHolder.getRequestAttributes() != null;
}
Run Code Online (Sandbox Code Playgroud)

所有类都实现了一个名为IClientSession的接口.singletonScoped和sessionScoped bean都从找到实现的BaseClientSession扩展.

然后,每个服务都可以使用客户端会话,即:

@Resource
private ClientSession …
Run Code Online (Sandbox Code Playgroud)

session spring background

7
推荐指数
1
解决办法
1281
查看次数

Spring 的 ISO-8859 请求上的 Jetty Utf8Appendable$NotUtf8Exception

远程服务使用 ISO-8859-15 编码的请求调用我们的 Jetty 服务器。这个特殊请求被映射到 Spring 控制器上。Jetty 无法以正确的方式对请求进行编码,并显示以下异常:

exception=org.eclipse.jetty.util.Utf8Appendable$NotUtf8Exception: Not valid UTF8! byte F6 in state 3}
org.eclipse.jetty.util.Utf8Appendable$NotUtf8Exception: Not valid UTF8! byte F6 in state 3
    at org.eclipse.jetty.util.Utf8Appendable.appendByte(Utf8Appendable.java:168) ~[na:na]
    at org.eclipse.jetty.util.Utf8Appendable.append(Utf8Appendable.java:93) ~[na:na]
    at org.eclipse.jetty.util.UrlEncoded.decodeUtf8To(UrlEncoded.java:506) ~[na:na]
    at org.eclipse.jetty.util.UrlEncoded.decodeTo(UrlEncoded.java:554) ~[na:na]
    at org.eclipse.jetty.server.Request.extractParameters(Request.java:285) ~[na:na]
    at org.eclipse.jetty.server.Request.getParameter(Request.java:695) ~[na:na]
    ....
Run Code Online (Sandbox Code Playgroud)

解决方案

在 Spring 中,即使整个应用程序都使用 UTF-8,也可以通过 CharacterEncodingFilter 强制对请求进行编码。异常应该消失。

<filter>
    <filter-name>encoding-filter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>ISO-8859-15</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>encoding-filter</filter-name>
    <url-pattern>/app/specialRequest.do</url-pattern>
</filter-mapping>
Run Code Online (Sandbox Code Playgroud)

如果这不适合你

  • 找出远程系统编码
  • 启动Wireshark通过 ip.src == xxx.xxx.xxx.xxx 过滤器分析传入的包
  • 在请求正文中搜索特殊字符(将十六进制值重新计算为二进制,并尝试几种常用的编码以准确找到与异常匹配的编码)
  • 通过 Jetty 的 start.ini …

encoding jetty

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

background ×1

encoding ×1

jetty ×1

session ×1

spring ×1