亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Guice - 相當于 Guice 中的 Spring Autowired

Guice - 相當于 Guice 中的 Spring Autowired

墨色風雨 2021-12-22 15:18:42
我正在嘗試使用 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 等依賴項嗎?
查看完整描述

2 回答

?
幕布斯7119047

TA貢獻1794條經驗 獲得超8個贊

是的,@Inject可以作為@Autowired......給定一些條件。

Guice 不需要Modules,盡管它們經常使用。因此,如果您愿意,您可以擺脫它們。


如果您的類是具體類,您可以直接使用@Inject它,就像@Autowired,但您可能還必須標記該類,@Singleton因為 Guice 中的默認范圍不是單例,而是每個人的新實例,與 Spring 不同。


Guice 不是 Spring,但其中一個最重要的特性存在于另一個中。


在 Tomcat 中使用 Guice

當你想用 Tomcat 使用 Guice 時,需要做的配置很少,但仍然有配置。您將需要一個Module,但只需要servlet。


在您的 中web.xml,添加以下內容:


<listener>

  <listener-class>path.to.MyGuiceServletConfig</listener-class>

</listener>


<filter>

  <filter-name>guiceFilter</filter-name>

  <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>

</filter>


<filter-mapping>

  <filter-name>guiceFilter</filter-name>

  <url-pattern>/*</url-pattern>

</filter-mapping>

MyGuiceServletConfig.java


public class MyGuiceServletConfig extends GuiceServletContextListener {

  @Override protected Injector getInjector() {

    return Guice.createInjector(new ServletModule() {

      @Override protected void configureServlets() {

        serve("/*").with(MyServlet.class); // Nothing else is needed.

      }

    });

  }

}

MyServlet.java


public class MyServlet extends HttpServlet {


  @Inject // Similar to @Autowired

  private MyService myService;


  @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    resp.getWriter().write(myService.hello("Guice"));

  }

}

現在你有一個選擇MyService:要么用它制作一個接口,你必須定義和綁定一個實現,要么用它制作一個具體的類。


MyService 作為接口

MyService.java


@ImplementedBy(MyServiceImpl.class) // This says "hey Guice, the default implementation is MyServiceImpl."

public interface MyService {

  String hello(String name);

}

MyServiceImpl.java


@Singleton // Use the same default scope as Spring

public class MyServiceImpl implements MyService {

  // @Inject dependencies as you wish.

  public String hello(String name) { return "Hello, " + name + "!"; }

}

如果您想避免@ImplementedBy,您仍然可以使用上面的模塊,并添加bind(MyService.class).to(MyServiceImpl.class).in(Scopes.SINGLETON);或編寫相同的提供程序方法Module:


@Provides @Singleton MyService provideMyService() {

  return new MyServiceImpl();

}

MyService 作為一個具體的類

MyService.java


@Singleton // Use the same default scope as Spring

public class MyService {

  // @Inject dependencies as you wish.

  public String hello(String name) { return "Hello, " + name + "!"; }

}


查看完整回答
反對 回復 2021-12-22
?
GCT1015

TA貢獻1827條經驗 獲得超4個贊

在 Guice 中,沒有@AutowiredSpring 注釋的直接等價物。依賴注入的使用在入門頁面中有解釋。


1)您必須使用注釋來注釋您的服務的構造函數@Inject:


 @Inject

  BillingService(CreditCardProcessor processor, 

      TransactionLog transactionLog) {

    this.processor = processor;

    this.transactionLog = transactionLog;

  }

2)然后在一個模塊中定義類型和實現之間的綁定:


public class BillingModule extends AbstractModule {

  @Override 

  protected void configure() {


     /*

      * This tells Guice that whenever it sees a dependency on a TransactionLog,

      * it should satisfy the dependency using a DatabaseTransactionLog.

      */

    bind(TransactionLog.class).to(DatabaseTransactionLog.class);


     /*

      * Similarly, this binding tells Guice that when CreditCardProcessor is used in

      * a dependency, that should be satisfied with a PaypalCreditCardProcessor.

      */

    bind(CreditCardProcessor.class).to(PaypalCreditCardProcessor.class);

  }

}

3)最后構建一個注入器并使用它:


 public static void main(String[] args) {

    /*

     * Guice.createInjector() takes your Modules, and returns a new Injector

     * instance. Most applications will call this method exactly once, in their

     * main() method.

     */

    Injector injector = Guice.createInjector(new BillingModule());


    /*

     * Now that we've got the injector, we can build objects.

     */

    BillingService billingService = injector.getInstance(BillingService.class);

    ...

  }


查看完整回答
反對 回復 2021-12-22
  • 2 回答
  • 0 關注
  • 228 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號