Guice Integrated STRUTS2 Easy Tutorial

Source: Internet
Author: User

1.Guice Introduction

2.Helloworld

Introduction: This article is about how to use Guice for Di and how to integrate STRUTS2

1. Introduction

Guice is Google's launch of a DI framework, because its excellent won the Jolt Award. It's a lot lighter than spring. The runtime specifies a configuration class, implements its Com.google.inject.Module interface, and specifies the interface and implementation class.

The required base Jar pack has

Guice need the jar package, if it is a Web application, need Guice-servlet-2.0.jar, integration Struts2 need Guice-struts2-plugin-2.0.jar

Aopalliance.jar

Guice-2.0.jar

Guice-servlet-2.0.jar

Guice-struts2-plugin-2.0.jar

Struts2 the jar package needed

Commons-fileupload-1.2.1.jar

Commons-io-1.3.2.jar

Commons-logging-1.0.4.jar

Freemarker-2.3.13.jar

Ognl-2.6.11.jar

Struts2-core-2.1.6.jar

Xwork-2.1.2.jar



Guice vs. Spring (this table is transferred from the network)
Spring Guice
Using XML By isolating a relationship between a class and a class into XML, the container is responsible for injecting the invoked object, so called Dependency injection Without using XML, the relationship between classes and classes is isolated into module, where the reputation needs to be injected, and the container injects the invoked object according to the description in module.
Using annotation Use

Support for custom annotation annotations, for the same interface-defined object references, labeled them with different custom annotation annotation, you can achieve the same class inside the same interface reference, injected to different implementations, in the module to distinguish between annotations, flexibility greatly increased.

The use of annotation is not necessarily a good thing, such as the new characteristics of the model is not necessarily good, the current majority of servers do not support jdk1.5,wls to 9 before the support, and the current customer because of the price reason is rarely selected wls9, at least we did not have the project. The function is strong again, the customer does not need, what use?
Operational efficiency When you load a spring configuration file, parsing XML is inefficient and getbean inefficient, but using the environment does not involve Getbean, only the production environment uses the Getbean, and when the spring application is loaded, all injections are completed, So this inefficient problem is not a problem. With Annotation,cglib, one of the most obvious differences between efficiency and spring is that spring is filling the injected area when it loads the spring configuration file, while the Guice is injected at the time of use, with high efficiency and flexibility.
Class Coupling degree Low coupling, the emphasis on class non-invasive, in an externalized way to deal with dependencies, the class inside is very clean, in the configuration file, the dependence on the class is very low. High, code-level annotation, di tag @inject intrusion code, coupled to the class level, more than intrusion, simply invade, code coupling too much guice things, greatly deviate from the original intention of relying on injection, for the maintainability of the code, the readability are unfavorable
class when writing Need to write XML, configure beans, configure injection Just declare it as @inject, wait to be injected,

Finally declare the injection mode in the Unified module
Only IOC support No, spring is already dabbling in a lot of parts Yes, now it's just a di container.
Is it easy to refactor code Unified XML Configuration portal, easy to change Configuration work is done in module, and Spring XOR
Supports multiple injection modes Builder, Setter method Field, builder, setter method
Flexibility 1, if the reference to the same interface definition needs to inject different implementations, it is necessary to write different module, cumbersome

2, Dynamic injection

If you want to inject an implementation, you still unknown, how to do, Spring is no way, in advance in the configuration file written dead, and Guice can do, that is, I want to inject this object I do not know what to inject, is in the runtime to get the implementation of this interface, So this greatly increases the flexibility of the injection-dependent, dynamic injection.
Integration with existing frameworks 1, high, many existing excellent frameworks (such as struts1.x, etc.) provide spring integration portals, and spring is more than just a dependency injection, including many aspects.

2,spring also provides integration of hibernate and so on, which can greatly simplify the development difficulty.

3, provide for orm,rmi,webservice and so on many interfaces, the system is huge.
1, it can be integrated with existing frameworks, but relying on a slightly more efficient di would be a bit harder to replace spring's status.
Complexity of configuration Locating the relationship between classes and classes in XML is less difficult Code-level positioning classes and the relationship between the class, the difficulty is slightly higher

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.