OSGI Blueprint入门之八

       Blueprint除了组装bean,osgi服务引用等的DI(IOC)功能之外,还可通过各种命名空间(namespace)来扩展。在《Blueprint入门之六》中,我们就用过一个与ConfigAdmin相关的命名空间(http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0)。

 
      除了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:
 
  
  
  
  
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <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"> 
  3.   <persistence-unit name=" com.ponder.myPU " transaction-type="RESOURCE_LOCAL"> 
  4.     <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> 
  5.     <non-jta-data-source> 
  6. osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/myDS) 
  7. </non-jta-data-source> 
  8.     <class>com.ponder.*</class> 
  9.     <properties> 
  10.       <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/> 
  11.     </properties> 
  12.   </persistence-unit> 
  13. </persistence> 
 
   注:以上persistence.xml中的(osgi.jndi.service.name=jdbc/myDS)是将注册到jndi的jdbc的数据源注入进来。
 
   注入EntityManagerFactory:
 
  
  
  
  
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:jpa="http://aries.apache.org/xmlns/jpa/v1.1.0" > 
  3.  
  4.   <bean id="processor1" class="com.ponder.processor"> 
  5.         <jpa:unit property="emf" unitname="com.ponder.myPU" /> 
  6.     </bean>   
  7. ... .... 
  8. </blueprint> 
 
 
   注入EntityManager:
 
  
  
  
  
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:jpa="http://aries.apache.org/xmlns/jpa/v1.1.0" > 
  3.  <bean id="processor1" class="com.ponder.processor"> 
  4.         <jpa:context property="em" unitname="com.ponder.myPU" /> 
  5.     </bean> 
  6. ... .... 
  7. </blueprint> 
 
 
    在事务(Transaction)方面,Blueprint通过命名空间(xmlns:tx="http://aries.apache.org/xmlns/transactions/v1.0.0)支持声明式事务(Declaractive Transaction):
 
  
  
  
  
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" 
  3. xmlns:jpa="http://aries.apache.org/xmlns/jpa/v1.0.0" 
  4. xmlns:tx="http://aries.apache.org/xmlns/transactions/v1.0.0"> 
  5. <bean id="processor1" class="com.ponder.processor"> 
  6.   <jpa:context property="em" unitname="com.ponder.myPU" /> 
  7.   <tx:transaction method="*" value="Required" /> 
  8. </bean> 
  9. ... ... 
  10. </blueprint> 
 

你可能感兴趣的:(java,DI,jpa,IOC,osgi,blueprint)