Guice-Guice中的Spring Autowired等效于什么 [英] Guice - what is the equivalent of Spring Autowired in Guice
问题描述
我正在尝试使用Guice,而我来自Spring.
I am trying to use Guice and I am coming from Spring.
我想知道@Inject
是否等于Spring中的@Autowired
,是否可以像在Spring中一样使用它在Web应用程序中使用.
I am wondering if @Inject
is the equivalent of @Autowired
in Spring and if I can use it in web application exactly as I am using it in Spring.
想象一下,我有一个依赖于服务的Facade,在Spring中我可以为该服务定义一个bean,然后在服务器启动时就可以在Facade中获取该服务的实例.
Imagine I have a Facade which depends on a service, in Spring I can define a bean for that service and then when the server starts up I can get the instance of the service inside my facade.
class FacadeImpl{
@Autowire Service service;
...
}
假设该服务具有具体的实现,并且在Spring中将自动注入它.
Assume that service has a concrete implementation and in Spring will automatically inject it.
Guice有类似的方法吗?我可以做类似的事情吗
Does Guice has a similar approach? can I do something like
class Facade{
@Inject Service service;
}
还是只有春天才有的魔力?
or is it a magic that only spring does?
在我的Web应用程序中,我正在启动嵌入式tomcat,并且以这种方式使用了google guice模块
In my web application, I am starting embedded tomcat and I used google guice modules in such a way
Guice.createInjector(new ConfigurationModule());
希望这足以注入"用@Inject
注释的内容.
hoping that this will be enough to "inject" whatever is annotated with @Inject
.
但是,它不起作用(我并不感到惊讶). 你们可以帮我弄清楚哪个BP注入对我的Servlet或Facades等的依赖吗??
However, it is not working ( I am not surprised ). Can you guys help me to figure out which are the BP to inject dependencies on my Servlets or Facades etc..?
推荐答案
是的,在某些条件下,@Inject
可以充当@Autowired
....
Guice不需要Module
,尽管它们很常用.因此,您可以根据需要摆脱它们.
Yes, @Inject
can act as @Autowired
... given some conditions.
Guice doesn't require Module
s, though they're very often used. So you can get rid of them if you want to.
如果您的班级是具体班级,则可以像@Autowired
一样直接@Inject
班级,但是您可能还必须标记班级@Singleton
,因为Guice中的默认作用域不是单例,而是新的实例所有人,与Spring不同.
If your class is a concrete class, you can directly @Inject
it, just like @Autowired
, but you also probably have to mark the class @Singleton
because the default scope in Guice is not singleton, but a new instance everyone, unlike Spring.
Guice不是Spring,但是其中一个的最重要的特征存在于另一个.
Guice is not Spring, but the most important features of one are present in the other.
当您想将Guice与Tomcat一起使用时,几乎不需要进行任何配置,但是仍然需要进行配置.您将需要Module
,但仅需要servlet.
When you want to use Guice with Tomcat, there is very little configuration to do, but there is still configuration. You will require a Module
, but only the servlet.
在您的 web.xml
中,添加以下内容:
In your web.xml
, add the following:
<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
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
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
:您可以从中创建一个接口,然后定义,绑定一个实现,或者从中创建一个具体的类.
Now you have a choice with MyService
: either you make an interface out of it and you have to define, and bind an implementation, or you make a concrete class out of it.
MyService.java
MyService.java
@ImplementedBy(MyServiceImpl.class) // This says "hey Guice, the default implementation is MyServiceImpl."
public interface MyService {
String hello(String name);
}
MyServiceImpl.java
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
中编写提供程序方法:
If you want to avoid the @ImplementedBy
, you can still use your module above, and add bind(MyService.class).to(MyServiceImpl.class).in(Scopes.SINGLETON);
, or write a provider method in the same Module
:
@Provides @Singleton MyService provideMyService() {
return new MyServiceImpl();
}
MyService
作为具体课程
MyService.java
MyService
as a concrete class
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 + "!"; }
}
这篇关于Guice-Guice中的Spring Autowired等效于什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!