Java Spring 和 Guice 在一起,或者只是 Spring

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21056063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:14:12  来源:igfitidea点击:

Spring and Guice together, or just Spring

javaspringguiceapplication-designweb-application-design

提问by csalazar

I'm starting a new Java web app from scratch.

我正在从头开始一个新的 Java Web 应用程序。

I don't have much experience on Spring Framework, but I know I'd like to use some of its features, such as Transaccions Management.

我对 Spring Framework 没有太多经验,但我知道我想使用它的一些功能,例如 Transaccions Management。

On the other hand, I really like Guice for dependency injection.

另一方面,我真的很喜欢 Guice 的依赖注入。

I know that Guice and Spring can work together: http://www.jroller.com/mindcrime/entry/an_example_of_integrating_guice

我知道 Guice 和 Spring 可以一起工作:http: //www.jroller.com/mindcrime/entry/an_example_of_integrating_guice

But before starting designing my application, I wanted to know if someone had experienced issues by following that approach.

但在开始设计我的应用程序之前,我想知道是否有人在遵循这种方法时遇到了问题。

Also, what I really like from Guice is that you don't need an XML configuration file, but just java Modules, which shorter and are easier to read. Is there any alternative to XML configuration files on Spring, similar to Guice?

此外,我真正喜欢 Guice 的一点是,您不需要 XML 配置文件,而只需要 java 模块,它更短且更易于阅读。Spring 上是否有类似于 Guice 的 XML 配置文件的替代方案?

采纳答案by MariuszS

I think Spring alone is good enough for enterprise application.

我认为仅 Spring 就足以用于企业应用程序。

Spring dosent need XML too!!! Modern Spring Apps uses JavaConfigand minimalconfiguration. Take look at Spring Boot Guides. Whole Spring apps can not use any XML at all.

Spring 也不需要 XML !!!现代 Spring Apps 使用JavaConfig最小配置。看看Spring Boot 指南。整个 Spring 应用程序根本不能使用任何 XML。

Guice is nice, but very limited. With Spring is possible to write web application or REST application with transactions and persistance very easy and fast. With Guice this is more complicated.

Guice 很好,但非常有限。使用 Spring 可以非常容易和快速地编写具有事务和持久性的 Web 应用程序或 REST 应用程序。使用Guice,这更复杂。

回答by Jakub Kubrynski

If you're just starting then I'll recommend you using https://github.com/spring-projects/spring-boot

如果您刚刚开始,那么我会建议您使用https://github.com/spring-projects/spring-boot

It has great autoconfiguration feature and saves writing boilerplate code. I even can release you from using application server due to embedded Tomcat. For example implementing simple MVC controller (which can be used as REST endpoints) looks like that:

它具有强大的自动配置功能,并且可以节省编写样板代码。由于嵌入式Tomcat,我什至可以让您不再使用应用程序服务器。例如,实现简单的 MVC 控制器(可用作 REST 端点)如下所示:

@Controller
@EnableAutoConfiguration
public class SampleController {

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleController.class, args);
    }
}

Now you can execute java -jar your_package.jarand thats all. You will also get transaction management, database integration, etc. More examples can be found in mentioned link, especially in https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samplesdirectory

现在您可以执行了java -jar your_package.jar,仅此而已。您还将获得事务管理、数据库集成等。更多示例可以在上述链接中找到,尤其是在https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples目录中

回答by Tom G

Spring has had Java-based annotation config for two major releases now. You don't need to write a single line of XML (not even a web.xml!)

Spring 现在已经为两个主要版本提供了基于 Java 的注释配置。您不需要编写一行 XML(甚至不需要编写 web.xml!)

I've worked with Guice and Spring. Guice is sufficient for smaller projects that need DI, but if you're going to be using Spring for MVC or transactional support you might as well just use its DI as well. Guice also doesn't have good profile support the way Spring does -- you have to do your own manual switching of modules if you want to have separate beans for local development, test environments, and production.

我曾与 Guice 和 Spring 合作过。对于需要 DI 的小型项目,Guice 就足够了,但是如果您打算将 Spring 用于 MVC 或事务支持,您也可以只使用它的 DI。Guice 也不像 Spring 那样具有良好的配置文件支持——如果您想为本地开发、测试环境和生产使用单独的 bean,您必须自己手动切换模块。

