Spring报错: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [xxx]

Spring报错: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [xxx]

完整报错

00:00:00,056 ERROR ContextLoader:353 - Context initialization failed
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [xxx] for bean with name 'xxx' defined in class path resource [applicationContext.xml]; nested exception is java.lang.ClassNotFoundException: xxx
	at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1352)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:628)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:597)
	at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1445)
	at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:975)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:752)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
	at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)
	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)
	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5157)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5680)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1707)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1697)
	at java.util.concurrent.FutureTask.run(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: xxx
	at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1945)
	at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1788)
	at org.springframework.util.ClassUtils.forName(ClassUtils.java:250)
	at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:394)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1397)
	at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1344)
	... 19 more
七月 00, 0000 0:00:00 下午 org.apache.catalina.core.StandardContext listenerStart
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [xxx] for bean with name 'xxx' defined in class path resource [applicationContext.xml]; nested exception is java.lang.ClassNotFoundException: xxx
	at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1352)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:628)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:597)
	at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1445)
	at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:975)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:752)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
	at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)
	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)
	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5157)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5680)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1707)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1697)
	at java.util.concurrent.FutureTask.run(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: xxx
	at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1945)
	at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1788)
	at org.springframework.util.ClassUtils.forName(ClassUtils.java:250)
	at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:394)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1397)
	at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1344)
	... 19 more

常见原因

本文采用xml方式配置Bean

例:
配置文件

<bean id="testAction" class="cn.ssh.web.TestAction" scope="prototype">
	<property name="testService" ref="testService"></property>
</bean>
<bean id="testService" class="cn.ssh.service.TestService">
	<property name="testDao" ref="testDao"></property>
</bean>
<bean id="testDao" class="cn.ssh.dao.TestDao"></bean>

SSH常见写法

//TestAction.java
public class TestAction extends ActionSupport implements ModelDriven<Test> {
	// 调用服务层的资源,交给IOC创建
	TestService testService;
	public TestService getTestService() {
		return TestService;
	}
	public void setTestService(TestService testService) {
		this.testService = testService;
	}
}

//TestService.java
public class TestService {
	//dao的资源
	TestDao testDao;	
	public TestDao getTestDao() {
		return testDao;
	}
	public void setTestDao(TestDao testDao) {
		this.testDao = testDao;
	}
}

//TestDao.java
public class TestDao {
}

//Test.java
public class Test {
}
原因一:

拼写或对应错误

要注意两个对应
1.<property>中ref<bean>中id对应
ref与id对应
2.<property>中name类中的属性名对应
name与属性名对应

原因二:

缺少get或set方法
get、set方法
这里set方法不可或缺,get方法不写有时可行,但建议写上

对于 Spring Cloud 中的服务启动失败并报错 org.springframework.beans.factory.BeanDefinitionStoreException,这通常是由于 Spring 容器无法加载或解析 Bean 定义所致。这种异常通常有以下几种常见情况和解决方案: 1. 配置文件错误:检查配置文件(如 application.properties 或 application.yml)中的配置项是否正确,括配置项的格式、键名和键值是否正确。 2. 扫描错误:检查是否正确配置了扫描路径,确保 Spring 能够扫描到需要加载的 Bean。可以使用 @ComponentScan 注解或在配置文件中配置 component-scan 属性来指定扫描路径。 3. Bean 注解错误:检查被注解的类是否正确使用了 SpringBean 注解,如 @Component、@Service、@Repository 等。确保这些注解被正确应用在需要被 Spring 管理的类上。 4. 类路径错误:检查项目的类路径是否正确配置,确保所有需要加载的类都位于正确的路径下。 5. 依赖缺失或版本冲突:检查项目的依赖是否完整并且版本兼容。可以使用 Maven 或 Gradle 等构建工具来管理依赖,并确保所有依赖的版本符合要求。 6. 其他异常情况:如果以上方法都没有解决问题,可以尝试查看详细的异常堆栈信息,定位具体的错误原因。根据异常信息,可能需要进一步检查代码逻辑、配置文件或其他相关因素。 希望以上解决方案能帮助到你解决 Spring Cloud 服务启动失败的问题。如果问题仍然存在,请提供更多的错误信息或异常堆栈,以便更准确地定位问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值