【Hello】Guice

介绍
Guice is a lightweight dependency injection framework for Java .
即 Guice 是轻量级依赖注入的 Java 框架。

Guice

特点

  1. 取消 Bean 概念,使用 Java 代码描述绑定规则
  2. 泛型支持
  3. 专注于 Dependency Injection

依赖

Maven

1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- Guice -->
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.2.1</version>
</dependency>

<!-- Guice扩展插件:多值绑定 -->
<dependency>
<groupId>com.google.inject.extensions</groupId>
<artifactId>guice-multibindings</artifactId>
<version>4.2.1</version>
</dependency>

Gradle

1
2
3
4
5
// Guice
compile 'com.google.inject:guice:4.2.1'

// Guice扩展插件:多值绑定
compile 'com.google.inject.extensions:guice-multibindings:4.2.1'

注入 @Inject

1.构造器注入(推荐)

1
2
3
4
5
6
7
8
9
10
11
12
public class BookManager {

private final BookService bookService;
private final PriceService priceService;

@Inject
public BookManager(BookService bookService, PriceService priceService) {
this.bookService = bookService;
this.priceService = priceService;
}
...
}

2.设值注入

1
2
3
4
5
6
7
8
9
public class BookManager {

@Inject
private BookService bookService;

@Inject
private PriceService priceService;
...
}

绑定 bind(…)

1.类名绑定

1
bind(BookService.class).to(BookServiceImpl.class);

2.实例绑定

1
bind(BookDao.class).toInstance(new BookDaoImpl()));

3.链式绑定

1
2
3
4
5
6
7
bind(PriceService.class).to(PriceServiceImpl.class);
bind(PriceServiceImpl.class).toInstance(new PriceServiceImpl() {
@Override
public BigDecimal getPrice() {
return BigDecimal.ONE;
}
});

4.Provider 绑定

1
2
3
4
5
6
bind(CurrencyService.class).toProvider(CurrencyProvider.class);

@Provides
List<String> getSupportedCurrencies(CurrencyService currencyService) {
return currencyService.getSupportedCurrency();
}

5.命名绑定

1
2
3
4
5
@Provides
@Named("supportedCurrencies")
List<String> getSupportedCurrencies(CurrencyService currencyService) {
return currencyService.getSupportedCurrency();
}

6.泛型绑定

1
2
3
bind(new TypeLiteral<List<String>>() {})
.annotatedWith(Names.named("supportedCurrencies"))
.toInstance(Arrays.asList("CNY", "JPY"));

7.集合绑定

1
2
3
4
5
6
7
8
9
10
// 集合Set绑定
Multibinder<String> currencyBinder = Multibinder.newSetBinder(binder(), String.class);
currencyBinder.addBinding().toInstance("ZH");
currencyBinder.addBinding().toInstance("EN");
currencyBinder.addBinding().toInstance("DE");

// 集合Map绑定
MapBinder<String, Integer> authorBinder = MapBinder.newMapBinder(binder(),String.class,Integer.class);
authorBinder.addBinding("Leif").toInstance(20);
authorBinder.addBinding("Chen").toInstance(18);

Module 的相互关系

  1. 并列
  2. 嵌套
  3. 覆盖

作用域

  1. 默认:一般实例,构造速度快
  2. 单例 singleton :构造速度慢的实例,必须线程安全,如数据库连接
  3. Session Scope :含有 session/request 信息的实例

Guice 与 Spring 的依赖注入代码比较

一、Spring 依赖注入

  • Dao
1
2
3
4
public interface UserDao {

void save();
}
1
2
3
4
5
6
7
public class UserDaoImpl implements UserDao {

@Override
public void save() {
System.out.println("Spring BookDao method: save().");
}
}
  • Service
1
2
3
4
public interface UserService {

void save();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class UserServiceImpl implements UserService {

private final UserDao userDao;

public UserServiceImpl(UserDao userDao) {
this.userDao = userDao;
}

@Override
public void save() {
System.out.println("Spring BookService method: save().");
userDao.save();
}
}
  • Manager
1
2
3
4
5
6
7
8
9
10
11
12
public class UserManager {

private final UserService userService;

public UserManager(UserService userService) {
this.userService = userService;
}

public void test() {
userService.save();
}
}
  • 配置文件 applicationContext.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="userDao" class="com.chen.spring.dao.impl.UserDaoImpl"/>
<bean id="userService" class="com.chen.spring.service.impl.UserServiceImpl">
<constructor-arg ref="userDao"/>
</bean>
<bean id="userManager" class="com.chen.spring.manager.UserManager">
<constructor-arg ref="userService"/>
</bean>

</beans>
  • 主程序
1
2
3
4
5
6
7
8
public class SpringApp {

public static void main(String[] args) {
BeanFactory beanFactory = new ClassPathXmlApplicationContext("applicationContext.xml");
UserManager userManager = (UserManager) beanFactory.getBean("userManager");
userManager.test();
}
}

二、Guice 依赖注入

  • Dao
1
2
3
4
public interface BookDao {

void save();
}
1
2
3
4
5
6
7
public class BookDaoImpl implements BookDao {

@Override
public void save() {
System.out.println("Guice BookDao method: save().");
}
}
  • Service
1
2
3
4
public interface BookService {

void save();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class BookServiceImpl implements BookService {

private final BookDao bookDao;

@Inject
public BookServiceImpl(BookDao bookDao) {
this.bookDao = bookDao;
}

@Override
public void save() {
System.out.println("Guice BookService method: save().");
bookDao.save();
}
}
  • Manager
1
2
3
4
5
6
7
8
9
10
11
12
13
public class BookManager {

private final BookService bookService;

@Inject
public BookManager(BookService bookService) {
this.bookService = bookService;
}

public void test() {
bookService.save();
}
}
  • Module
1
2
3
4
5
6
7
8
public class BookModule extends AbstractModule {

@Override
protected void configure() {
bind(BookDao.class).toInstance(new BookDaoImpl());
bind(BookService.class).to(BookServiceImpl.class);
}
}
  • 主程序
1
2
3
4
5
6
public class GuiceApp {

public static void main(String[] args) {
Guice.createInjector(new BookModule()).getInstance(BookManager.class).test();
}
}

参考

[1] 慕课网:使用Google Guice实现依赖注入
[2] 代码

0%