西元2006年07月17日
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 的複雜度吧..
難道 大家都要被迫學習 JSF ?!
看來.. 在 Web-tier 利用 InjectionFilter or InjectionInitServlet 先取得相關資源
例如 DataSources, EntityManager 放到 SessionScope 或 ApplicationScope ?
應該是比較可行的方案了..
01:40 發表於 *Overview | 永久網址 | 留言 (0) | Email this


The comments are closed.