FAQ(23):Error creating bean with name '***' defined in class path resource [applicationContex

本文记录了一次在Spring框架中由于未提供无参构造方法而导致的Bean创建失败问题。问题出现在PersonDao Bean的实例化过程中,由于缺少默认构造器而引发异常。通过对问题的定位与分析,最终确定解决方案为添加无参构造器。

这是p命名空间使用碰到的Bug;

下面是Log:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'PersonDao1' defined in class path resource [applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.spring.IOC.test.PersonDao]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.spring.IOC.test.PersonDao.<init>()
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1076)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1021)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
	at com.spring.IOC.test.PersonDaoTest.test(PersonDaoTest.java:13)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.spring.IOC.test.PersonDao]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.spring.IOC.test.PersonDao.<init>()
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:85)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1069)
	... 36 more
Caused by: java.lang.NoSuchMethodException: com.spring.IOC.test.PersonDao.<init>()
	at java.lang.Class.getConstructor0(Unknown Source)
	at java.lang.Class.getDeclaredConstructor(Unknown Source)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:80)
	... 37 more


解决:

一个javaBean的Dao实例:PersonDao的无参构造参数没有加进去!!
改:

	public PersonDao(Person person){
		this.person = person;
	}
	
	
	public Person getPerson() {
		return person;
	}

所以,在以后的Dao层代码编写,最好把构造器都加上去。

在 Spring 框架里,出现 `Error creating bean with name 'processorMetrics' defined in class path resource` 错误,一般是由配置问题、依赖问题或者 Bean 初始化逻辑有误等因素造成的。下面是一些常见的解决办法: ### 1. 检查配置文件 要保证 `processorMetrics` Bean 的配置无误,不管是使用 Java 配置类,还是 XML 配置文件。 **Java 配置类示例**: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { @Bean public ProcessorMetrics processorMetrics() { return new ProcessorMetrics(); } } ``` **XML 配置文件示例**: ```xml <bean id="processorMetrics" class="com.example.ProcessorMetrics"/> ``` ### 2. 检查依赖注入 查看 `processorMetrics` Bean 是否有依赖其他 Bean,并且确保这些依赖的 Bean 已正确配置和初始化。 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class ProcessorMetrics { private AnotherBean anotherBean; @Autowired public ProcessorMetrics(AnotherBean anotherBean) { this.anotherBean = anotherBean; } // 其他代码 } ``` ### 3. 查看构造函数和初始化方法 `processorMetrics` Bean 的构造函数或者初始化方法也许存在异常,要保证这些方法里的逻辑正确。 ```java import javax.annotation.PostConstruct; @Component public class ProcessorMetrics { public ProcessorMetrics() { // 构造函数逻辑 } @PostConstruct public void init() { // 初始化逻辑 } } ``` ### 4. 启用 `debug` 模式 在启动应用时启用 `debug` 模式,这样能获取更详细的错误信息,有助于定位问题。 ```plaintext java -jar your-application.jar --debug ``` ### 5. 检查类路径和依赖 确认 `ProcessorMetrics` 类在类路径中,并且所需的依赖都已正确添加到项目里。 参考 Spring 框架中出现类似错误信息的问题,例如 `Error creating bean with name 'dataSource'`、 `Error creating bean with name 'XX'` 等,解决思路通常是从 Bean 的定义、配置、依赖等方面着手排查问题 [^1][^2][^3][^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

后台技术汇

对你的帮助,是对我的最好鼓励。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值