Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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
Spring Bean中的构造函数_Spring_Spring Mvc_Javabeans - Fatal编程技术网

Spring Bean中的构造函数

Spring Bean中的构造函数,spring,spring-mvc,javabeans,Spring,Spring Mvc,Javabeans,我有一个关于创建新bean的小问题。基本上根据请求,我得到一些参数,这些参数需要传递给bean。下面我将为每个请求实例化ControllerService。相反,我希望它是scope=protype的bean。这样每次请求我都能得到一个新的对象。 但是如何设置通过bean中的构造函数发送的2个属性(kpiName、kpiInput) @Autowired @Qualifier("serviceManager") Cleanser serviceManager; @RequestMappin

我有一个关于创建新bean的小问题。基本上根据请求,我得到一些参数,这些参数需要传递给bean。下面我将为每个请求实例化ControllerService。相反,我希望它是scope=protype的bean。这样每次请求我都能得到一个新的对象。 但是如何设置通过bean中的构造函数发送的2个属性(kpiName、kpiInput)

@Autowired
@Qualifier("serviceManager")
Cleanser serviceManager;  

@RequestMapping(method = RequestMethod.POST)
public @ResponseBody
String getKPIResult(@RequestParam("kpiName") String kpiName,
            @RequestParam("kpiInput") String kpiInput) {    

    return serviceManager.checkAndExecute(new ControllerService(kpiName, kpiInput));
}

在这样的情况下,如果你与春天背道而驰,我建议你也许在做一些不被认为是最佳实践的事情。如果没有更多的背景,就很难看出这一点

SpringSocial使用
请求
范围bean来体现特定用户的存储库。我现在知道为什么了,因为这是一种非常低效的做事方式,更不容易理解

<bean id="connectionRepository" factory-method="createConnectionRepository"
    factory-bean="usersConnectionRepository" scope="request">
    <constructor-arg
        value="#{T(org.springframework.security.core.context.SecurityContextHolder).getContext().getAuthentication().getPrincipal()}" />
    <aop:scoped-proxy proxy-target-class="false" />
</bean>

这里可以看到使用
factorybean
factorymethod
在需要类实例时声明要调用的类/方法。构造函数参数是使用SpEL传递的。我不太确定SpringMVC如何响应web请求,但我相当确定您可以使用SpringIntegration传递消息,并使用SpEL获取消息的头/有效负载形式,以传递给构造函数


不过,我还是要再次质疑您的设计模式——一种更常见的SOA习惯用法是在启动时创建服务,并从那时起使它们尽可能无状态,而不是为每个请求创建具有特定状态的实例。祝你好运

不要。SpringMVC中的控制器主要是从旧的JavaServlet派生的,根据规范,它应该是无状态的

事实上,控制器对象是在处理程序映射框架内硬缓存的,而不是在每个请求上从bean上下文中获取。将scope设置为“prototype”实际上不会起任何作用,因为处理程序(控制器)实际上只获得一次