e4 rcp @inject 注入自己的对象

原创
2017/07/11 16:21
阅读数 76
package redisclient.context.func;

import org.eclipse.e4.core.contexts.ContextFunction;
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
import org.eclipse.e4.core.contexts.IContextFunction;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.ui.model.application.MApplication;
import org.osgi.service.component.annotations.Component;

import redisclient.context.bean.Bean;

@Component(name="redis.services.bean", service=IContextFunction.class,
property="service.context.key=redisclient.context.bean.Bean")
public class TestContextFunction extends ContextFunction {
	@Override
	public Object compute(IEclipseContext context, String contextKey) {
		
		Bean bean =  ContextInjectionFactory.make(Bean.class, context);
		
		 MApplication app = context.get(MApplication.class);
	     IEclipseContext appCtx = app.getContext();
	     appCtx.set(Bean.class, bean);
		
		return bean;
	}

}

e4 中实现@inject  Bean bean 需要实现contextFunction类,需要注意的是service.context.key='类的全路径',如果注解不生效,添加配置文件,同时在manifest中加入Service-Component: OSGI-INF/service.xml

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="Bean">
   <implementation class="redisclient.context.func.TestContextFunction"/>
    <property name="service.context.key" type="String"  value="redisclient.context.bean.Bean"/>
   <service>
      <provide interface="org.eclipse.e4.core.contexts.IContextFunction"/>
   </service>
</scr:component>

 

    

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部