2 回答

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 + "!"; }
}

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);
...
}
添加回答
舉報