Java lightweight IOC framework Guice and iocguice

Source: Internet
Author: User

Java lightweight IOC framework Guice and iocguice

Getting started with Google-Guice (a clear description of the process ):
Http://blog.csdn.net/derekjiang/article/details/7231490

 

With Guice, you need to add third-party packages (guice-3.0.jar and javax. inject. jar)

Link: http://pan.baidu.com/s/1nuMjYOT password: 1soo

Method to import the package into MyEclipse or eclipse: http://jingyan.baidu.com/article/6079ad0e7e4de128fe86db40.html

Google Guice is a lightweight dependency injection framework.
Guice is an absolutely lightweight java IoC container developed by Google Daniel Bob lee. Its advantages include:
1. Fast, claim to be 100 times faster than spring.
2. There is no external configuration (you can use the Guice extension package if you need to use external resources). It is completely based on the annotation Feature and supports refactoring and static code check.
3. Simple, fast, and basically no learning cost.
Guice is more suitable for embedded or high-performance but simple project solutions, such as OSGI containers, and spring is more suitable for large project organizations.
(As a beginner, I don't have much experience with the above two points, so I have no unique and detailed insights on this aspect. I am also in touch with these things with a learning mentality .)

 

Injection Method: constructor, attribute, and function injection. To implement guice, you only need to mark @ Inject on the constructor, field, or injection function.
(This part of the complete Learning Web site: http://www.cnblogs.com/whitewolf/p/4185908.html)

Constructor Injection

public class ServiceImpl implements Service {  private PeopleService people;  private GoodsService goods;  @Inject  public ServiceImpl(PeopleService people, GoodsService goods) {    this.people = people;    this.goods = goods;  }}

Property Injection

public class ServiceImpl implements Service {  private PeopleService people;  private GoodsService goods;  @Inject  public init(PeopleService people, GoodsService goods) {    this.people = people;    this.goods = goods;  }}

Function Injection

public public class ServiceImpl implements Service {  private PeopleService people;  private GoodsService goods;  @Inject  public setPeopleService(PeopleService people) {    this.people = people;  }  @Inject  public setGoodsService(GoodsService goods) {    this.goods = goods;  }}

 

Module dependency Registration
Guice provides dependency configuration classes, which must be inherited to AbstractModule to implement the configure method. In the configure method, we can use Binder to configure dependencies.
The Binder uses the chain to form a set of unique semantic DSL, such:
• Basic configuration: binder. bind (serviceClass). to (implClass). in (Scopes. [SINGLETON | NO_SCOPE]);
• No base class or interface configuration: binder. bind (implClass). in (Scopes. [SINGLETON | NO_SCOPE]);
• Service instance configuration: binder. bind (serviceClass). toInstance (servieInstance). in (Scopes. [SINGLETON | NO_SCOPE]);
• Multiple instances are injected by name: binder. bind (serviceClass ). annotatedWith (Names. named ("name ")). to (implClass ). in (Scopes. [SINGLETON | NO_SCOPE]);
• Runtime injection: The @ Provides annotation injection method is equivalent to spring @ Bean.
• @ ImplementedBy: Or Mark @ ImplementedBy on the implementation interface to specify its implementation class. This method is somewhat counter-OO design, so the abstract should not know its implementation class.
Only @ Inject annotation is required for the above configuration in the injection method, but @ Named annotation must be added before the parameter for name-based injection, as shown below:

public void configure() {  final Binder binder = binder();  binder.bind(NamedService.class).annotatedWith(Names.named("impl1")).to(NamedServiceImpl1.class);  binder.bind(NamedService.class).annotatedWith(Names.named("impl2")).to(NamedServiceImpl2.class);}@Injectpublic List<NamedService> getAllItemServices(@Named("impl1") NamedService nameService1,@Named("impl2") NamedService nameService2) {}

Guice can also use the @ Provides annotation injection method for runtime injection: as shown in Figure

@Providespublic List<NamedService> getAllItemServices(@Named("impl1") NamedService nameService1,@Named("impl2") NamedService nameService2) {final ArrayList<NamedService> list = new ArrayList<NamedService>();list.add(nameService1);list.add(nameService2);return list;}

 

Google Guice series tutorials-basic practices

Http://www.cnblogs.com/youngC/archive/2012/12/21/2828419.html? Utm_source = tuicool & utm_medium = referral

(Headquarters share

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.