简体   繁体   English

Spring Controller init方法

[英]Spring Controller init method

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. 如果我将字段设置为值,则下次调用将撤消该字段。

Is there a possibility to add a init-method or something? 是否有可能添加init方法或其他东西? A method which is called once, when my controller is triggerd? 当我的控制器被触发时,被调用一次的方法? I'm using spring 3.0 and a annotation configuration atm. 我正在使用spring 3.0和一个注释配置atm。

Spring Controllers should be handled stateless by default, that is correct. 默认情况下,Spring控制器应该处理无状态,这是正确的。 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. 也许您应该重新考虑控制器中对字段的需求。

In fact there is a init method for spring beans. 实际上,spring bean有一个init方法。 You can simply annotate a public void method on your controller with @PostConstruct . 您可以使用@PostConstruct在控制器上简单地注释public void方法。 This method is executed after dependencies have been injected . 在注入依赖项之后执行此方法。 Thus, this method is invoked exactly ones after the controller instance is created. 因此,在创建控制器实例之后,将调用此方法。

As far as I understand your question you look for some method that is executed before every call to a method of your controller. 据我了解你的问题,你找一些在每次调用控制器方法之前执行的方法 In that case you could simply at a call to your "init"-method in the beginning of each of your controller methods . 在这种情况下,您可以在每个控制器方法的开头简单地调用“init”方法 If you dont want to do this explicit in your code AOP provide you an alternative. 如果您不想在代码中明确地执行此操作,AOP会为您提供替代方案。

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. 如果我将字段设置为值,则下次调用将撤消该字段。

I believe that is incorrect: Spring controllers can be stateful . 我认为这是不正确的:Spring控制器可以是有状态的 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. 您应该非常小心,因为Controller应该是可重入且线程安全的,并支持多个线程同时执行多个请求。

It is probably safe to say that it best practice for a controller to be designed to be effectively stateless; 可以肯定地说,将控制器设计为有效无状态是最佳实践 ; ie no state that changes while the controller is "live". 即控制器“活动”时没有状态发生变化。

Is there a possibility to add a init-method or something? 是否有可能添加init方法或其他东西?

It is not entirely clear what you mean. 你的意思并不完全清楚。 However: 然而:

  • The controller's handleRequest is called to start a request. 调用控制器的handleRequest来启动请求。
  • If you declare any bean (eg a controller bean) as ApplicationContextAware it will be called back to inform it of the ApplicationContext. 如果将任何bean(例如控制器bean)声明为ApplicationContextAware ,它将被回调以通知它ApplicationContext。
  • If you declare any bean as ServletContextAware it will be called back to inform it of the ServletContext. 如果您将任何bean声明为ServletContextAware ,它将被回调以通知它ServletContext。
  • If you declare any bean as an InitializingBean it will be called back when all properties have been set. 如果将任何bean声明为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.) (我不确定这些回调/钩子如何映射到注释......但我确信它们会这样做。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM