osgi persistence.xml读取database.properties文件属性
database.properties
user=sa
password=root
...
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence
xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
<properties>
</properties>
</persistence-unit>
</persistence>
代码:
public class Connection{
....
Bundle thisBundle = FrameworkUtil.getBundle(Connection.class);
BundleContext context = thisBundle.getBundleContext();
ServiceReference<PersistenceProvider> serviceReference = (ServiceReference<PersistenceProvider>) context.getServiceReference(PersistenceProvider.class.getName());
Properties properties = new Properties();
File file = new File("cfg/database.properties");
InputStream in = new FileInputStream(file);
properties.load(in);
String user = properties.getProperty("user");
Map<String,String> map = new HashMap<String,String>();
map.put("hibernate.connection.username", user);
.....
//通过map设置属性值
context.getService(serviceReference).createEntityManagerFactory("test", map);
}
...