Northpark博客

Today does not walk, will have to run tomorrow.


  • 首页

  • 关于

  • 旅行

  • 微世界

  • 音乐

  • 读书

  • 归档

guice整合guice-servlet,web scope注解 <6>

Posted on 2018-06-10 | In guice

guice整合guice-servlet,web scope注解 <6>

guice servlet提供了几个比较有用的web scope,类似与传统servlet
的session,request这些提供的范围等。

guice servlet 提供的web scope 如下:

  • @RequestScoped
  • @SessionScoped
  • @RequestParameters

1.@RequestScoped

例子如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package cn.northpark.action;

import com.google.inject.servlet.RequestScoped;

/**
* 类似我们请求的时候将值保存在此访问之中
* @author bruce
*
*/
@RequestScoped
public class RequstScope {


}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package cn.northpark.action;

import com.google.inject.Inject;
import com.google.inject.Provider;

public class RequstScopeTest {

@Inject
private Provider<RequstScope> requst=null;


RequstScope a=requst.get();//通过此种方法去取此范围的值

}

2.@SessionScoped

例子如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package cn.northpark.action;

import com.google.inject.servlet.SessionScoped;

@SessionScoped
public class SessionScope {

private int count=0;

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}


}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package cn.northpark.action;

import com.google.inject.Inject;
import com.google.inject.Provider;

public class SessionScopeTest {

@Inject
private Provider<SessionScope> session=null;


SessionScope a=session.get();//通过此种方法去取此范围的值

void test(){
a.setCount(2);
}
}

3.@RequestParameters

例子如下:

1
2
3
@Inject
@RequestParameters
private Provider<Map<String, String[]>> reqParamMapProvider;

guice servlet 还是比较好用,如果你选择用servlet开发的时候建议用它了。

guice整合guice-servlet,web开发 <5>

Posted on 2018-06-05 | In guice

guice整合guice-servlet,web开发 <5>

介绍

Guice Servlet 为使用web应用程序和Servlet容器提供了一个完整的模式。. Guice’s servlet
扩展允许从你的servlet应用中完全淘汰web.xml,并且具有类型安全(type-safe)的优势。
符合Java方式的配置你的servlet和filter组件。

这不仅在于可以使用更好的API来配置你的web应用程序,而且也在于在web应用组件中加入依赖注入,意味着你的servlet和filter得益于以下几个方面:

  • 构造方法注入(Constructor injection)
  • 类型安全,更符合习惯的配置方式(Type-safe, idiomatic configuration)
  • 模块化(打包和发布个性化的Guice Servlet类库
  • Guice 面向切面编程
  • 在标准的servlet生命周期都将受益。

==guice servlet简化了传统servlet的开发。==

具体如下:

1
2
3
4
5
6
7
8
9
10
11
<filter>
<filter-name>guiceFilter</filter-name>
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class><!--这个是guice servlet的过滤器-->
</filter>
<filter-mapping>
<filter-name>guiceFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>cn.northpark.listener.GoogleGuiceServletContextListener</listener-class><!--这个是用于注册module及servlet的-->
</listener>
Read more »

guice 常用的绑定方式 <4>

Posted on 2018-05-31 | In guice

guice 常用的绑定方式 <4>

guice在moudle中提供了良好的绑定方法。

它提供了普通的绑定,自定义注解绑定,按名称绑定等。

下面直接看代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import com.google.inject.AbstractModule;
import com.google.inject.name.Names;

/**
* 继承 AbstractModule这个抽象类
* @author bruce
*
*/
public class TestModule extends AbstractModule{

@Override
protected void configure() {

//普通绑定
bind(Dog.class).to(DarkDog.class);

//自定义注解绑定--一个接口可以多个实现
bind(Dao.class).annotatedWith(CSV.class).to(DaoImpl.class);
bind(Dao.class).annotatedWith(CSV1.class) .to(DaoImpl2.class);

//names注解常量绑定--也可用于一个接口多个实现的绑定
bindConstant().annotatedWith(Names.named("maxResults")).to(10);//类似于绑定了一个常量

}
}

Read more »

guice 配置模块的两种方式 <3>

Posted on 2018-05-29 | In guice

guice 配置模块的两种方式 <3>

guice是使用module进行绑定的,它提供了两种方式进行操作.

第一种是继承AbstractModule抽象类.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 import com.google.inject.AbstractModule;

/**
* 继承 AbstractModule这个抽象类
* @author bruce
*
*/
public class TestModule extends AbstractModule{

@Override
protected void configure() {

bind(Dog.class).to(DarkDog.class);

}
}

第二种是实现Module接口:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import com.google.inject.Binder;
import com.google.inject.Module;

/**
* 实现module 接口
* @author bruce
*
*/
public class GoogleGuiceModule implements Module{

@Override
public void configure(Binder arg) {
arg.bind(Dog.class).to(DarkDog.class);
}


}

Read more »

guice三种注入方式 <2>

Posted on 2018-05-27

guice三种注入方式 <2>

guice提供了强大的注入方式。

  • 属性注入
  • 构造器注入
  • set方式注入

1.属性注入:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import com.google.inject.Inject;
import com.google.inject.Singleton;

/**
* guice 不同的注入方式
* @author bruce
*
*/
@Singleton
public class TestInjection {

@Inject
private Dao dao;

void test(){

dao.testGuice();
}

}

2.构造方法注入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import com.google.inject.Inject;
import com.google.inject.Singleton;

/**
* guice 不同的注入方式
*
* @author bruce
*
*/
@Singleton
public class TestInjection {

private Dao dao;

@Inject
public TestInjection(Dao dao) {
this.dao = dao;
}

void test() {

dao.testGuice();
}

}

3.set方式注入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import com.google.inject.Inject;
import com.google.inject.Singleton;

/**
* guice 不同的注入方式
*
* @author bruce
*
*/
@Singleton
public class TestInjection {

private Dao dao;

@Inject
public void setDao(Dao dao) {
this.dao = dao;
}


void test() {

dao.testGuice();
}

}
Read more »

guice的基本使用<1>

Posted on 2018-05-25 | In guice

guice的基本使用<1>

guice是google一个轻量级的DI注入框架,现在比较强大了,也与目前流行的struts2、jpa等都有集成了。

先看一个例子:

1
2
3
public interface Dao {
void testGuice();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
import com.google.inject.Singleton;

@Singleton//声明此DaoImpl为单例
public class DaoImpl implements Dao{

@Override
public void testGuice() {

System.out.println("测试guice,实现依赖!");

}

}
1
2
3
4
5
6
7
8
9
10
import com.google.inject.AbstractModule;                                                  
//需要继承AbstractModule这个类,用于绑定
public class TestModule extends AbstractModule{

@Override
protected void configure() {
bind(Dao.class).to(DaoImpl.class);//这句代码的意思是说:运行时动态的将DaoImpl对象赋给Dao对象并且是单例的
}

}
1
2
3
4
5
6
7
8
9
10
11
import com.google.inject.Guice;
import com.google.inject.Injector;

public class Test {

public static void main(String[] args) {
Injector injector = Guice.createInjector(new TestModule());
Dao dao = injector.getInstance(DaoImpl.class);
dao.testGuice();
}
}
1234…16
Bruce

Bruce

Extreme ways

92 posts
45 categories
83 tags
RSS
GitHub Twitter Facebook
Links
  • NorthPark
  • 挖粪の男孩
  • 小白博客
© 2015 - 2019 Bruce
Powered by Hexo
NorthPark中文网
Mac破解软件
院线大片
情商提升
  本站访客数 人次   本站总访问量 次