guice servlet提供了几个比较有用的web scope,类似与传统servlet 的session,request这些提供的范围等。
guice servlet 提供的web scope 如下:
1.@RequestScoped
2.@SessionScoped
3.@RequestParameters
1.@RequestScoped
例子如下:
1 package com.ming.user.action; 2 3 import com.google.inject.servlet.RequestScoped; 4 5 /** 6 * 类似我们请求的时候将值保存在此访问之中 7 * @author mingge 8 * 9 */ 10 @RequestScoped 11 public class RequstScope { 12 13 14 }
1 package com.ming.user.action; 2 3 import com.google.inject.Inject; 4 import com.google.inject.Provider; 5 6 public class RequstScopeTest { 7 8 @Inject 9 private Provider<RequstScope> requst=null; 10 11 12 RequstScope a=requst.get();//通过此种方法去取此范围的值 13 14 }
2.@SessionScoped
例子如下:
1 package com.ming.user.action; 2 3 import com.google.inject.servlet.SessionScoped; 4 5 @SessionScoped 6 public class SessionScope { 7 8 private int count=0; 9 10 public int getCount() { 11 return count; 12 } 13 14 public void setCount(int count) { 15 this.count = count; 16 } 17 18 19 }
1 package com.ming.user.action; 2 3 import com.google.inject.Inject; 4 import com.google.inject.Provider; 5 6 public class SessionScopeTest { 7 8 @Inject 9 private Provider<SessionScope> session=null; 10 11 12 SessionScope a=session.get();//通过此种方法去取此范围的值 13 14 void test(){ 15 a.setCount(2); 16 } 17 }
3.@RequestParameters
例子如下:
1 @Inject 2 @RequestParameters 3 private Provider<Map<String, String[]>> reqParamMapProvider;
guice servlet 还是比较好用,如果你选择用servlet开发的时候建议用它了。