Some comments inline with the code below:
> GreeterDefaultConstructor() {
This constructor isn't public, it is package protected. Guice won't be
able to invoke this constructor.
>
> //What goes in here?????????????????????
Usually default constructors are used to setup an object's initial
state to know values. Therefore, you could set Displayer and Name to
well known default values if you like. Otherwise, you'll need to use
the @Inject to tell Guice to Inject the Displayer and Name into the
constructor.
The purpose of the default constructor is merely so that Guice can
instantiate the class. If you want to inject these dependencies do this:
@Inject
public GreeterDefaultConstructor(Displayer displayer, Name name) { ... }
Otherwise, you will need to either inject these dependencies using
public method or not inject the class at all and just use Guice to
instantiate the class.
>
> void sayHello() {
> System.out.println("Default Constructor Injection");
> System.out.println("-----------------------------");
> displayer.display("Hello "+ name.display("Sumeet"));
> System.out.println("\n");
> }
This will probably fail since name is null.
-bp
Hi All,
I am very new to GUICE. I was just going through the
JavaOnGuice pdf, and had a small doubt
about constructor injection.......i mean if u dont annotate the
constructor with @Inject, you need to
provide a default no arguments constructor. Could someone please give
an example describing the
usage of the default constructor and how will Guice be able to inject
the dependency without the
@Inject annotation.................
This is the example...........
/*
An example of guice Default Constructor Injection
*/
package com;
import com.google.inject.Inject;
public class GreeterDefaultConstructor {
Displayer displayer;
Name name;
GreeterDefaultConstructor() {
//What goes in here?????????????????????
}
void sayHello() {
System.out.println("Default Constructor Injection");
System.out.println("-----------------------------");
displayer.display("Hello "+ name.display("Sumeet"));
System.out.println("\n");
}
}
Thanks in advance...............