使用不同控制器中的对象

时间:2016-06-04 05:33:07

标签: java spring spring-mvc global-variables

这可能是一个愚蠢的问题,但我只是感到困惑。 我有一个控制器,在那里创建一个对象&我想在其他几个控制器中使用该对象。
目前我的对象是一个类变量,当发出其他用户请求或会话请求时会被覆盖,我确定它不好用。会产生一些会话问题。 我们假设以下是我的情景:

    @Controller
    public class DemoController
    {
        /* CURRENTLY THIS IS WHAT I'M DOING BUT I DON'T WANT THIS VARIABLE GLOBAL*/
        private MyCommonObject myCommonObject = new MyCommonObject();


        @RequestMapping(value="/demo-one", method=RequestMethod.POST)
        public ModelAndView postControllerOne(@ModelAttribute SearchForm searchForm,
                ModelMap modelMap)
        {
            //I want to use this object in all other controllers too
            myCommonObject = someMethodToGetApiResult(searchForm);

            SomeOtherObject someOtherObject = getSomeObject(myCommonObject);
            modelMap.addAttribute("someOtherObject",someOtherObject);

            return new ModelAndView("/firstJSP");
        }

        @RequestMapping(value="/demo-two", method=RequestMethod.POST)
        public ModelAndView postControllerTwo(@ModelAttribute SomeForm someForm,
                ModelMap modelMap)
        {
            // Used the class variable here
            SomeOtherObject someOtherObject = getSomeObject(myCommonObject,someForm);

            modelMap.addAttribute("someOtherObject",someOtherObject);
            return new ModelAndView("/secondJSP");
        }

        @RequestMapping(value="/demo-three", method=RequestMethod.POST)
        public ModelAndView postControllerThree(@ModelAttribute SomeForm someForm,
                ModelMap modelMap)
        {
            // Used the class variable here
            SomeOtherObject someOtherObject = getSomeObject(myCommonObject,someForm);

            modelMap.addAttribute("someOtherObject",someOtherObject);
            return new ModelAndView("/thirdJSP");
        }

        @RequestMapping(value="/demo-four", method=RequestMethod.POST)
        public ModelAndView postControllerFour(@ModelAttribute SomeForm someForm,
                ModelMap modelMap)
        {
            // Used the class variable here
            SomeOtherObject someOtherObject = getSomeObject(myCommonObject,someForm);

            modelMap.addAttribute("someOtherObject",someOtherObject);
            return new ModelAndView("/fourthJSP");
        }
    }

谢谢。

3 个答案:

答案 0 :(得分:1)

如果您使用的是集中式服务器,而不是将类声明为@Component和单例,并初始化您的对象一次。

如果您在包含多个服务器的分布式环境中工作,则每个服务器都有自己的实例。在这种情况下,您应该使用将保留该类的外部服务器,并且每个其他服务器将从该服务器接收该值。

答案 1 :(得分:1)

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! TableViewCell tableView.rowHeight = 44.0 UIView.animateWithDuration(0.7, delay: 1.0, options: .CurveEaseOut, animations: { tableView.rowHeight = 88.0 cell.layoutIfNeeded() }, completion: { finished in print("Row heights changed!") }) return cell } 是一个单身人士。所有网络请求只有一个实例

这意味着整个webapp中只有一个DemoController值。点击myCommonObject上次获胜的人,以及/demo-one/demo-two的所有点击都会使用最后一个例子,无论是谁在做。

我假设/demo-four存储状态信息,否则为什么要做你想做的事情。每次任何人点击MyCommonObject时,都会重置此状态对象。不能这样做。简而言之,不要将状态存储在控制器中。

由于您希望每个客户端都有/demo-one个实例,请将其存储在MyCommonObject

答案 2 :(得分:0)

如果你想在几个myCommonObject中使用你的Controllers,并且如果这个对象包含非特定于用户的状态(即范围是整个web应用程序),那么你可以将它定义为您的spring应用程序上下文中的bean(XML或Java配置以及范围单例)并使用它所需的任何Controller