Guice Basic use, Guice integrated Guice-servlet,web Development (V)

Source: Internet
Author: User
Tags ming

Introduced

The Guice servlet provides a complete pattern for using Web applications and servlet Containers. The Guice's servlet extension allows you to completely retire Web. XML from your servlet app and has the benefit of type safety (type-safe). Configure your servlet and filter components in a java-compliant manner.

Not only is it possible to use a better API to configure your Web application, but also to include dependency injection in your Web application components, meaning that your servlet and filter benefit from the Following:

    • Construction method Injection (Constructor Injection)
    • type-safe, more custom-configured (type-safe, idiomatic Configuration)
    • Modularity (packaging and publishing of personalized Guice servlet class libraries
    • Guice Programming for Facets

Will benefit from the standard servlet life Cycle.

The Guice servlet simplifies the development of traditional Servlets.

Specific as Follows:

1   <Filter>2     <Filter-name>Guicefilter</Filter-name>3     <Filter-class>Com.google.inject.servlet.GuiceFilter</Filter-class><!--this is the guice servlet filter--4   </Filter>5   <filter-mapping>6     <Filter-name>Guicefilter</Filter-name>7     <Url-pattern>/*</Url-pattern>8   </filter-mapping>9   <Listener>Ten     <Listener-class>Com.ming.core.web.listener.GoogleGuiceServletContextListener</Listener-class><!--this is for registering the module and the servlet-- one   </Listener>

1  packagecom.ming.core.web.listener;2 3 Importcom.google.inject.Guice;4 Importcom.google.inject.Injector;5 Importcom.google.inject.servlet.GuiceServletContextListener;6 Importcom.ming.user.UserModule;7 8  public classGoogleguiceservletcontextlistenerextendsGuiceservletcontextlistener {9 Ten @Override one     protectedInjector getinjector () { a          -         returnGuice.createinjector (NewUsermodule ()); -         //If you bind more than one module, you need to do it as follows the         //return guice.createinjector (new usermodule (), new Usermodule ()); -     } -  -}

1  packagecom.ming.user;2 3 Importcom.google.inject.AbstractModule;4 Importcom.google.inject.servlet.ServletModule;5 Importcom.ming.core.web.filter.EncodeFilter;6 Importcom.ming.user.action.UserServlet;7  public classUsermoduleextendsAbstractmodule {8 9 Ten @Override one     protected voidConfigure () { aInstallNewservletmodule () { - @Override -             protected voidconfigureservlets () { the                  -                 //Configure the servlet you access -                 //serve ("/userservlet"). with (userservlet.class); -                  +                 //If you have multiple access addresses for a servlet, you can configure it -Serve ("/userservlet", "/usercontroller"). with (userservlet.class); +                  a                 //If you want your URL to support a regular match, you can write it like this at                 //Serveregex ("^user"). with (userservlet.class); -                  -                 //similarly, The filter configuration is as follows -                 //filter ("/encodefilter"). through (encodefilter.class); -                  -                 //Multiple Addresses in                 //filter ("/encodefilter", "/haha"). through (encodefilter.class); -                  to                 //Support Regular +                 //Filterregex ("^aaa"). through (encodefilter.class); -                  the             } *         }); $         Panax Notoginseng     } -  the      +}
1  packagecom.ming.user.action;2 3 Importjava.io.IOException;4 Importjava.util.ArrayList;5 Importjava.util.List;6 7 Importjavax.servlet.ServletException;8 Importjavax.servlet.http.HttpServlet;9 Importjavax.servlet.http.HttpServletRequest;Ten Importjavax.servlet.http.HttpServletResponse; one  a Importcom.google.inject.Inject; - Importcom.google.inject.Singleton; - Importcom.ming.user.entity.User; the Importcom.ming.user.service.UserService; -  - /** -  *  +  * @authorMingge -  * +  */ a @Singleton at  public classUserservletextendsHttpServlet { -      -      -     Private Static Final LongSerialversionuid = 1L; -  - @Inject in     PrivateUserService userservice; -  to     protected voiddoget (httpservletrequest request, httpservletresponse response) +             throwsservletexception, IOException { -  theString account = Request.getparameter (' account '); *         intUserID = integer.valueof (request.getparameter ("userid")); $User U =NewUser ();Panax Notoginseng U.setaccount (account); - u.setuser_id (userId); theList<user> ulist=NewArraylist<>(); + Ulist.add (u); a         Try { the Userservice.add (u); +System.out.println ("ok"); -}Catch(Exception E) { $System.out.println ("error"); $ e.printstacktrace (); -             //Note: After invoking the service layer method out of exception, continue to throw the exception so that the thrown exception can be caught at transactionfilter, and then the transaction rollback operation is performed -             Throw Newruntimeexception (e); the         } - Wuyi     } the  -     protected voiddoPost (httpservletrequest request, httpservletresponse response) wu             throwsservletexception, IOException { -  about doget (request, response); $     } -  -}

It looks simple. Specific example Download: http://pan.baidu.com/s/1geMXE1t

Guice Basic use, Guice integrated Guice-servlet,web Development (V)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.