java Spring Controller 初始化方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3236971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 00:56:27  来源:igfitidea点击:

Spring Controller init method

javaspringspring-mvc

提问by onigunn

as far as I understand are spring controllers stateless right? If I set a field to a value, this is revoked by the next call.

据我所知,spring 控制器是无状态的,对吗?如果我将一个字段设置为一个值,它会被下一次调用撤销。

Is there a possibility to add a init-method or something? A method which is called once, when my controller is triggerd? I'm using spring 3.0 and a annotation configuration atm.

有没有可能添加一个init-method之类的?当我的控制器被触发时调用一次的方法?我正在使用 spring 3.0 和注释配置 atm。

回答by Nils Schmidt

Spring Controllers should be handled stateless by default, that is correct. Nevertheless this does not mean your value will be revoked by the next call. From the programmers perspective it is not decidable if you end up with same instance of your controller or a different instance. It is further more not assured that no one else used the controller (and therefore changed its state in the meantime). This is why it is not advisable to save any state at all in fields of your controller. Maybe you should reconsider the need for a field in your controller.

默认情况下,Spring 控制器应该是无状态处理的,这是正确的。尽管如此,这并不意味着您的价值将被下一次调用撤销。从程序员的角度来看,最终是使用相同的控制器实例还是不同的实例是无法确定的。更不能保证没有其他人使用控制器(因此在此期间改变了它的状态)。这就是为什么不建议在控制器的字段中保存任何状态的原因。也许您应该重新考虑控制器中是否需要一个字段。

In fact there is a init method for spring beans. You can simply annotate a public void method on your controller with @PostConstruct. This method is executed after dependencies have been injected. Thus, this method is invoked exactly ones after the controller instance is created.

事实上,spring bean 有一个 init 方法。您可以使用@PostConstruct简单地在控制器上注释公共无效方法。此方法在依赖项被注入后执行。因此,在创建控制器实例后,将完全调用此方法。

As far as I understand your question you look for some method that is executed before every call to a methodof your controller. In that case you could simply at a call to your "init"-method in the beginning of each of your controller methods. If you dont want to do this explicit in your code AOP provide you an alternative.

据我了解您的问题,您会寻找在每次调用控制器方法之前执行的方法。在这种情况下,您可以简单地在每个控制器方法的开头调用“init”方法。如果您不想在您的代码中显式执行此操作,AOP 会为您提供替代方案。

回答by Stephen C

As far as I understand are spring controllers stateless right? If I set a field to a value, this is revoked by the next call.

据我所知,spring 控制器是无状态的,对吗?如果我将一个字段设置为一个值,它会被下一次调用撤销。

I believe that is incorrect: Spring controllers can be stateful. You should be very careful though because a Controller is expected to be re-entrant and thread-safe and to support multiple threads simultaneously executing multiple requests.

我认为这是不正确的:Spring 控制器可以是有状态的。不过你应该非常小心,因为控制器应该是可重入的和线程安全的,并且支持多个线程同时执行多个请求。

It is probably safe to say that it best practicefor a controller to be designed to be effectively stateless; i.e. no state that changes while the controller is "live".

可以肯定地说,将控制器设计为有效无状态的最佳实践;即当控制器处于“活动”状态时没有改变的状态。

Is there a possibility to add a init-method or something?

有没有可能添加一个init-method之类的?

It is not entirely clear what you mean. However:

你的意思并不完全清楚。然而:

  • The controller's handleRequestis called to start a request.
  • If you declare any bean (e.g. a controller bean) as ApplicationContextAwareit will be called back to inform it of the ApplicationContext.
  • If you declare any bean as ServletContextAwareit will be called back to inform it of the ServletContext.
  • If you declare any bean as an InitializingBeanit will be called back when all properties have been set.
  • handleRequest调用控制器以启动请求。
  • 如果您声明任何 bean(例如控制器 bean),ApplicationContextAware它将被回调以通知它 ApplicationContext。
  • 如果您声明任何 bean,ServletContextAware它将被回调以通知它 ServletContext。
  • 如果您将任何 bean 声明为 an InitializingBean,则在设置所有属性后,它将被回调。

And there are doubtless other callbacks and hooks that you could use to trigger some delayed initialization / context setting.

毫无疑问,还有其他回调和钩子可以用来触发一些延迟的初始化/上下文设置。

(I'm not sure how these callbacks / hooks map to annotations ... but I'm sure that they do.)

(我不确定这些回调/挂钩如何映射到注释......但我确定它们确实如此。)