西元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
西元2006年04月09日
@Resource 資源取得 JNDI 的相關資源
在 TheServerside 看到這篇文章
http://www.theserverside.com/news/thread.tss?thread_id=39828
我利用簡單的資料庫連結做說明, Java EE 5.0 如何使用 @Resource 來取得相關資源
1. JDBC direct connect the Database
這是簡單的範例, 我們可以直接利用 DriverManager 去取得 connection, 這種寫法是由 Application 直接驅動連結, 沒有透過 Container, 我認為不是很好的方法 !
// JDBC Driver 的主要 class 名稱
Class.forName("com.mysql.jdbc.Driver");
// 每個 Driver都有標準的連結位置撰寫方式
String url = "jdbc:mysql://host:port/database";
// 將 URL 與登入帳號密碼連結
Connection con = DriverManager.getConnection( url ,"username","password");
//....
con.close();
2. DataSource Connection
透過 JNDI 以及 Context lookup 取得 DataSource, 再用 DataSource.getConnection() 取得 Connection.
Context initCtx = new InitialContext();
DataSource ds = (DataSource) initCtx.lookup("jdbc/mysql");
Connection conn = ds.getConnection();
//... use this connection to access the database ...
conn.close();
3. @Resource mapped global JNDI
當開發工程師知道, application server 之中, 已經具備了 jdbc/mysql 這個 JNDI Name, 這樣算是取代了 JNDI context lookup 的方法.
@Resource(name="jdbc/mysql")
private DataSource ds;
Connection conn = ds.getConnection();
//... use this connection to access the database ...
conn.close();
4. @Resource dependency injection
當開發工程師不知道, application server 之中, 到底具備了哪些資源可以利用, 可以利用 Java EE 5.0 Annotation Resource Injection 的特性, 讓組裝部署人員做相關的設定.
@Resource
private DataSource ds;
Connection conn = ds.getConnection();
//... use this connection to access the database ...
conn.close();
web.xml
sun-web.xml
所以, 組裝人員可以在 web.xml , sun-web.xml ( glassfish ) 設定相關的 injection 環境及對應到 application server 的 global 的 jndi 設定, 這樣, 可以輕易地完成 Injection 的實作.
17:20 發表於 *Overview | 永久網址 | 留言 (0) | Email this
西元2006年03月30日
Java EE 5.0 Resource Injections
Java EE 5.0 之中有定義一些可以使用 injection 的 classes
*Servlet
- servlets
- servlet filters
- event listeners
* JSP
- tag handlers
- tag library event listeners
* JSF
- scoped managed beans
* JAX-WS
- service endpoints
- handlers
* EJB
- beans
- interceptors
* Java EE platform
- main class ( static )
- login callback handler
之前沒有看到這個規定, 難怪我嘗試著使用 Struts Action 透過 @Resource, @EJB 取得相關的 Resources, 都是回傳 NULL, 看來 action 之中目前僅能透過 JNDI context.lookup 的方式取得 ejb3 及其他的環境變數.
另外, 曾經當面和 saijones 提到 Deployment Descriptor 與 Annotations 的互補性, 在 spec 中也有定義, 當你在撰寫程式的時候, 使用 @Annotations 可以簡化開發的流程, 基本上, 你就少寫了一個 XML 檔案, 更可以直接設定預設值. 但是當部署人員或應用系統組合人員, 發現你的一些數值需要重新設定, 是不必重新打開原始碼, 修改 @annotations 的部分, 僅僅需要丟一個 xml 檔案放在 /META-INF/ 之中.
不過, 這種方法, 會不會造成 debug 更為艱鉅呢, 如果工程師被告知有 bugs, 可能永遠不知道是 deployer 將初始值修改成另外一個數值 !! 看來, 簡化開發是否能夠簡化 Debug !! 將會是新的 UnitTest 設計考驗 !!
另外, 使用 java EE platform 之中的 annotation 大多是 @Retention 為 RUNTIME 的, 所以並非僅用於 compile-time, annotation 的資料是會封裝在 classes 之中.. 所以使用上要格外留意 :)
21:05 發表於 *Overview | 永久網址 | 留言 (0) | Email this
西元2006年02月16日
為何要使用 Java EE5 之官方說辭
java.sun 有一篇文章 Why Move to Java EE ( http://java.sun.com/javaee/whymove.html )
這篇文章說明了使用 Java EE 5 可以達到
- 簡化開發 : 最重要的改善應該是利用 annotation 取代 D.D. ( Deployment Descriptor ) 吧 !
- EJB 的改善 : 採用 POJOs 的方式以及加強 Persistence 的技術. 讓 EJB3 開發上更為容易 !
- 加強 WebServices 的功能 : 除了開發上的簡化之外更整合了許多最新的技術
- JSF, JSTL, AJAX 等 Web 技術
我從撰寫 J2EE 到去年更名的 JavaEE 已經是第五個年頭.
在其間, 由 Servlet 轉到 JSP 再利用 struts, ejb, hibernate 等等的技術與整合方式,
現在覺得無論是什麼架構, 一定要和 IDE 整合的更平順, 讓開發上更為容易 !
例如, Hibernate 透過 Middlegen 可以產生 hbm.xml 及 PersistenceObject, 這樣子的開發模式,
比起使用 IDE 製作 DAO + ValueObject 來得輕鬆與容易.
我相信未來 EJB3.0 的撰寫與測試, 結合 IDE 會更方便 :)
但是 Java Enterprise Edition 包含了 Programming 技術之外, 還有 Security, Transaction, Management 等底層.
我覺得現在所有的 Application Server 都以 JMX 做為核心來製作. 這是一件好事情, 另外 Secuirty 是否能夠標準化, 我當然期望能夠標準化 ~ Authentication / Authorization for J2EE 的標準應該還有一段路要處理 !
JSF + EJB3 這種開發模式是否能夠成功呢 ?
說真的, JSF 成為 Java EE 5 Application Server 的標準之一, 我卻看不到他的未來.
08:47 發表於 *Overview | 永久網址 | 留言 (0) | Email this

