spring 错误 error creating bean with name

问题描述

spring 错误 error creating bean with name

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService' defined in class path resource [beans.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.zxspring.service.UserService]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.zxspring.service.UserService.()
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:883)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:839)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:440)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
at com.zxspring.service.UserServiceTest.testadd(UserServiceTest.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.zxspring.service.UserService]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.zxspring.service.UserService.()
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:58)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:877)
... 38 more
Caused by: java.lang.NoSuchMethodException: com.zxspring.service.UserService.()
at java.lang.Class.getConstructor0(Class.java:2706)
at java.lang.Class.getDeclaredConstructor(Class.java:1985)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:54)
... 39 more

解决方案

beans.xml
<?xml version="1.0" encoding="UTF-8"?>
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-2.5.xsd">

<property name="userDAO" ref="u" />
</bean>

解决方案二:

package com.zxspring.service;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zxspring.model.User;

public class UserServiceTest {
@Test
public void testadd() throws Exception{
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");

    UserService service = (UserService)ctx.getBean("userService");

    User u = new User();
    u.setUsername("xiaohao");
    u.setPassword("xiaohao");

    service.add(u);
}

}

解决方案三:

package com.zxspring.service;
import com.zxspring.dao.UserDao;
import com.zxspring.model.User;

public class UserService {

private UserDao userDAO;
public void add(User user){
    userDAO.save(user);
}

public UserDao getUserDAO() {
    return userDAO;
}

public void setUserDAO(UserDao userDAO) {
    this.userDAO = userDAO;
}

public UserService(UserDao userDAO){
    super();
    this.userDAO = userDAO;
}

}

解决方案四:

package com.zxspring.model;

public class User {

private String username;
private String password;

public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username = username;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}

}

解决方案五:

package com.zxspring.dao.impl;

import com.zxspring.dao.UserDao;
import com.zxspring.model.User;

public class UserDaoImpl implements UserDao {
public void save(User user) {
System.out.println("user save sucess!!!");
}
}

解决方案六:

package com.zxspring.dao;

import com.zxspring.model.User;

public interface UserDao {
public void save(User user);
}

解决方案七:

解决方案八:

检查userserivce在spring的配置中怎么配置的。

解决方案九:

package com.zxspring.dao.impl;

import com.zxspring.dao.UserDao;
import com.zxspring.model.User;

public class UserDaoImpl implements UserDao {
public void save(User user) {
System.out.println("user save sucess!!!

时间: 2024-09-12 08:44:52

spring 错误 error creating bean with name的相关文章

Spring中的经典疑问: Error creating bean with name &amp;#039;urlMapping&amp;#039; ...

问题描述 正在学习spring的多动作控制器,然而配置完成运行后,出现了这样的错误信息,我估计很多老鸟见过,如下: Error creating bean with name 'urlMapping' defined in ServletContext resource [/WEB-INF/classes/applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.

spring mvc-service层加入@Tractional 后报错error creating bean

问题描述 service层加入@Tractional 后报错error creating bean 我在一个web项目中想加入事务,是采用spring mvc+hibernate的框架配置文件中已经加入 <!-- 配置Hibernate事务管理器 --> <bean id=""transactionManager"" class=""org.springframework.orm.hibernate4.HibernateTran

spring cloud 报错Error creating bean with name &amp;#39;hystrixCommandAspect&amp;#39; ,解决方案

  spring cloud 升级到最新版 后,报错: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hystrixCommandAspect' defined in class path resource [org/springframework/cloud/netflix/hystrix/HystrixCircuitBreakerConfiguration.cla

Error creating bean with name &amp;#039;dataSource&amp;#039; defined in ServletContext resource

问题描述 struts-config.xml配置信息<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.d

Error creating bean with name &amp;#39;sessionFactory&amp;#39; defined in file

在hibernate和spring整合时,出现错误如下: Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in file [G:\workspace\s_hibernate\src\applicationContext.xml]: Invocation of

ssh 框架问题 能显示页面,但是出现Error creating bean with n

问题描述 ssh 框架问题 能显示页面,但是出现Error creating bean with n 严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException: Error crea

Error creating bean with name &amp;#39;org.springframework.validation.beanvalidation.LocalValidatorFactory

Error creating bean with name 'org.springframework.validation.beanvalidation.LocalValidatorFactoryBean#0' Caused by: javax.validation.ValidationException: Unable to create a Configuration, because no Bean Validation provider could be found. Add a pro

Error creating bean with name &amp;#39;sessionFactory&amp;#39; defined in ServletContext resource [/WEB-INF/applica

 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is java.io.FileNotFoun

s2sh-【求助】关于S2SH整合遇到的Error creating bean with name &amp;amp;#39;sessionFactory&amp;amp;#39;

问题描述 [求助]关于S2SH整合遇到的Error creating bean with name 'sessionFactory' 这是生成的applicationContext.xml配置: <?xml version="1.0" encoding="UTF-8"?> xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.