除了ConfigAdmin之外,我们还可以看到aries JPA container提供了JPA相关的blueprint命名空间(http://aries.apache.org/xmlns/jpa/v1.1.0) ,Aries JPA Container根据bundle中的persistence.xml构建持久化域单元(persistence domain unit)后,需要将相应的EntityManagerFactory或EntityManager注入到需要用到该持久化域的bean实例里,就可以使用这个命名空间。
persistence.xml:
<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name=" com.ponder.myPU " transaction-type="RESOURCE_LOCAL"> <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> <non-jta-data-source> osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/myDS) </non-jta-data-source> <class>com.ponder.*</class> <properties> <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/> </properties> </persistence-unit> </persistence>
注:以上persistence.xml中的(osgi.jndi.service.name=jdbc/myDS)是将注册到jndi的jdbc的数据源注入进来。
注入EntityManagerFactory:
<?xml version="1.0" encoding="UTF-8"?> <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:jpa="http://aries.apache.org/xmlns/jpa/v1.1.0" > <bean id="processor1" class="com.ponder.processor"> <jpa:unit property="emf" unitname="com.ponder.myPU" /> </bean> ... .... </blueprint>
注入EntityManager:
<?xml version="1.0" encoding="UTF-8"?> <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:jpa="http://aries.apache.org/xmlns/jpa/v1.1.0" > <bean id="processor1" class="com.ponder.processor"> <jpa:context property="em" unitname="com.ponder.myPU" /> </bean> ... .... </blueprint>
在事务(Transaction)方面,Blueprint通过命名空间(xmlns:tx="http://aries.apache.org/xmlns/transactions/v1.0.0)支持声明式事务(Declaractive Transaction):
<?xml version="1.0" encoding="UTF-8"?> <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:jpa="http://aries.apache.org/xmlns/jpa/v1.0.0" xmlns:tx="http://aries.apache.org/xmlns/transactions/v1.0.0"> <bean id="processor1" class="com.ponder.processor"> <jpa:context property="em" unitname="com.ponder.myPU" /> <tx:transaction method="*" value="Required" /> </bean> ... ... </blueprint>