傳統的資源注入採用JNDI注入方式:html
InitialContext initCtx = new InitialContext(); // Perform JNDI lookup to obtain the resource. catalogDS = (DataSource) initCtx.lookup("java:comp/env/jdbc/catalogDS"); 在JavaEE5時代經過註解的方式注入資源: private @Resource DataSource catalogDS; 但JavaEE5的資源注入是受限的,只有容器受管組件,好比EJB,Servlet,JSP tag hangler等。一個方面考慮的掃描的性能,另外一個 對組件的建立有更好的控制。 在JavaEE6時代又出現了DI和CDI,讓資源注入搞得無處不在。也大大增了開發難度。DI相關Annotation。 @Inject,@Named,@Qualifier,@Scope,@Singleton
http://www.oracle.com/technetwork/articles/javaee/injection-141192.htmljava