Google Guice是一个轻量级的依赖注入框架,专注于依赖注入和IoC,适用于中小型应用。
Spring Framework是一个全面的企业级框架,提供了广泛的功能,适用于大型企业应用。
是吧!IOC 容器不止Spring,还有Google Guice,来体验体验吧:
首先,第0步那肯定是添加maven依赖:
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.2.3</version> <!-- 使用适当的版本号 -->
</dependency>
步骤1:创建接口和实现类
首先,创建一个接口和实现类:
// Service接口
public interface Service {
void execute();
}
// Service的实现类
public class MyService implements Service {
@Override
public void execute() {
System.out.println("MyService 执行了.");
}
}
步骤2:创建Guice模块
接下来,创建一个Guice模块,其中配置了Guice容器来管理依赖关系。在这个模块