我想知道org.springframework.beans.factory.config.Scope.resolveContextualObject(String key)和的目的是org.springframework.beans.factory.config.Scope.getConversationId()什么?
从javadoc:
对象resolveContextualObject(字符串键)
解决给定键的上下文对象(如果有)。例如,键“ request”的HttpServletRequest对象。
字符串getConversationId()
返回当前基础范围的对话ID(如果有)。对话ID的确切含义取决于基础存储机制。对于会话范围的对象,会话ID通常等于会话ID(或从会话ID派生)。如果自定义对话位于整个会话中,则当前对话的特定ID是合适的。
这个描述告诉我的并不多。您能否举一些例子说明如何使用这些方法?
我的观察是,它resolveContextualObject(String key)看起来像代码的气味,作用域可以在其中暴露一些内部对象。
具有:
public class MyCustomScope implements Scope {
private Pair<String, String> myPair;
@Override
public Object resolveContextualObject(String key) {
if ("myKey".equals(key)) return myPair;
return null;
}
// ...
}
@Configuration
public class RegisterMyScopeConfig {
@Bean
public BeanFactoryPostProcessor beanFactoryPostProcessor() {
return beanFactory -> beanFactory.registerScope(
"mycustomscope", new MyCustomScope());
}
}
Run Code Online (Sandbox Code Playgroud)
然后你可以:
@Scope("mycustomscope")
@Component
class MyComponent {
@Value("#{myKey.first}")
private String firstOfMyPair;
// or
@Value("#{myKey}")
private Pair<String,String> myPair;
}
Run Code Online (Sandbox Code Playgroud)
当然,您如何解析与键匹配的对象的方式可能会更奇特;)。例如,在GenericScope中,它看起来像这样:
@Override
public Object resolveContextualObject(String key) {
Expression expression = parseExpression(key);
return expression.getValue(this.evaluationContext, this.beanFactory);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
377 次 |
| 最近记录: |