java - 如何配置 Bean 原型(prototype)范围提供程序以在 Bean 创建时使用 session 信息?

标签 java spring spring-boot

每次访问代理以获取实例时,我都需要提供基于 session 信息的bean。我怎样才能做到这一点?

现在我尝试了以下方法。例如:

第一个类定义了一个 session 范围的 bean。

@Component
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class BeanSession implements Serializable {

    private Serializable rootState;

    public <T extends Serializable> T getRootState() {
        return (T) rootState;
    }

    public void setRootState(Serializable rootState) {
        this.rootState = rootState;
    }
}

第二类有一些与其领域相关的逻辑,并且也知道如何提供信息。每次都必须创建 bean,因为信息在线程处理期间可能会发生变化。因此,每次访问 Attribute1 时,我一定会获得包含最新信息的 bean。

@Service
public class Attribute1Service {

    @Resource
    private BeanSession beanSession;

    public void setDefaultValue() {
        Configuration configuration = beanSession.getRootState();
        configuration.getAttribute1().setValue("VALUE 1");
    }

    @Bean
    public Attribute1 attribute1() {
        Configuration configuration = beanSession.getRootState();
        return configuration.getAttribute1();
    }

}

最后,第三个类将 attribute1 声明为依赖项来执行自己的逻辑。

@Service
public class Attribute2Service {

    @Resource
    private BeanSession beanSession;

    @Resource
    private Processor processor;

    @Resource
    private Attribute1 attribute1;

    public void defineAttribute2() {
        Configuration configuration = beanSession.getRootState();
        String value = processor.getValue(configuration, attribute1);
        configuration.getAttribute2().setValue(value);
    }

    public void defineAttribute3() {
        Configuration configuration = beanSession.getRootState();
        String value = processor.getValue(configuration, attribute1);
        configuration.getAttribute3().setValue(value);
    }

}

但是,问题是在 attribute1 创建期间,我收到以下错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'Attribute2Service': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'attribute1' defined in class path resource [Attribute1Service.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [String]: Factory method 'attribute1' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.beanSession': Scope 'session' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:324) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1378) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
...

我不想从 Attribute2Service 上的 beanSession 访问 attribute1 信息,因为这会在信息提供者和消费者之间造成硬耦合.

最佳答案

异常说明了一切 - 您的 attribute1 bean 是在应用程序初始化期间创建的(通过 session 作用域 bean),但没有与请求绑定(bind)的线程。您还应该代理您的 attribute1 bean,因为您要将其注入(inject)到单例(属性 2 服务。)

关于java - 如何配置 Bean 原型(prototype)范围提供程序以在 Bean 创建时使用 session 信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57699534/

相关文章:

html - 我可以禁用为复选框标记 : in Spring MVC? 生成隐藏字段吗

java - 带有 Spring Boot : static resource (i. e 的 Spring Security 4。 css, js, img) 404问题

spring - 从 Gradle Autowiring BuildProperties bean - NoSuchBeanDefinitionException

java - 最接近Java中网页重新加载的等价物

java - Kotlin 将字符串数组映射并缩减为 Map<String, Any!>

java - 随机选择子节点后如何获取 Firebase 实时数据库中子节点的 key

java - 如何将对象添加到 vector 中?

java - Spring Security - 显示特定于登录用户的内容

spring-boot - cache.period 和 cache.cachecontrol.max-age 有什么区别?

java - spring data批量保存时如何判断违规实体