本文主要是介绍【第一代服务注册形式】 - 将一个Bundle注册为服务,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、创建业务接口类及其实现类
- public interface Validator {
- public boolean validate(String username, String password) throws Exception;
- }
- public class UserValidatorImpl implements Validator {
- public boolean validate(String username, String password) throws Exception {
- if ( "admin" .equals(username) && "admin" .equals(password)){
- return true ;
- }
- return false ;
- }
- }
2、创建Bundle的激活器类
- public class UserValidatorActivator implements BundleActivator {
- private ServiceRegistration serviceRegistration;
- public void start(BundleContext context) throws Exception {
- Validator obj = new UserValidatorImpl();
- serviceRegistration = context.
- registerService(Validator.class .getName(), obj, props); //注册服务
- }
- public void stop(BundleContext context) throws Exception {
- serviceRegistration.unregister(); //卸载服务
- }
- }
3、MANIFEST.MF文件内容
- Manifest-Version: 1.0
- Bundle-ManifestVersion: 2
- Bundle-Name: UserValidatorBundle
- Bundle-SymbolicName: UserValidatorBundle
- Bundle-Version: 1.0.0
- Bundle-Activator: com.cjm.bundle.user.activator.UserValidatorActivator
- Bundle-Vendor: RIAWork
- Import-Package: org.osgi.framework;version = "1.3.0"
- Export-Package: com.cjm.bundle.user.service;version = "1.0.0"
4、打包Bundle后,执行install命令安装Bundle,执行start命令启动Bundle
5、可以执行services命令查看被注册的服务:
这篇关于【第一代服务注册形式】 - 将一个Bundle注册为服务的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!