Atitit.guice3 IOC Best Practices o9o

Source: Internet
Author: User

Atitit.guice3 IOC Best Practices o9o

1. Guice 's strengths and compare to a spring 1

2. Two parts : :: bind and inject @Inject 1

3. Binding all Methods Summary 2

3.1. Bind itself. Replace new. Binder.bind (Beanservice1.class); Bind itself 2

3.2.1.1.1 Chain Binding 2

3.3. Using multiple identical interfaces , different implementations , using named Bindings Annotations named Bindings 2

3.4.3) Binding gaze and instance basic type String,int This basic type 3

3.5. Instance Binding 3

3.6. Default Bindings 4

3.7. Single Case binding 4

4. Other anno Description 4

4.1. @ImplementedBy , recommended. Direct parent class/interface specifies implementation class 4

4.2. @ProvidedBy generation of complex objects ... 4

5. Integration with Spring 5

6. Disadvantages of Guice 5

7. References 5

1. Guicethe Strengths andwith aSpringthe comparison

High speed ...

No configuration file required ... Simple ... SPR Use annotations also to configure file configuration SACN Package ... trouble. ..

Compiler check and refactoring support, due to code implementation

2. Two sections:::binding andinjected@Inject

Binding Module

The core of Guice is com.google.inject.Module, which is similar to the Spring Bean Factory

3.2 Attribute injection

1 The purpose of the property binding is to tell Guice. When you create an object of that type. Which attributes also require dependency injection. In a sample:

2 other aspects, for example Struts2 of the Action or Service the injection between the layers and so on. Just use @Inject . Not so much set,get,

Author:: Old Wow's paw attilax Ayron, email:[email protected]

Reprint please indicate source: Http://blog.csdn.net/attilax

3. Binding all Methods Summary 3.1. Binds itself. Replace New. Binder.bind (Beanservice1.class);bind itself

Like BeanService1 is an implementation class and does not implement any interface, it can of course be injected by other classes, able to use the default binding itself, although it makes no sense. For injected class parameters. Guice is created directly after it is identified.

or use. Binder.bind (Beanservice1.class);

3.2.1.1.1Chained Bindings

Chained bindings are the simplest. The most direct, and the most used binding method.

3.3. Use multiple identical interfaces,not the same implementation。 Using named BindingsAnnotationsNaming bindings

Chained binding is not a good way to differentiate which implementation to use. The ability to think of Annotations binding as an extension of chained bindings is specifically designed to solve the problem of multiple implementations of the same interface. Annotations binding can also be divided into two types. One is the need to write their own Annotations. The second one simplifies some of them.

The way you use @Named is very similar to the way you write Annotation yourself, just to make the corresponding simplification. No longer need to write Annotation yourself . [Java]view plaincopy

3 Public class realbillingservice implements BillingService {

4   

5     @Inject    

6  public realbillingservice (@Named("Checkout") Creditcardprocessor processor,

7 bind (creditcardprocessor. class )  

8 . Annotatedwith (names.named ("Checkout"))

9 . to (Checkoutcreditcardprocessor. class  );

3.4.3) binding gaze and instance base typeString,intthis basic type

。 It is assumed that the basic type, such as String, int, is injected. Two things need to be done: one is to add a name to the number of participants and gaze at @named. For example, see the following:

@Inject

public void SetName (@Named ("Beanservice1name") String name) {

THIS.name = name;

}

@Inject @Named("thql")

Public String testhql;

BD . bindconstant (). Annotatedwith (Names. named ("thql")) . to ("from Tuserusers") ;

Bind (String.class)

. Annotatedwith (Names.named ("JDBC URL"))

. ToInstance ("Jdbc:mysql://localhost/pizza");

3.5. Instance Bindings

The chained bindings described above are binding the class object of an interface to the class object of the implementation class , whereas instance binding can be seen as a special case of chained binding. It directly binds an instance object to its class object.

?

1

2

3

4

5

6

Bind (String.class)

. Annotatedwith (Names.named ("JDBC URL"))

. ToInstance ("Jdbc:mysql://localhost/pizza");

Bind (Integer.class)

. Annotatedwith (names.named ("Login timeout Seconds"))

. ToInstance (10);

3.6. Default Bindings

Xx=new xx ()

3.7. Single-Case binding

Bind (Anotherconcreteclass.class). in (Singleton.class);

Binder.bind (HelloWorld. class ). to (Helloworldimplagain. class ). In (Scopes.singleton);

In fact, Guice offers two kinds of Scope,com.google.inject.scopes.singleton and Com.google.inject.Scopes.NO_SCOPE. The so-called no scope is the generation of a new instance at a time.

Or...

@Singleton
2 public class Helloworldimpl implements HelloWorld {

3.8. Specifying constructor bindings

In the Configure method. When you bind a type to the second type, specify the target type to use that constructor to generate the object.

?

1

2

3

4

5

6

7

8

9

10

11

public class Billingmodule extends Abstractmodule {

@Override

protected void Configure () {

try {

Bind (Transactionlog.class). Toconstructor (

DatabaseTransactionLog.class.getConstructor (Databaseconnection.class));

} catch (Nosuchmethodexception e) {

Adderror (e);

}

}

}

This binding method is mainly used when it is inconvenient to use annotations @Inject to decorate the constructor of the target type. For example, the target type is the type provided by the third party. Or, there are multiple constructors in the target type, and different constructors may be used in different situations.

4. Other AnnoDescription4.1.@ImplementedBy ,To bind in a parent type4.2.It is recommended that the immediate parent class / Interface Specify the implementation class

@ImplementedBy (Class) gaze mode. Can point directly from your interface to a default implementation, and omit the implementation of Com.google.inject.Module.

In fact, this violates the principle of polymorphism. Generally use less, and finally give control to module to deal with.

This is usually done as a default implementation class to implement the LAN, or K practical .... Walk ten seems to only apply to an interface ...

4.3. @ProvidedBy generation of complex objects...

It is very complicated to assume that the process of creating objects in a method. We'll think about it. Is it possible to separate it and form a special class of action?

Reference di framework Google-guice Introduction-OPEN development experience Library. htm

4.3.1.1. In@Providesmethod to bind in the

5. With springthe Integration

final Beanfactory beanfactory = new Classpathxmlapplicationcontext (

New string[] {"Applicationcontext.xml", "Daocontext.xml"});

Binder.bind (beanfactory.class). ToInstance (Beanfactory);

After Guice and spring are defined, the Spring factory is also managed by Guice.

Binder.bind (helloworld.class). Toprovider (

fromspring (Helloworld.class, "HelloWorld"));

6. Guicethe shortcomings

Guice Of course there are drawbacks too-too simple. Many java EE developers feel that they do not seem to have any real value. It's more like a toy. But I'm bullish on Guice 's ideas-simple and type-safe.

7. References

Google-guice Introduction -forlong401 's Column - Blog channel -CSDN.NET.htm

Guice really can't enjoy enterprise-class components - Sycamore Rain-168-itpub blog . htm

alternative ways to use Google Guice binding constants ---- Read and inject configuration information -Sky's blog-blogjava.htm

Di Framework Google-guice Introduction-Open Open hair experience database. htm

Atitit.guice3 IOC Best Practices o9o

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.