猿问

Guice - 相当于 Guice 中的 Spring Autowired

我正在尝试使用 Guice,我来自 Spring。


我想知道是否@Inject与@AutowiredSpring 中的等效,以及我是否可以在 Web 应用程序中使用它,就像我在 Spring 中使用它一样。


想象一下,我有一个依赖于服务的 Facade,在 Spring 中我可以为该服务定义一个 bean,然后当服务器启动时,我可以在我的 Facade 中获取该服务的实例。


class FacadeImpl{

  @Autowire Service service;

  ...

}

假设服务有一个具体的实现,并且在 Spring 中会自动注入它。


Guice 有类似的方法吗?我可以做类似的事情吗


class Facade{

  @Inject Service service;

}

还是只有春天才有的魔法?


在我的网络应用程序中,我开始使用嵌入式 tomcat,并以这种方式使用了 google guice 模块


Guice.createInjector(new ConfigurationModule());

希望这足以“注入”任何用@Inject.


但是,它不起作用(我并不感到惊讶)。你们能帮我弄清楚哪些是 BP 来注入我的 Servlets 或 Facades 等依赖项吗?


墨色风雨
浏览 197回答 2
2回答

幕布斯7119047

是的,@Inject可以作为@Autowired......给定一些条件。Guice 不需要Modules,尽管它们经常使用。因此,如果您愿意,您可以摆脱它们。如果您的类是具体类,您可以直接使用@Inject它,就像@Autowired,但您可能还必须标记该类,@Singleton因为 Guice 中的默认范围不是单例,而是每个人的新实例,与 Spring 不同。Guice 不是 Spring,但其中一个最重要的特性存在于另一个中。在 Tomcat 中使用 Guice当你想用 Tomcat 使用 Guice 时,需要做的配置很少,但仍然有配置。您将需要一个Module,但只需要servlet。在您的 中web.xml,添加以下内容:<listener>&nbsp; <listener-class>path.to.MyGuiceServletConfig</listener-class></listener><filter>&nbsp; <filter-name>guiceFilter</filter-name>&nbsp; <filter-class>com.google.inject.servlet.GuiceFilter</filter-class></filter><filter-mapping>&nbsp; <filter-name>guiceFilter</filter-name>&nbsp; <url-pattern>/*</url-pattern></filter-mapping>MyGuiceServletConfig.javapublic class MyGuiceServletConfig extends GuiceServletContextListener {&nbsp; @Override protected Injector getInjector() {&nbsp; &nbsp; return Guice.createInjector(new ServletModule() {&nbsp; &nbsp; &nbsp; @Override protected void configureServlets() {&nbsp; &nbsp; &nbsp; &nbsp; serve("/*").with(MyServlet.class); // Nothing else is needed.&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; }}MyServlet.javapublic class MyServlet extends HttpServlet {&nbsp; @Inject // Similar to @Autowired&nbsp; private MyService myService;&nbsp; @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {&nbsp; &nbsp; resp.getWriter().write(myService.hello("Guice"));&nbsp; }}现在你有一个选择MyService:要么用它制作一个接口,你必须定义和绑定一个实现,要么用它制作一个具体的类。MyService 作为接口MyService.java@ImplementedBy(MyServiceImpl.class) // This says "hey Guice, the default implementation is MyServiceImpl."public interface MyService {&nbsp; String hello(String name);}MyServiceImpl.java@Singleton // Use the same default scope as Springpublic class MyServiceImpl implements MyService {&nbsp; // @Inject dependencies as you wish.&nbsp; public String hello(String name) { return "Hello, " + name + "!"; }}如果您想避免@ImplementedBy,您仍然可以使用上面的模块,并添加bind(MyService.class).to(MyServiceImpl.class).in(Scopes.SINGLETON);或编写相同的提供程序方法Module:@Provides @Singleton MyService provideMyService() {&nbsp; return new MyServiceImpl();}MyService 作为一个具体的类MyService.java@Singleton // Use the same default scope as Springpublic class MyService {&nbsp; // @Inject dependencies as you wish.&nbsp; public String hello(String name) { return "Hello, " + name + "!"; }}

GCT1015

在 Guice 中,没有@AutowiredSpring 注释的直接等价物。依赖注入的使用在入门页面中有解释。1)您必须使用注释来注释您的服务的构造函数@Inject:&nbsp;@Inject&nbsp; BillingService(CreditCardProcessor processor,&nbsp;&nbsp; &nbsp; &nbsp; TransactionLog transactionLog) {&nbsp; &nbsp; this.processor = processor;&nbsp; &nbsp; this.transactionLog = transactionLog;&nbsp; }2)然后在一个模块中定义类型和实现之间的绑定:public class BillingModule extends AbstractModule {&nbsp; @Override&nbsp;&nbsp; protected void configure() {&nbsp; &nbsp; &nbsp;/*&nbsp; &nbsp; &nbsp; * This tells Guice that whenever it sees a dependency on a TransactionLog,&nbsp; &nbsp; &nbsp; * it should satisfy the dependency using a DatabaseTransactionLog.&nbsp; &nbsp; &nbsp; */&nbsp; &nbsp; bind(TransactionLog.class).to(DatabaseTransactionLog.class);&nbsp; &nbsp; &nbsp;/*&nbsp; &nbsp; &nbsp; * Similarly, this binding tells Guice that when CreditCardProcessor is used in&nbsp; &nbsp; &nbsp; * a dependency, that should be satisfied with a PaypalCreditCardProcessor.&nbsp; &nbsp; &nbsp; */&nbsp; &nbsp; bind(CreditCardProcessor.class).to(PaypalCreditCardProcessor.class);&nbsp; }}3)最后构建一个注入器并使用它:&nbsp;public static void main(String[] args) {&nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp;* Guice.createInjector() takes your Modules, and returns a new Injector&nbsp; &nbsp; &nbsp;* instance. Most applications will call this method exactly once, in their&nbsp; &nbsp; &nbsp;* main() method.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; Injector injector = Guice.createInjector(new BillingModule());&nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp;* Now that we've got the injector, we can build objects.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; BillingService billingService = injector.getInstance(BillingService.class);&nbsp; &nbsp; ...&nbsp; }
随时随地看视频慕课网APP

相关分类

Java
我要回答