org.osgi.framework.bootdelegation=sun.*,com.sun.* org.osgi.framework.system.packages.extra=sun.misc import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class JdbcService { private static final String URL = "jdbc:mysql://localhost:3306/mydatabase"; private static final String USERNAME = "username"; private static final String PASSWORD = "password"; public Connection getConnection() throws SQLException { return DriverManager.getConnection(URL, USERNAME, PASSWORD); } } import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Service; @Component @Service public class JdbcService { } import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; import java.sql.Connection; import java.sql.SQLException; public class OsgiPlugin implements BundleActivator { private JdbcService jdbcService; public void start(BundleContext bundleContext) throws Exception { ServiceReference<JdbcService> serviceReference = bundleContext.getServiceReference(JdbcService.class); jdbcService = bundleContext.getService(serviceReference); Connection connection = jdbcService.getConnection(); bundleContext.ungetService(serviceReference); } public void stop(BundleContext bundleContext) throws Exception { } } Bundle-Activator: com.example.osgi.OsgiPlugin Import-Package: org.osgi.framework, com.example.jdbc import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Service; @Component @Service public class JdbcService { private static final String URL = "jdbc:mysql://localhost:3306/mydatabase"; private static final String USERNAME = "username"; private static final String PASSWORD = "password"; public Connection getConnection() throws SQLException { return DriverManager.getConnection(URL, USERNAME, PASSWORD); } } import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; import java.sql.Connection; import java.sql.SQLException; public class OsgiPlugin implements BundleActivator { private JdbcService jdbcService; public void start(BundleContext bundleContext) throws Exception { ServiceReference<JdbcService> serviceReference = bundleContext.getServiceReference(JdbcService.class); jdbcService = bundleContext.getService(serviceReference); Connection connection = jdbcService.getConnection(); bundleContext.ungetService(serviceReference); } public void stop(BundleContext bundleContext) throws Exception { } } Bundle-Activator: com.example.osgi.OsgiPlugin Import-Package: org.osgi.framework, com.example.jdbc


上一篇:
下一篇:
切换中文