gpt4 book ai didi

java - 导出 jar 文件后 Spring NoSuchBeanDefinitationException 抛出

转载 作者:行者123 更新时间:2023-12-01 12:42:45 25 4
gpt4 key购买 nike

我有 spring 项目,我可以在导出 jar 文件之前从 spring 上下文中获取 spring bean。我导出 jar 文件并在 cmd 上运行该 jar 文件。我得到了NoSuchBeanDefinitationException 。该项目找不到 spring bean。我用过<context:component-scan>在 spring 配置文件中。我想知道问题是什么?

编辑

在 bean 中

package com.example.userprofile;

@Service("UserProfileManager")
@Transactional
public class UserProfileManagerImpl implements UserProfileManager{

@Autowired
private UserProfileDao userProfileDao;

在spring配置文件中

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

<tx:annotation-driven transaction-manager="transactionManager" />
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory" />


<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql://localhost:3309/test"
p:username="root" p:password="root" />

<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>hibernate.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>

当我使用 JBoss IDE 测试该项目时,它运行得很好。但是当我将项目导出到jar文件并在cmd上运行时。我收到以下错误。 enter image description here

here是我的项目 zip 文件。抱歉更新晚了。

最佳答案

虽然问题陈述太宽泛,无法提供具体答案,但您可以执行以下操作:

  1. 打开您的 jar 文件,确保您的类包含所有注释。
  2. 检查打包的 jar 以确保您拥有所有需要的库。
  3. 在命令行中检查您的类路径,确保您引用的是库的路径。
  4. 如果您使用构建脚本,请检查依赖库的范围并查看是否正在导出所有需要的 jar。

如果没有任何内容跳出,请粘贴整个堆栈跟踪,而不仅仅是代码片段以及 jar 的结构,以便我们也能看到与您相同的大图。

关于java - 导出 jar 文件后 Spring NoSuchBeanDefinitationException 抛出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24948050/

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