首页 文章

Spring引导为每个用户定义配置bean

提问于
浏览
1

我正在使用Spring启动 . 我对 spring 靴 beans 有一些疑问 .

但我怀疑

我使用的是作为单例的默认范围的bean . 因此,每个应用程序只有一个实例 .

@Configuration
public class ...{

     @Bean
     public void method() {}
}

现在我使用范围是原型的bean . 因此,每个请求都会有每个实例 .

@Configuration
public class ...{

     @Bean 
     @Scope("prototype")
     public void method() {}
}

我想要每个用户单个实例..?所有请求都使用每个用户的单个实例

2 回答

  • 0
    @Configuration
    class Abc {
     @Bean
     @Scope("session")
     public YourBean getYourBean() {
     return new YourBean();
    }
    }
    
  • 2

    您将需要使用原型bean定义一个带有属性的单例bean:(xml示例)

    enter image description here

    使用@bean定义:

    @Component
    @Scope("singleton")
    public class SingletonBean {
    
       // ..
    
         @Autowired
         private PrototypeBean prototypeBean;
       //..
    
    }
    
    
    
    @Component
    @Scope("prototype")
    public class PrototypeBean {
     //.......
    }
    

    示例:https://www.baeldung.com/spring-inject-prototype-bean-into-singleton

相关问题