我试图在DAO类中注入EntityManager。
但我在服务器初始化(WildFly 10.0.0)期间收到此错误。
18: 35:57318错误[org.springframework.web.context.ContextLoader](ServerService线程池--60)上下文初始化失败:org。springframework。豆。工厂BeanCreationException:创建名为“myEmf”的bean时出错:调用init方法失败;嵌套的异常是javax。命名。NameNotFoundException:myPersistenceUnit——服务jboss。命名。上下文Java语言组织中的myPersistenceUnit。springframework。豆。工厂支持AbstractAutowireCapableBeanFactory。org上的initializeBean(AbstractAutowireCapableBeanFactory.java:1578)。springframework。豆。工厂支持AbstractAutowireCapableBeanFactory。org上的doCreateBean(AbstractAutowireCapableBeanFactory.java:545)。springframework。豆。工厂支持AbstractAutowireCapableBeanFactory。位于org的createBean(AbstractAutowireCapableBeanFactory.java:482)。springframework。豆。工厂支持AbstractBeanFactory 1美元。org上的getObject(AbstractBeanFactory.java:305)。springframework。豆。工厂支持默认SingletonBeanRegistry。位于org的getSingleton(DefaultSingletonBeanRegistry.java:230)。springframework。豆。工厂支持AbstractBeanFactory。org上的doGetBean(AbstractBeanFactory.java:301)。springframework。豆。工厂支持AbstractBeanFactory。org上的getBean(AbstractBeanFactory.java:196)。springframework。豆。工厂支持DefaultListableBeanFactory。org上的预实例化单例(DefaultListableBeanFactory.java:753)。springframework。上下文支持AbstractApplicationContext。位于org的finishBeanFactoryInitialization(AbstractApplicationContext.java:835)。springframework。上下文支持AbstractApplicationContext。在org上刷新(AbstractApplicationContext.java:537)。springframework。网状物上下文上下文加载器。在org上配置和刷新WebApplicationContext(ContextLoader.java:446)。springframework。网状物上下文上下文加载器。位于org的initWebApplicationContext(ContextLoader.java:328)。springframework。网状物上下文ContextLoaderListener。contextInitialized(ContextLoaderListener.java:107)位于io。下拖。servlet。果心应用程序侦听器。contextInitialized(ApplicationListeners.java:187)位于io。下拖。servlet。果心DeploymentManagerImpl。部署(DeploymentManagerImpl.java:198)于org。野蝇。扩大下拖。部署。在部署服务下。位于org的startContext(UndertowDeploymentService.java:100)。野蝇。扩大下拖。部署。UndertowDeploymentService 1美元。在java上运行(UndertowDeploymentService.java:82)。util。同时发生的遗嘱执行人$RunnableAdapter。在java上调用(Executors.java:511)。util。同时发生的未来任务。在java上运行(FutureTask.java:266)。util。同时发生的线程池执行器。java上的runWorker(ThreadPoolExecutor.java:1142)。util。同时发生的ThreadPoolExecutor$工作者。在java上运行(ThreadPoolExecutor.java:617)。lang.Thread。在org上运行(Thread.java:745)。jboss。Thread。JBossThread。运行(JBossThread.java:320)的原因:javax。命名。NameNotFoundException:myPersistenceUnit——服务jboss。命名。上下文Java语言组织中的myPersistenceUnit。jboss。像命名。基于服务的名称存储。在org上查找(ServiceBasedNamingStore.java:106)。jboss。像命名。NamingContext。在org上查找(NamingContext.java:207)。jboss。像命名。InitialContext$DefaultInitialContext。在org上查找(InitialContext.java:235)。jboss。像命名。NamingContext。在org上查找(NamingContext.java:193)。jboss。像命名。NamingContext。在javax上查找(NamingContext.java:189)。命名。InitialContext。在javax上查找(InitialContext.java:417)。命名。InitialContext。在org上查找(InitialContext.java:417)。springframework。jndi。JndiTemplate$1。doInContext(JndiTemplate.java:155)位于org。springframework。jndi。JndiTemplate。在org上执行(jndemplate.java:87)。springframework。jndi。JndiTemplate。在org上查找(jndemplate.java:152)。springframework。jndi。JndiTemplate。在org上查找(JndiTemplate.java:179)。springframework。jndi。JndiLocatorSupport。在org上查找(JndiLocatorSupport.java:104)。springframework。jndi。JndiObjectLocator。在org上查找(JndiObjectLocator.java:106)。springframework。jndi。JndiObjectFactoryBean。org上的lookupWithFallback(JndiObjectFactoryBean.java:231)。springframework。jndi。JndiObjectFactoryBean。位于org的afterPropertiesSet(JndiObjectFactoryBean.java:217)。springframework。豆。工厂支持AbstractAutowireCapableBeanFactory。org上的invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)。springframework。豆。工厂支持AbstractAutowireCapableBeanFactory。initializeBean(AbstractAutowireCapableBeanFactory.java:1574)。。。22个以上
root-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>
坚持不懈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上下文。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>
网状物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>
用户DAO。Java语言
@Repository
public class UserDAO {
@PersistenceContext
private EntityManager entityManager;
...
}
发生了什么?
我解决了将bean添加到根上下文的问题。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"/>
在您的根上下文中。xml引用错误。
jndi-name="myPersistenceUnit"
正确的是
jndi-name="java:/MyDS"
您的服务器知道第二个,而不是第一个。
我有一个实体类InAppNotification。看起来像这样的java: 我使用JPA来处理数据库查询,这就是JPA接口的定义: 这是我application.properties的样子: 但是,当我试图在构建后通过运行 来打包应用程序时,我会遇到以下问题: 尝试调用不存在的方法。尝试从以下位置进行:javax.el.ELManager.getExpress sionWorks(ELManage
在将project从Spring Boot版本从1.2.3.release迁移到1.3.0.release之后,我已经开始得到以下异常。 创建类路径资源[org/springframework/boot/autoconfigure/admin/springapplicationadminjmxautoconfiguration.class]中定义的名为'Spring ApplicationAdmi
我遵循本教程将消息发送到azure服务队列:https://docs.microsoft.com/en-us/azure/java/spring-framework/configure-spring-boot-starter-java-app-with-azure-service-bus 到我现有的spring boot应用程序,但我得到以下错误: 用户类别: 控制器类: pom xml: 添加它
我试图将弹性搜索集成到spring-boot应用程序中,但我得到了这个*创建名为“client”的bean时出错*异常,不确定是什么地方出了问题,因为我之前找不到任何类似的线索...非常感谢你为大家指路。这是mu elasticsearch配置: 这是我的主要应用程序: 我的pom.xml: 这是te异常跟踪:
我有这些错误编译,我不知道我哪里错了,我只有这3个类
我正在尝试在spring boot应用程序中使用SQLite。但是应用程序不能创建下面的bean。 org.springframework.boot.autocigure.orm.jpa.hibernatejaconfiguration 我该怎么办?我在这个站点上搜索了相关的问题,但是找不到一个合适的。 如下所示。 4.0.0 org.springframework.Boot spring-boo