Preliminary study on Guice (i.)

Source: Internet
Author: User

1. What is Guice?

Guice is a lightweight, Google-developed, Dependency Injection Framework (IOC) based on JAVA5, which mainly uses generics and annotation features. Guice is very small and fast. Guice is type-safe and can be injected into constructors, properties, methods (arbitrary methods that contain arbitrary arguments, not just setter methods).

2. Comparison of Guice and spring

A bit like spring, Guice is also a dependency injection framework that uses annotations primarily to complete dependency injection, rather than using XML primarily as spring (although annotations can be used, but XML is also configured as an integral part of spring). The Guice feature is lightweight enough to use Guice is a good choice for applications where performance requirements (including startup speed, run time, etc.) are high, and the Guice relies on a small number of jar packages, which is a bit easier for project management. Disadvantages are: The Guice function is not strong enough, as the enterprise-level framework in the functional or slightly weaker, the code is more intrusive, the dependency relationship is not as intuitive as the XML description (this is of course for those who are accustomed to XML as a description of the person, you may be used to annotate the way, this varies )。

3. Download the jar Package

Download the jar package, or maven dependency, to load the Guice required jar package into the project in use:

Maven Dependency Example:

< dependency >

 < groupId >com.jolira</ groupId > < artifactId >guice</ artifactId > < version >3.0.0</ version >

4. Package Dependency

Description: Javax.inject-1.jar is required to meet the Java Dependency Injection Standard (JSR-330), Cglib and ASM are the jar packages required for AOP, and Aopalliance is an AOP Java interface package.

5, HelloWorld
package  com.guice.test2;< Span class= "Hljs-keyword" >import; /** * TODO: Using guice provided annotations, Implementedby, means that the interface is implemented by the Helloguiceimpl class so that we can not manually configure dependencies * *< Span class= "Hljs-javadoctag" > @author  E468380 */  @ImplementedBy  ( Helloguiceimpl.class) public   interface  helloguice  { public  void  sayhello  ();}
package  com.guice.test2;< Span class= "Hljs-keyword" >public  class  helloguiceimpl  implements   Helloguice  {  @Override  public  void  sayhello  () {System.out.println (
      "Hello Guice!" );} 
package com.guice.test2;import org.junit.Test;import;import;/** * TODO : 在这里我们不需要再new一个module,Guice会根据我们提供的注解自己来配置依赖关系 *  * @author E468380 */publicclass TestGuice {    @Test    publicvoidtestHelloGuice() {        Injector injector = Guice.createInjector();        HelloGuice helloGuice = injector.getInstance(HelloGuice.class);        helloGuice.sayHello();    }}

Output Result:

Hello Guice!

Preliminary study on Guice (i.)

Related Article

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: 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.