Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » EclipseContextFactory Inject my own classes
EclipseContextFactory Inject my own classes [message #1061535] Sun, 02 June 2013 13:57 Go to next message
AIT YAHIA Idir is currently offline AIT YAHIA IdirFriend
Messages: 29
Registered: April 2013
Junior Member
Hello,

i am trying to inject some of my own classes, like a Modelprovider to be reused by injection in whole of my E4 application,

At first sight, it looks simple concept, but it does not work, I read almost all the posts on this forum that talks about EclipseContextFactory but I can not implement a simple case,

in this Blog tickets from Lars Vogel http://www.vogella.com/blog/2012/02/29/eclipse-4-is-now-a-full-dependency-injection-container-with-creatable/

it describes exactly what I want to do, but i try this without sucess, I still retrieves a null object

this is a sample code of my implementation

// this is my injected class

@Creatable  @Singleton
public class SatModelProvider {
	
    private  List<Satellite> Satellites;
    private  List<Transponder> Transponders;
	
@Inject
public SatModelProvider(IEclipseContext context){ 

  System.out.println("the class SatModelProvider is instantiated correctly ");	
 }

} 


here I am trying to retrieve the créated instance

public class LoadFormFileHandler {

	
    @Inject SatModelProvider satData;
	
	@Execute
	public void execute(IEclipseContext context,Shell shell) {
          
            System.out.println(satData.getSatellites().get(0).getName());

       }

the retreived object satData is null,

how do I check if my class is included well in the context, and how I should proceed to retreive the existing instance.

most articles talk about using the EclipseContectFactory that I can not also implement (I've tried in the constructor with the code below)

// this is my constructor
@Inject
public SatModelProvider(IEclipseContext context) 

{
	
   ContextInjectionFactory.make(SatModelProvider.class, context);	
	
}



I thank you in advance, I know the subject has been treated several times in this forum, but I was not successful implementation, if I manage to make this scenario work, I understand once the DI mechanisme in Eclipse 4.
Re: EclipseContextFactory Inject my own classes [message #1061555 is a reply to message #1061535] Sun, 02 June 2013 20:06 Go to previous messageGo to next message
Erdal Karaca is currently offline Erdal KaracaFriend
Messages: 756
Registered: July 2009
Senior Member
I think your assumption that calling ContextInjectionFactory.make() in SatModelProvider's constructor is wrong (which shouldn't work anyways due to recursion).

You have to provide an instance of SatModelProvider OUTSIDE SatModelProvider and put it in your root IEclipseContext. You could do this in SatModelProvider's bundle activator, for example. Or you could use a model processor or a model addon.

AIT YAHIA Idir wrote on Sun, 02 June 2013 15:57
Hello,

i am trying to inject some of my own classes, like a Modelprovider to be reused by injection in whole of my E4 application,

At first sight, it looks simple concept, but it does not work, I read almost all the posts on this forum that talks about EclipseContextFactory but I can not implement a simple case,

in this Blog tickets from Lars Vogel http://www.vogella.com/blog/2012/02/29/eclipse-4-is-now-a-full-dependency-injection-container-with-creatable/

it describes exactly what I want to do, but i try this without sucess, I still retrieves a null object

this is a sample code of my implementation

// this is my injected class

@Creatable  @Singleton
public class SatModelProvider {
	
    private  List<Satellite> Satellites;
    private  List<Transponder> Transponders;
	
@Inject
public SatModelProvider(IEclipseContext context){ 

  System.out.println("the class SatModelProvider is instantiated correctly ");	
 }

} 


here I am trying to retrieve the créated instance

public class LoadFormFileHandler {

	
    @Inject SatModelProvider satData;
	
	@Execute
	public void execute(IEclipseContext context,Shell shell) {
          
            System.out.println(satData.getSatellites().get(0).getName());

       }

the retreived object satData is null,

how do I check if my class is included well in the context, and how I should proceed to retreive the existing instance.

most articles talk about using the EclipseContectFactory that I can not also implement (I've tried in the constructor with the code below)

// this is my constructor
@Inject
public SatModelProvider(IEclipseContext context) 

{
	
   ContextInjectionFactory.make(SatModelProvider.class, context);	
	
}



I thank you in advance, I know the subject has been treated several times in this forum, but I was not successful implementation, if I manage to make this scenario work, I understand once the DI mechanisme in Eclipse 4.

Re: EclipseContextFactory Inject my own classes [message #1061559 is a reply to message #1061555] Sun, 02 June 2013 20:57 Go to previous messageGo to next message
AIT YAHIA Idir is currently offline AIT YAHIA IdirFriend
Messages: 29
Registered: April 2013
Junior Member
thanks for your response, i will check this by using an addon,as you said I have noticed the recursion, I have a loop exception.

Normally annotation @Creatable is sufficient to instantiate and put my Class in the context. My class is instantiated with the annotation (I checked with a System.out.println in the constructor), but it is not injected (i get null object with (@Inject SatModelProvider satData;)

[Updated on: Sun, 02 June 2013 20:58]

Report message to a moderator

Re: EclipseContextFactory Inject my own classes [message #1061815 is a reply to message #1061535] Tue, 04 June 2013 08:27 Go to previous messageGo to next message
Mateusz Malinowski is currently offline Mateusz MalinowskiFriend
Messages: 23
Registered: March 2013
Junior Member
Maybe my tip will help you.

I created a class:

@Creatable
public class MyClass
{
    private IEventBroker broker;

    @Inject
    public MyClass(IEventBroker broker)
    {
        this.broker = broker;
    }

    // some other methods...
}


Then in the same plugin in the addon I call this:
@PostConstruct
public void postConstruct(MApplication app, IEventBroker broker) 
{
    IEclipseContext context = app.getContext();
    ContextInjectionFactory.inject(MyClass.class, context);
    context.set(MyClass.class, new MyClass(broker));
}


So, I get the context from main application, I inject MyClass there and then I set MyClass in the context (providing the proper argument for a constructor, which in this case could be injected in @PostConstruct).
Now whenever I inject MyClass within different plugins, it is always the same instance of MyClass (it was crucial for not to create multiple instances).
Re: EclipseContextFactory Inject my own classes [message #1061911 is a reply to message #1061815] Tue, 04 June 2013 16:30 Go to previous messageGo to next message
Erdal Karaca is currently offline Erdal KaracaFriend
Messages: 756
Registered: July 2009
Senior Member
Mateusz,
I think you should "simplify" your code:

    IEclipseContext context = app.getContext();
    MyClass myClassInstance = ContextInjectionFactory.make(MyClass.class, context);
    context.set(MyClass.class, myClassInstance);


Injecting things into MyClass.class should have no effect Smile

Mateusz Malinowski wrote on Tue, 04 June 2013 10:27
Maybe my tip will help you.

I created a class:

@Creatable
public class MyClass
{
    private IEventBroker broker;

    @Inject
    public MyClass(IEventBroker broker)
    {
        this.broker = broker;
    }

    // some other methods...
}


Then in the same plugin in the addon I call this:
@PostConstruct
public void postConstruct(MApplication app, IEventBroker broker) 
{
    IEclipseContext context = app.getContext();
    ContextInjectionFactory.inject(MyClass.class, context);
    context.set(MyClass.class, new MyClass(broker));
}


So, I get the context from main application, I inject MyClass there and then I set MyClass in the context (providing the proper argument for a constructor, which in this case could be injected in @PostConstruct).
Now whenever I inject MyClass within different plugins, it is always the same instance of MyClass (it was crucial for not to create multiple instances).

Re: EclipseContextFactory Inject my own classes [message #1061970 is a reply to message #1061535] Wed, 05 June 2013 07:57 Go to previous message
Mateusz Malinowski is currently offline Mateusz MalinowskiFriend
Messages: 23
Registered: March 2013
Junior Member
Erdal,

Thanks for a tip! I still would say that you need somehow provide arguments for a constructor, but in this particular case, the IEventBroker is in the application context so it can be injected.

Nevertheless, you are right and it works! Smile
Previous Topic:Integrating JDT and org.eclipse.ant in Eclipse IDE for C/C++ Developers
Next Topic:Dependency Injection in eclipse plugins (not RCP apps)
Goto Forum:
  


Current Time: Tue Dec 23 03:54:44 GMT 2014

Powered by FUDForum. Page generated in 0.01964 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software