Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring中的会话或请求范围变量_Java_Spring_Web Applications_Guice - Fatal编程技术网

Java Spring中的会话或请求范围变量

Java Spring中的会话或请求范围变量,java,spring,web-applications,guice,Java,Spring,Web Applications,Guice,我希望在Spring中有一个作用域变量。在Guice中,它非常简单: @Singleton class MyBean { @Inject @Named("session-scoped") private Provider<Integer> someString; void doSomething() { // returns a random number for a current session. // Each u

我希望在Spring中有一个作用域变量。在Guice中,它非常简单:

@Singleton
class MyBean
{
    @Inject @Named("session-scoped")
    private Provider<Integer> someString;
    void doSomething()
    {
        // returns a random number for a current session.
        // Each user session should generate new number,
        // but one session should keep the same number.
        Integer n1 = someString.get();
        Integer n2 = someString.get();
        assert n1 == n2;
    }
}
...
class MyModule extends AbstractModule
{
    @Override
    protected void configure()
    {
    ...
    }

    @Provides
    @SessionScoped
    @Named("session-scoped") Integer someString()
    {
        return new Random().nextInt();
    }
}
@Singleton
类MyBean
{
@注入@Named(“会话范围”)
私有提供者字符串;
无效剂量测定法()
{
//返回当前会话的随机数。
//每个用户会话都应生成新编号,
//但是一个会话应该保持相同的数字。
整数n1=someString.get();
整数n2=someString.get();
断言n1==n2;
}
}
...
类MyModule扩展了AbstractModule
{
@凌驾
受保护的void configure()
{
...
}
@提供
@会议范围
@命名的(“会话范围”)整数someString()
{
返回新的Random().nextInt();
}
}
我怎么能在春天做类似的事情呢


请记住,它是java.lang.Integer,而不是某些用户bean,它不能被aop代理。

在您的情况下,您不需要会话范围。您只需要一个单例自定义提供程序实例

interface CustomProvider<E> {
    public E get();
}

@Configuration
class TestConfig {
    @Bean
    public CustomProvider<Integer> factory() {
        return new CustomProvider<Integer>() {
            @Override
            public Integer get() throws BeansException {
                return new Random().nextInt();
            }
        };
    }
}
您也可以在
@组件
注释类上使用
@范围
注释

在XML中

<bean id="someBean" class="com.example.SomeBean" scope="session" />


以及如何使用它?你能给我看看注射点吗?请记住,它是
java.lang.String
,不是SomeBean,它不能被代理。@kan啊,我没看到。让我再打给你。我想这是你能做的最好的了。匹配您在Guice中的操作。嗯,他确实需要会话作用域才能在同一会话中获得相同的值。使用您的提供程序添加答案。@EmersonFarrugia啊,应该已经阅读了代码中的注释。这是我自己已经做过的最好的一次,但我被迫将值缓存在提供程序中。。。在更复杂的场景中-值是从其他注入bean计算的,我无法在构造函数中进行初始化,这看起来更糟,我需要添加多线程安全性。听起来您的域模型不够强大。问问自己“这些值是什么意思?”“它们为什么是会话作用域?”“它们是如何关联的?”等等。如果您可以将它们建模为实体对象(),那么您可以将该对象建模为具有会话作用域的bean,将其注入到任何需要的地方,并让其他bean对其进行变异。我不喜欢Spring如此具有侵扰性,它规定了我应该如何设计域模型。目前它只是一个缓存的参数,如果它在域模型中的位置变得更加清晰,我会对它进行重构,但是因为spring,我应该使我的设计过于复杂,guice更好,它足够灵活,不会用样板代码和它自己的guts污染我的类。顺便说一句,使用
Provider
的方法不适合我,spring自动连接了自己的
DefaultListableBeanFactory.DependencyProvider
类,然后在上下文中懒洋洋地查找
Integer
类型,没有注意到
Provider
,因为它无法支持我所理解的泛型。因此,我应该创建
CustomIntegerProvider
垃圾类。我已经讨厌春天了。关于:
Provider
,我假设那是你自己的课,我的回答只是@sotirios delimanolis的
CustomProvider
的后续内容。re:泛型,假设您使用的是Spring4,Spring支持使用泛型类型自动连接类。re:你的领域模型,我的观点是思考你在做什么;你实际上是通过名字注入了一个神奇的整数。这是一种“全局变量”代码味道,无论您使用的是Guice、Spring还是其他什么<代码>整数也是最后一个对象,防止容器构建代理来桥接会话和单例作用域。
@Bean
@Scope(value = WebApplicationContext.SCOPE_SESSION /* or simply "session" */)
public SomeBean someBean() {
    return new SomeBean();
}
<bean id="someBean" class="com.example.SomeBean" scope="session" />
@Component
@Scope("session")
public class IntegerProvider implements Provider<Integer> {

   private Integer value = new Random().nextInt();

   public Integer get() {
       return this.value;
   }
}
@Autowired
private Provider<Integer> integerProvider;
assert this.integerProvider.get().equals(this.integerProvider.get();