Kava Pava Gava Tava Nava Zava Java

          everything about Java
          隨筆 - 15, 文章 - 0, 評論 - 1, 引用 - 0
          數據加載中……

          2011年11月21日

          SpringSource Tool Suite 和 Maven,剛剛安裝后的一個錯誤

          剛剛安裝了SpringSource Tool Suite,想試一下 Roo,結果剛建立的一個空項目就有錯。打開“Markers”視圖看錯誤信息,發現自己home directory里面的.m2目錄,就是本地repository里面缺少一下庫。上網搜索了一下,Maven是會從中央repository自動下載需要的庫的。至于錯誤,一些人說是和偉大的墻有關。一些建議是用Nexus搭建本地私服。

          抱著再試一試的想法,用命令行到項目路徑下,試著執行了 mvn test,Maven會自動下載需要的庫。一遍不成功可以重新試幾次。總之,當全部庫都自動下載了之后,clean項目,重建,問題解決。


          posted @ 2011-11-21 18:59 bing 閱讀(573) | 評論 (0)編輯 收藏

          2010年3月16日

          GUICE 與 Servlet 以及 Vaadin 的集成

          部分(1):GUICE 與 Servlet 集成

          http://code.google.com/p/google-guice/wiki/Servlets

          Guice 提供了與 Servlet 的集成,可以完全替代 web.xml,使用類型安全,Java程序員所習慣的方式對 servlet 和 filter 進行配置。

            <filter>
              
          <filter-name>guiceFilter</filter-name>
              
          <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
            
          </filter>

            
          <filter-mapping>
              
          <filter-name>guiceFilter</filter-name>
              
          <url-pattern>/*</url-pattern>
            
          </filter-mapping>

          首先,下載最新版本的 guice-servlet jar 包,將其與 guice 的包共同放在 classpath 路徑下。然后將 GuiceFilter 放在 web.xml 的起始位置,
          這樣,對于任何路徑,guiceFilter 都會被使用,得到控制。

          下面,要對 Guice 進行配置,方法是:   Guice.createInjector(newServletModule());

          這句話可以在任何時候被調用,但推薦的做法是在一個 ServletContextListener 中被調用,這個 Listener 在 Web 應用啟動時被調用,在任何請求到來之前。做法如下,首先擴展 Google 提供的基類:

          public class MyGuiceServletConfig extends GuiceServletContextListener {

            @Override
            
          protected Injector getInjector() {
              
          return Guice.createInjector(new ServletModule());
            }
          }

          然后在 web.xml 中進行注冊

          <listener>
            
          <listener-class>com.example.MyGuiceServletConfig</listener-class>
          </listener>

          配置 servlet 是在 ServletModule 中進行的。以下是一個匿名類的例子:

             Guice.createInjector(new ServletModule() {

               @Override
               
          protected void configureServlets() {
                 serve(
          "*.html").with(MyServlet.class)
               }
             }

          可以用 web.xml風格的路徑限制法 serve("/my/*").with(MyServlet.class)。

          也可以類似地配置 filter:
          filter("/*").through(MyFilter.class);

          注意:每個 servlet 和 filter 都必須是 @Singleton,必須在標注或者 module 里面指明,所有不是 Singleton 的 scope 都是錯誤。這與 Servlet 的規范一致。

          安裝了 ServletModule 模塊后,任何 Guice 注入的實例都可以被注入下面的對象:

          @RequestScoped
          class SomeNonServletPojo {

            @Inject
            
          public SomeNonServletPojo(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
              
            }

          }

          此外,http請求的參數可以如此注入: @Inject @RequestParameters Map<String, String[]> params;

          分配順序:

          filter 和 servlet 將會按照在 ServletModule 里面出現的順序分配。

          此外,可以一次匹配多個路徑: serve("*.html", "/my/*").with(MyServlet.class);

          正則文法匹配:serveRegex("(.)*ajax(.)*").with(MyAjaxServlet.class)

          指定初始化參數:

            Map<String, String> params = new HashMap<String, String>();
            params.put(
          "coffee""Espresso");
            params.put(
          "site""google.com");

            
                serve(
          "/*").with(MyServlet.class, params)

          這些參數可以用 getInitParams 得到。

          其他高級特性省略不再討論。

          部分(2):GUICE 與 Vaadin 集成

          http://vaadin.com/wiki/-/wiki/Main/Integrating Vaadin with Guice 2.0

          首先,下載 guice-2.0.jar, guice-servlet-2.0.jar, aopalliance.jar,將這些包拷貝到 WebContent/WEB-INF/lib/ 目錄。

          import javax.servlet.ServletException;
          import javax.servlet.http.HttpServletRequest;

          import com.google.inject.Inject;
          import com.google.inject.Provider;
          import com.google.inject.Singleton;
          import com.vaadin.Application;
          import com.vaadin.terminal.gwt.server.AbstractApplicationServlet;

          @Singleton
          public class GuiceApplicationServlet extends AbstractApplicationServlet {

              
          protected final Provider<Application> applicationProvider;

              @Inject
              
          public GuiceApplicationServlet(Provider<Application> applicationProvider) {
                  
          this.applicationProvider = applicationProvider;
              }

              @Override
              
          protected Class getApplicationClass() throws ClassNotFoundException {
                  
          return Application.class;
              }

              @Override
              
          protected Application getNewApplication(HttpServletRequest request) throws ServletException {
                  
          return applicationProvider.get();
              }

          }
           
          上面這個class 可以直接拷貝到項目中不需要改變。Provider 由 Guice 注入,為每個用戶創建一個 Application 實例。

          下面是一個簡單的 Application 的例子。其中參數 text 是被注入的。

          #!java
          import com.google.inject.Inject;
          import com.google.inject.name.Named;
          import com.vaadin.Application;
          import com.vaadin.ui.Label;
          import com.vaadin.ui.Window;

          public class MyApplication extends Application {
              
              @Inject @Named(
          "welcome"protected String text;
              
              @Override
              
          public void init() {
                  Window window 
          = new Window();
                  window.addComponent(
          new Label(text));
                  
                  setMainWindow(window);
              }

          }

          初始化,需要寫一個 Guice 的 GuiceServletContextListener來配置 ServletModule,配置 Servlet,Application,以及其他 Guice 管理的實例,比如上面例子用到的 text。

          #!java
          import com.google.inject.Guice;
          import com.google.inject.Injector;
          import com.google.inject.name.Names;
          import com.google.inject.servlet.GuiceServletContextListener;
          import com.google.inject.servlet.ServletModule;
          import com.google.inject.servlet.ServletScopes;
          import com.vaadin.Application;

          public class MyServletConfig extends GuiceServletContextListener {

              @Override
              
          protected Injector getInjector() {

                  ServletModule module 
          = new ServletModule() {
                      @Override
                      
          protected void configureServlets() {
                          serve(
          "/*").with(GuiceApplicationServlet.class);

                          bind(Application.
          class).to(MyApplication.class).in(ServletScopes.SESSION);
                          bindConstant().annotatedWith(Names.named(
          "welcome")).to("This is my first Vaadin/Guice Application");
                      }
                  };

                  Injector injector 
          = Guice.createInjector(module);

                  
          return injector;
              }
          }

          web.xml 需要如此配置,來啟動 Guice Filter 和上面的那個配置模塊 (Listener)

          <web-app>

            

            
          <filter>
               
          <filter-name>guiceFilter</filter-name>
               
          <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
            
          </filter>

            
          <filter-mapping>
               
          <filter-name>guiceFilter</filter-name>
               
          <url-pattern>/*</url-pattern>
            
          </filter-mapping>
              
            
          <listener>
               
          <listener-class>de.timedout.vaadin.guice.MyServletConfig</listener-class>
            
          </listener>
            
          </web-app>












































































































































































































































































          posted @ 2010-03-16 15:05 bing 閱讀(2191) | 評論 (0)編輯 收藏

          2010年1月27日

          Java Type Conversion Framework

               摘要: Study several java type conversion frameworks, especially Morph, EZMorph and Dozer  閱讀全文

          posted @ 2010-01-27 14:49 bing 閱讀(451) | 評論 (0)編輯 收藏

          2010年1月11日

          Hibernate 的 Proxy 陷阱

               摘要: Hibernate 是個很透明的框架。Hibernate 使用 Proxy 做 lazy loading。。。。看起來也很透明。。。。但還是有一堵墻在那里,小心不要一頭撞在上面。本文討論關于 Hibernate 使用 Proxy 要注意的兩個陷阱。  閱讀全文

          posted @ 2010-01-11 12:15 bing 閱讀(3572) | 評論 (0)編輯 收藏

          2010年1月6日

          關于 Vaadin / Hibernate 應用開發的結構的考慮

               摘要: 關于 vaadin 應用的構架的思考。特別是結合 hibernate 以及 hibernate validate  閱讀全文

          posted @ 2010-01-06 11:41 bing 閱讀(752) | 評論 (0)編輯 收藏

          2009年12月30日

          Google Guice 用戶手冊之 閱讀筆記

               摘要: Google Guice 是一個類似 Spring 那樣的條件注入 (IoC)庫。與Spring 不同的是,Guice輕巧,不用XML。Guice 的配置模塊就是 Java 類,因此可以得到編譯器的支持和檢查。本文是閱讀 Guice 手冊后的總結。  閱讀全文

          posted @ 2009-12-30 18:55 bing 閱讀(2201) | 評論 (1)編輯 收藏

          Exploring Vaadin (6) - summary of several classes

               摘要: Something about:
          Abstract Class PropertyFormatter
          Abstract Class AbstractSelect
          Interface FormFieldFactory
          Abstract Class AbstractField  閱讀全文

          posted @ 2009-12-30 14:05 bing 閱讀(646) | 評論 (0)編輯 收藏

          2009年12月23日

          Exploring Vaadin (5) 閱讀 com.vaadin.data.util.BeanItem 源代碼

               摘要: 如題,閱讀 com.vaadin.data.util.BeanItem 源代碼的筆記  閱讀全文

          posted @ 2009-12-23 18:10 bing 閱讀(360) | 評論 (0)編輯 收藏

          Exploring Vaadin (4) 閱讀 com.vaadin.data.util.MethodProperty 源代碼

               摘要: 閱讀 com.vaadin.data.util.MethodProperty 源代碼的筆記。閱讀 MethodProperty 的目的是因為這個類是 Property,同時處理 Bean 的屬性。本以為可能會牽扯到 Vaadin 處理類型轉換的地方,可以了解一下 Vaadin 是如何操作的。結果發現錯了。應該看看 com.vaadin.data.util.PropertyFormatter ,才是用來進行類型轉換的。  閱讀全文

          posted @ 2009-12-23 17:35 bing 閱讀(408) | 評論 (0)編輯 收藏

          Exploring Vaadin (3) 閱讀 com.vaadin.ui.Form 源代碼

               摘要: 閱讀 com.vaadin.ui.Form 源代碼的筆記  閱讀全文

          posted @ 2009-12-23 16:10 bing 閱讀(740) | 評論 (0)編輯 收藏

          主站蜘蛛池模板: 刚察县| 高邑县| 八宿县| 吉林省| 华宁县| 普兰店市| 高安市| 习水县| 西安市| 都匀市| 色达县| 武冈市| 清丰县| 长垣县| 灵璧县| 松阳县| 宁城县| 中西区| 昌黎县| 兴海县| 美姑县| 满洲里市| 长治县| 永仁县| 通州市| 彩票| 原阳县| 玉树县| 桐城市| 西畴县| 长岭县| 五莲县| 平谷区| 桦川县| 黄浦区| 阜南县| 杨浦区| 尚义县| 故城县| 宁河县| 澳门|