1

我有一个注入的无状态 EJB

@PersistenceUnit 
private EntityManagerFactory factory

我的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="ejbPU-ro" transaction-type="RESOURCE_LOCAL">
        <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
        <non-jta-data-source>mySQLDataSource</non-jta-data-source>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="openjpa.jdbc.DBDictionary" value="mysql"/>
            <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(foreignKeys=true"/>
            <property name="openjpa.jdbc.SchemaFactory" value="native(foreignKeys=true)"/>
            <property name="openjpa.Log" value="DefaultLevel=INFO,SQL=INFO"/>
        </properties>
    </persistence-unit>
</persistence>

当我使用单元测试时:

Properties p = new Properties();
p.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.core.LocalInitialContextFactory");
p.setProperty(Context.PROVIDER_URL, "http://127.0.0.1:4204/openejb/ejb");
InitialContext initialContext = new InitialContext(p);

还定义了openejb.homeVM arg 和单元测试完美运行。

部署到 TomEE 时,我在my.war/WEB-INF/lib/my.jar/META-INF.

conf/tomee.xml我有:

<JndiProvider id="prov" type="javax.naming.InitialContext">
    java.naming.factory.initial = org.apache.openejb.core.LocalInitialContextFactory
    java.naming.provider.url = http://127.0.0.1:4204/openejb/ejb
</JndiProvider>

和:

<Resource id="mySQLDataSource" type="DataSource">
    JdbcDriver          com.mysql.jdbc.Driver
    JdbcUrl             jdbc:mysql://127.0.0.1:3306/mydb
    UserName            root
    Password        
        JtaManaged      false
        DefaultAutoCommit   false
</Resource>

日志看起来不错,但是当我运行代码时,注入的工厂为空。

有任何想法吗?

4

1 回答 1

1

几样东西:

1)您的初始上下文:

p.setProperty(Context.INITIAL_CONTEXT_FACTORY,"org.apache.openejb.core.LocalInitialContextFactory");
p.setProperty(Context.PROVIDER_URL, "http://127.0.0.1:4204/openejb/ejb"); <- useless since you are in embedded mode

2)你的 web.xml 有 metadata-complete=true 所以扫描被禁止(删除它)

3)你通常不需要 org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet (见http://svn.apache.org/repos/asf/tomee/tomee/trunk/examples/rest-example-with-application / ) - 或使用 JAXRS 技巧通过 web.xml 声明它(IIRC javax.ws.rs.Application 作为 init 参数)

4)不确定您的 ajax url 是否正确(在 tomee@ 上提供的示例中)

终于得到了一个不为空的电动势(因为它在这里,所以在主干上测试过;)

于 2013-02-17T20:40:32.347 回答