回答by jwells131313

I would also suggest you take a look at HK2 for a light-weight J2SE DI engine. It is similar to Guice and CDI and has many extensibility features that you might find interesting. It also works with Guice and Spring via bi-directional bridges. You can start learning about HK2 here: http://hk2.java.net/. Information on the Guice bridge is here: https://hk2.java.net/2.2.0-b27/guice-bridge.html. Information on the Spring bridge is here: https://hk2.java.net/2.2.0-b27/spring-bridge.html.

我还建议您查看 HK2 以获得轻量级 J2SE DI 引擎。它类似于 Guice 和 CDI,并且具有许多您可能会感兴趣的可扩展特性。它还可以通过双向桥与 Guice 和 Spring 一起使用。你可以从这里开始学习 HK2:http: //hk2.java.net/。Guice 桥的信息在这里:https: //hk2.java.net/2.2.0-b27/guice-bridge.html。Spring Bridge的信息在这里:https: //hk2.java.net/2.2.0-b27/spring-bridge.html

The decision of which DI provider to use is based a lot on the special features needed by your application.

决定使用哪个 DI 提供程序很大程度上取决于您的应用程序所需的特殊功能。

回答by Ashish Khandelwal

Google Guice almost has 1:1 mapping for all the Spring Dependency Injection concepts but It has a few more cool things:

Google Guice 几乎对所有 Spring 依赖注入概念都有 1:1 映射,但它还有一些更酷的东西:

1. It's all in Java.Google Guice contains all the configurations in Java code, so we don't have to deal with any xml configurations or anything.

1. 这一切都在 Java 中。Google Guice 包含 Java 代码中的所有配置,因此我们不必处理任何 xml 配置或任何事情。

2. Better error messages.Guice has an interceptor that cleans up and rethrows nicer stack traces. Spring just spits out everything.

2. 更好的错误信息。Guice 有一个拦截器,可以清理并重新抛出更好的堆栈跟踪。春天只是吐出一切。

3. Just-in-time binding (or Implicit binding). This means if you have a zero-arg constructor, there's no additional binding needed, just inject it! Same if you're trying to inject a constructor with multiple args that are either zero-arg constructors, or already injected, there is no need for additional configuration. Again, just inject it!

3. 即时绑定(或隐式绑定)。这意味着如果您有一个零参数构造函数,则不需要额外的绑定,只需注入即可!同样,如果您尝试注入具有多个零参数构造函数或已注入参数的构造函数,则无需额外配置。再次,只需注入它!

4. Eager/Lazy injectionWhen you inject with Spring, it says I will inject nothing or all the things. There is an option in Guice to inject lazily, which means only create the subsection of the dependency graph that I need. This means a few things - you don't need those weird special Spring testing files, Guice injects way faster in testing, and you can run integration tests in Eclipse with minimal to no setup!

4. Eager/Lazy injection当你用 Spring 注入时,它说我不会注入任何东西或所有东西。Guice 中有一个选项可以延迟注入,这意味着只创建我需要的依赖关系图的子部分。这意味着一些事情 - 您不需要那些奇怪的特殊 Spring 测试文件,Guice 在测试中注入速度更快,并且您可以在 Eclipse 中运行集成测试,只需最少设置甚至无需设置!

5. Binding by types.This is different from Spring, which binds by names. In Spring, if you accidentally bind two instances to the same name, Spring will fail silently, and will confuse you even more by taking the binding which came last (yech). Spring offers a "bind by type" option, but don't let it fool you - its underlying implementation is still a String.

5.按类型绑定。这与 Spring 不同,后者通过名称进行绑定。在 Spring 中,如果您不小心将两个实例绑定到同一个名称,Spring 将无声无息地失败,并且会通过采用最后一个绑定 (yech) 使您更加困惑。Spring 提供了“按类型绑定”选项,但不要让它愚弄您——它的底层实现仍然是一个字符串。

Source: https://github.com/google/guice

来源:https: //github.com/google/guice