2009/8/19 zhaoyi <[email protected]> > > Hi, > Is there any other method instead of using constructor injecting? I > have some objects need to be injected which I don't want to put them > in constructor. >
not that I know of - the injector needs to have an instance of the object in order to inject it, but you only get back an instance from Java once its constructor has completed you might want to read these links: http://martinfowler.com/articles/injection.html#ConstructorVersusSetterInjection http://googletesting.blogspot.com/2009/02/constructor-injection-vs-setter.html any particular reason why you don't want to pass objects into the constructor, but still want them available inside it? On Aug 19, 7:18 am, Stuart McCulloch <[email protected]> wrote: > > 2009/8/19 zhaoyi <[email protected]> > > > > > > > > > > > > > Sorry, the code is: > > > > > import javax.swing.JPanel; > > > > > import com.google.inject.AbstractModule; > > > import com.google.inject.Guice; > > > import com.google.inject.Inject; > > > import com.google.inject.Injector; > > > import com.google.inject.name.Named; > > > import com.google.inject.name.Names; > > > > > public class AnnotationDemo3 { > > > @Inject > > > @Named("panel") > > > public JPanel panel=null; > > > > > public AnnotationDemo3(){ > > > System.out.println(panel); > > > } > > > > > public JPanel getPanel() { > > > return panel; > > > } > > > > > public void setPanel(JPanel panel) { > > > this.panel = panel; > > > } > > > > > public static void main(String[] args) { > > > Injector injector = Guice.createInjector(new > > > AbstractModule() { > > > @Override > > > protected void configure() { > > > > > bind(JPanel.class).annotatedWith(Names.named("panel")).to( > > > MyPanel.class); > > > } > > > }); > > > AnnotationDemo3 demo3 = > > > injector.getInstance(AnnotationDemo3.class); > > > System.out.println(demo3.panel); > > > } > > > } > > > > > class MyPanel extends JPanel { > > > > > } > > > > FYI, constructor injection goes something like this... > > > > public class AnnotationDemo3 { > > > > private JPanel panel; > > > > @Inject > > public AnnotationDemo3(@Named("panel") JPanel panel) { > > this.panel = panel; > > System.out.println(panel); > > } > > > > // etc... > > > > > > > > > On Aug 19, 7:00 am, Stuart McCulloch <[email protected]> wrote: > > > > 2009/8/19 zhaoyi <[email protected]> > > > > > > > Below is my code. The class AnnotationDemo3 has a panel field which > > > > > will be injected by annotation. The output of this program is: > > > > > > > null > > > > > demo.bindinganontation.MyPanel[, > > > > > > 0,0,0x0,invalid,layout=java.awt.FlowLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=] > > > > > > > In the constructor, the printf will print a null value which means > the > > > > > panel field doesn't have been created at this time. How can I let > it > > > > > creates the field at the very beginning time of the create created. > > > > > > I don't see any code here, but it sounds like you need to use > constructor > > > > injection: > > > > > > http://code.google.com/p/google-guice/wiki/Injections > > > > > > thanks. > > > > -- > > Cheers, Stuart > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "google-guice" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/google-guice?hl=en -~----------~----~----~----~------~----~------~--~---
