当前位置:网站首页>Simple sample code of google's lightweight DI framework guice
Simple sample code of google's lightweight DI framework guice
2022-08-06 04:02:00 【Xiao Wu classmate GOGOGO】
package java_basic;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import org.apache.commons.logging.Log;
import org.junit.Test;
/** * @className: GuiceDemo * @Description: TODO * @date: 2022/8/4 17:40 */
public class GuiceDemo {
public static interface UserService{
void process();
}
public static class UserServiceImpl implements UserService{
@Override
public void process() {
System.out.println("I need to do some business logic");
}
}
public static interface LogService{
void log(String msg);
}
public static class LogServiceImpl implements LogService{
@Override
public void log(String msg) {
System.out.println("-----LOG:"+msg);
}
}
public static interface Application{
void work();
}
// guice 的配置;
public class MyAppMoudle extends AbstractModule{
@Override
protected void configure() {
bind(LogService.class).to(LogServiceImpl.class);
bind(UserService.class).to(UserServiceImpl.class);
bind(Application.class).to(MyApp.class);
}
}
public static class MyApp implements Application{
private UserService userService;
private LogService logService;
// Inject dependencies in the constructor
@Inject
public MyApp(UserService userService, LogService logService){
this.userService = userService;
this.logService = logService;
}
@Override
public void work() {
userService.process();
logService.log("all is ok");
}
}
@Test
public void test(){
Injector injector = Guice.createInjector(new MyAppMoudle());
Application app = injector.getInstance(Application.class);
app.work();
}
}
需要事先导入mavan依赖:
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.1.0</version>
</dependency>
感兴趣的可以学习一下,继续深入了解
边栏推荐
猜你喜欢

为什么double = 5 / 9; 的结果为零

2022 Alibaba Cloud server configuration selection strategy

Internet protocol overview

【LaTex】 - How to use the alignment symbol &, the usage of the newline character \\, how to solve the Misplaced & error

Introduction to Elliptic Curves (4): Elliptic Curve Security, Compared with RSA

Practical Application Case of Digital Twin - Smart Port

字节跳动 Flink 状态查询实践与优化

【C语言数组下标越界】数组下标越界引发的死循环

电赛的控制类选题之问

Qixi Festival WeChat confession wall applet source code / Laravel-based confession wall WeChat applet source code
随机推荐
Preprocessing (in-depth understanding of C language)
2022 Practical Content Course on Building Modern Web Applications with Go
BGP详解(1)
Single player up to 50,000 bonus!The top conference paper recurrence competition is officially launched, and 70+ public tasks are waiting for you to challenge
Qixi Festival WeChat confession wall applet source code / Laravel-based confession wall WeChat applet source code
2022 Alibaba Cloud server configuration selection strategy
Sorting out the knowledge system of fully homomorphic encryption
请求DNS查找的host命令示例
旁路缓存策略的缓存一致性问题?
P1065 [NOIP2006 提高组] 作业调度方案
14. go channel
Django reports an error ModuleNotFoundError: No module named 'mysqlclient'
NetCore - custom exception handling
gst-launch-1.0
离散数学期末习题
【Untitled】
程序员上班第一天 | 每日趣闻
【无标题】
(九)集合 - List
89.(cesium之家)cesium聚合图(自定义图片)