scope="session"
で定義されたBeanは<aop:scoped-proxy/>
で作成されますが、このスコープ付きCGLIBプロキシにアクセスするとセッション外でも正しく動作します。私は、そのプロキシをオートワイヤリングするか、アプリケーションコンテキストから直接取得することを試みましたが、全く新しいスレッドであっても、リクエストのセッションの一部ではないたびに、プロキシではなくプロキシからの実際の値がnull
またはプロキシの投げ例外。ここでセッションスコープ付きBeanはセッション外スレッドで動作します
は、Bean定義
<bean id="commons" class="foo.bar.Commons" scope="session">
<aop:scoped-proxy/>
<property name="securityEnabled" value="true"/>
<property name="modificationAllowed" value="true"/>
<property name="autoSave" value="true"/>
</bean>
そして、ここでは、私はそれを使用する方法である例である:
public class CommonsModificatorProvider implements ModificatorProvider, ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public String getCurrentModificator() {
try {
Commons commons = applicationContext.getBean(Commons.class);
if (commons == null)
return "system";
String user = commons.getCurrentUser();
if (user == null)
return "system";
return user;
} catch (Exception e) {
return "system";
}
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
しかし、さえ全く新しいスレッドで、getCurrentUser()
は常にいくつかの値を返します。
私はCommonsModificationProvider
prototype
というスコープを作り、それをオートワイヤリングする代わりに、アプリケーション・コンテキストから毎回それをインスタンス化しましたが、違いはありません。 autowired 'singleton'スコープバージョンと同じCGLIBプロキシを取得します。