Spring Boot为每个用户定义配置Bean

时间:2018-09-03 08:14:02

标签: spring spring-boot

我正在使用Spring Boot。我对弹簧靴子有一些疑问。

但是我有疑问

  

我使用的bean是默认的作用域,即单例。因此,每个应用程序只能有一个实例。

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns="http://javafx.com/javafx"
            xmlns:fx="http://javafx.com/fxml"
            fx:controller="stackoverflow.testscroll.Controller"
            fx:id="content">
            <TitledPane fx:id="titledPane">
                <TableView fx:id="tableView">
                    <columns>
                        <TableColumn/>
                        <TableColumn/>
                        <TableColumn/>
                        <TableColumn/>
                        <TableColumn/>
                        <TableColumn/>
                        <TableColumn/>
                        <TableColumn/>
                    </columns>
                </TableView>
           </TitledPane>
</AnchorPane>

  

现在我使用范围为原型的bean。因此,每个请求将具有每个实例。

@Configuration
public class ...{

     @Bean
     public void method() {}
}

但是

  

我希望每个用户一个实例。所有请求每个用户都使用一个实例。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

您将需要使用原型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