Struts Action get Resource by Injection铪铪

来源:互联网 发布:网盘搜索cms系统 编辑:程序博客网 时间:2024/05/17 03:07

http://www.codecomments.com/archive419-2006-6-954408.html

Craig McClanahan:

A couple of possibilities come to mind ...

* Inject the resource into a container-instantiated object (like a servlet), and modify your framework to copy values in when it creates, say, an Action instance in the Struts case. BIG PROBLEM: thread safety ... the servlet instance is shared across all threads, so the variable would get stomped on if there was more than one simultaneous request.

* Modify your framework to scan for the injection annotations itself when it creates new object instances, and perform the same kinds of injection that the container can do.

* Use a dependency injection framework that knows how to initialize properties from JNDI lookups, and configure it to look up the corresponding resources.

* Use the managed beans portion of JSF (even if you are not using the rest of it) as a dependency injection framework, and let the container go ahead and do the injection for you. (If you're using an additional framework, it would need to be modified to use managed beans to create object instances, instead of doing
Class.newInstance() things itself).

Craig



換句話說, Application Server 決定了是否可以 inject 相關的 Resource 到你的系統之中,如果該 Application Server 並沒有支援 Struts Action 或其他 Framework,就等於即使你的系統也在 Java EE 5.0 的環境中執行 ~~也得使用 JNDI lookup 去取得相關的 Resource,

假設仍然使用習慣的 Framework, 就不會修改相關原始碼去掃描 Annotations.應該也不會另外找個 DI Framework 來增加 Framework 的複雜度吧.. 難道 大家都得因為 ManagedBean support Injection 要被迫學習 JSF ?!

看來.. 在 Web-tier 利用 InjectionFilter or InjectionInitServlet 先取得相關資源,例如 DataSources, EntityManager 放到 SessionScope 或 ApplicationScope ?應該是比較可行的方案了.. 

 

 




原创粉丝点击