gpt4 book ai didi

java - CCTOR 中的 Spring java.lang.NoClassDefFoundError

转载 作者:行者123 更新时间:2023-11-30 07:02:25 24 4
gpt4 key购买 nike

我正在使用 Spring 和 Hibernate 使用以下 bean 定义 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="hibernateConfiguration" class="pl.wicia.projector.database.DBConfiguration" scope="prototype">
<constructor-arg name="path" value="pl/wicia/projector/database/cfg.xml"/>
</bean>

<bean id="sessionFactory" class="pl.wicia.projector.database.HibernateSessionFactory" scope="prototype">
<constructor-arg name="dbConfig" ref="hibernateConfiguration"/>
</bean>

</beans>

现在,我想使用构造函数参数注入(inject)来创建数据库配置包装器:

package pl.wicia.projector.database;

import org.hibernate.cfg.Configuration;

public class DBConfiguration {

private Configuration configuration;

public DBConfiguration(String path){
this.configuration = new Configuration(); <--- this throws Exsception
this.configuration.configure(path);
}

public Configuration getConfiguration() {
return configuration;
}
}

这一行产生以下异常:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateConfiguration' defined in class path resource [pl/wicia/projector/spring/db_beans.xml]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [pl.wicia.projector.database.DBConfiguration]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: javax/transaction/SystemException
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:279)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1148)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1051)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:325)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1076)
at pl.wicia.projector.main.Projector.main(Projector.java:23)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [pl.wicia.projector.database.DBConfiguration]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: javax/transaction/SystemException
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:154)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:122)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:271)
... 8 more
Caused by: java.lang.NoClassDefFoundError: javax/transaction/SystemException
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at org.jboss.logging.Logger$1.run(Logger.java:2554)
at java.security.AccessController.doPrivileged(Native Method)
at org.jboss.logging.Logger.getMessageLogger(Logger.java:2529)
at org.jboss.logging.Logger.getMessageLogger(Logger.java:2516)
at org.hibernate.internal.CoreLogging.messageLogger(CoreLogging.java:28)
at org.hibernate.internal.CoreLogging.messageLogger(CoreLogging.java:24)
at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:86)
at pl.wicia.projector.database.DBConfiguration.<init>(DBConfiguration.java:22)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142)
... 10 more
Caused by: java.lang.ClassNotFoundException: javax.transaction.SystemException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 25 more

这就是我创建 Bean 的方式:

ApplicationContext context = new ClassPathXmlApplicationContext("pl/wicia/projector/spring/db_beans.xml");
DBConfiguration config = (DBConfiguration)context.getBean("hibernateConfiguration");

知道发生了什么吗? :)

最佳答案

您的运行时类路径上没有javax.transaction.SystemException。这可以通过添加来修复

<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>

如果您使用 Maven,则依赖于您的 pom.xml。但是,如果您缺少这样的基本类,您的应用程序中的依赖关系可能会出现更严重的问题。即使您修复了它,您也可能会处理导致抛出 javax.transaction.SystemException 的实际情况。

关于java - CCTOR 中的 Spring java.lang.NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40685119/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com