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

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 ?應該是比較可行的方案了.. 

 

 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值