创建名称为' myEmf'的bean时出错 - javax.naming.NameNotFoundException:myPersistenceUnit

时间:2016-08-29 22:23:01

标签: java spring hibernate spring-mvc jndi

我试图在DAO类中注入EntityManager。

但是我在服务器初始化期间遇到此错误(WildFly 10.0.0)。

  

18:35:57,318 ERROR [org.springframework.web.context.ContextLoader]   (ServerService线程池 - 60)上下文初始化失败:   org.springframework.beans.factory.BeanCreationException:错误   创建名为' myEmf'的bean:调用init方法失败;   嵌套异常是javax.naming.NameNotFoundException:   myPersistenceUnit - 服务   jboss.naming.context.java.myPersistenceUnit at   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)     在   org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:305)     在   org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)     在   org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)     在   org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)     在   org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:753)     在   org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:835)     在   org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)     在   org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)     在   org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)     在   org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)     在   io.undertow.servlet.core.ApplicationListeners.contextInitialized(ApplicationListeners.java:187)     在   io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:198)     在   org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:100)     在   org.wildfly.extension.undertow.deployment.UndertowDeploymentService $ 1.run(UndertowDeploymentService.java:82)     在   java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:511)     在java.util.concurrent.FutureTask.run(FutureTask.java:266)at   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)     在   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:617)     在java.lang.Thread.run(Thread.java:745)at   org.jboss.threads.JBossThread.run(JBossThread.java:320)引起:   javax.naming.NameNotFoundException:myPersistenceUnit - service   jboss.naming.context.java.myPersistenceUnit at   org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:106)     在org.jboss.as.naming.NamingContext.lookup(NamingContext.java:207)     在   org.jboss.as.naming.InitialContext $ DefaultInitialContext.lookup(InitialContext.java:235)     在org.jboss.as.naming.NamingContext.lookup(NamingContext.java:193)     在org.jboss.as.naming.NamingContext.lookup(NamingContext.java:189)     在javax.naming.InitialContext.lookup(InitialContext.java:417)at   javax.naming.InitialContext.lookup(InitialContext.java:417)at   org.springframework.jndi.JndiTemplate $ 1.doInContext(JndiTemplate.java:155)     在   org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:87)     在   org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:152)     在   org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:179)     在   org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:104)     在   org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:106)     在   org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java:231)     在   org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:217)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)     ......还有22个

根context.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/jee
    http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config/>

    <context:component-scan base-package="com.company.project"></context:component-scan>

    <jee:jndi-lookup id="myEmf" jndi-name="myPersistenceUnit"/>

</beans>

的persistence.xml

<persistence 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"
        version="2.0">
    <persistence-unit name="myPersistenceUnit" transaction-type="JTA">

      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jta-data-source>java:/MyDS</jta-data-source>

      <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServer2008Dialect" />
            <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform" />
      </properties>

    </persistence-unit>

</persistence>

servlet的context.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <annotation-driven />

    <resources mapping="/resources/**" location="/resources/" />

    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    </beans:bean>

    <beans:import resource="controllers.xml" />

    <task:annotation-driven />

</beans:beans>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
  </servlet>
  <servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file></welcome-file>
  </welcome-file-list>
</web-app>

的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.company</groupId>
    <artifactId>project</artifactId>
    <version>0.1.0</version>

    <properties>
        <java.version>1.8</java.version>
        <org.springframework-version>4.2.2.RELEASE</org.springframework-version>
    </properties>

    <dependencies>

        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework-version}</version>
            <exclusions>
                <!-- Exclude Commons Logging in favor of SLF4j -->
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                 </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-core</artifactId>
         <version>${org.springframework-version}</version>
        </dependency>

        <!-- Servlet -->
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-servlet-api</artifactId>
            <version>7.0.30</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>jstl-api</artifactId>
            <version>1.2</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.servlet</groupId>
                    <artifactId>servlet-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>jstl-impl</artifactId>
            <version>1.2</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.servlet</groupId>
                    <artifactId>servlet-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>


        <dependency>
           <groupId>org.hibernate</groupId>
           <artifactId>hibernate-core</artifactId>
           <version>5.2.1.Final</version>
        </dependency>

    </dependencies>

    <build>
      <plugins>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
            </configuration>
        </plugin>
      </plugins>
    </build>

</project>

UserDAO.java

@Repository
public class UserDAO {

    @PersistenceContext
    private EntityManager entityManager;

    ...

}

出了什么问题?

2 个答案:

答案 0 :(得分:0)

在您的root-context.xml中,引用错误。

jndi-name="myPersistenceUnit"

正确的是

jndi-name="java:/MyDS"

您的服务器知道第二个,而不是第一个。

答案 1 :(得分:0)

我解决了将bean添加到root-context.xml:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="myPersistenceUnit" />
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>