Home » Eclipse Projects » Eclipse 4 » EclipseContextFactory Inject my own classes
EclipseContextFactory Inject my own classes [message #1061535] |
Sun, 02 June 2013 09:57  |
Eclipse User |
|
|
|
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 16:06   |
Eclipse User |
|
|
|
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:57Hello,
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 #1061911 is a reply to message #1061815] |
Tue, 04 June 2013 12:30   |
Eclipse User |
|
|
|
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 
Mateusz Malinowski wrote on Tue, 04 June 2013 10:27Maybe 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).
|
|
| |
Goto Forum:
Current Time: Tue Jul 22 18:15:46 EDT 2025
Powered by FUDForum. Page generated in 0.03893 seconds
|