Default Constructor Injection Options

1,361 views
Skip to first unread message

sumeet

unread,
Jul 3, 2008, 5:01:13 AM7/3/08
to google-guice
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...............

Brian Pontarelli

unread,
Jul 3, 2008, 9:48:45 AM7/3/08
to google...@googlegroups.com
>

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

Stuart McCulloch

unread,
Jul 3, 2008, 10:24:17 AM7/3/08
to google...@googlegroups.com
2008/7/3 sumeet <sumeet....@gmail.com>:

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() {

as Brian says, this constructor should be marked public
 
               //What goes in here?????????????????????

anything you want that initializes the name and display
fields, you're really free to use any technique you like :)

if you want to use a default constructor, but also want
Guice to inject those fields then you can either mark
the fields with @Inject, or add some "setter" methods
for those fields, and mark them with @Inject.

however, note that if you use field/setter injection then
the fields won't be initialized until sometime after the
constructor has been called - so you have to be very
careful about when you use the fields in sub-classes.

HTH

       }

       void sayHello() {
               System.out.println("Default Constructor Injection");
               System.out.println("-----------------------------");
               displayer.display("Hello "+ name.display("Sumeet"));
               System.out.println("\n");
       }

}


Thanks in advance...............




--
Cheers, Stuart
Reply all
Reply to author
Forward
0 new messages