Could not instantiate bean class [com.bjsxt.service.UserService]: No default constructor found;

Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@46638f: defining beans [userDAO,userDAO2,userService]; root of factory hierarchy
Exception in thread "main" 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.bjsxt.service.UserService]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.bjsxt.service.UserService.<init>()
    ........................
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:880)
    at com.bjsxt.service.UserServiceTest.testAdd(UserServiceTest.java:18)
    at com.bjsxt.service.UserServiceTest.main(UserServiceTest.java:23)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.bjsxt.service.UserService]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.bjsxt.service.UserService.<init>()

   ......................

提示为没有构造方法


所有源码如下:

        <bean name="userDAO" class="com.bjsxt.dao.impl.UserDAOImpl">
		<property name="daoId" value="1"></property>
	</bean>
	
	<bean name="userDAO2" class="com.bjsxt.dao.impl.UserDAOImpl">
		<property name="daoId" value="2"></property>
	</bean>
		
	<bean id="userService" class="com.bjsxt.service.UserService" scope="prototype" autowire="byName">
		
	</bean>
注: byName ,通过属性的名字的方式查找 JavaBean 依赖的对象并为其注入。比如说类 Computer 有个属性 printer ,指定其 autowire 属性为 byName 后, Spring IoC 容器会在配置文件中查找 id/name 属性为 printer bean ,然后使用 Seter 方法为其注入。
    public class UserService {
	
	public UserService(UserDAO userDAO) {
		super();
		this.userDAO = userDAO;
	}

	private UserDAO userDAO = new UserDAOImpl();

	public UserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
	
	public void add(User u){
		this.userDAO.save(u);
	}	
}

public class UserDAOImpl implements UserDAO{

	private int daoId;
	
	public int getDaoId() {
		return daoId;
	}

	public void setDaoId(int daoId) {
		this.daoId = daoId;
	}
	
	public void save(User u) {
		System.out.println("User Saved");
	}
	
	@Override
	public String toString() {
		return "daoId="+daoId;
	}
}

	public void testAdd() {
		ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
		
		UserService service = (UserService) act.getBean("userService");
		System.out.println(service.getUserDAO());
	}

beans.xml

<bean name="userDAO" class="com.bjsxt.dao.impl.UserDAOImpl">
		<property name="daoId" value="1"></property>
	</bean>
	
	<bean name="userDAO2" class="com.bjsxt.dao.impl.UserDAOImpl">
		<property name="daoId" value="2"></property>
	</bean>
		
	<bean id="userService" class="com.bjsxt.service.UserService" scope="prototype" autowire="byName">
		
	</bean>


解决:

由于提示的是没有构造方法,因此添加一个构造方法即可,但是在源码中存在一个带参的构造方法,因此去掉下面代码即可

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

提示:

我们也可以把beans.xml中的代码写成如许:

<bean name="userDAO" class="com.bjsxt.dao.impl.UserDAOImpl">
		<property name="daoId" value="1"></property>
	</bean>
	
	<bean name="userDAO2" class="com.bjsxt.dao.impl.UserDAOImpl">
		<property name="daoId" value="2"></property>
	</bean>
		
	<bean id="userService" class="com.bjsxt.service.UserService" scope="prototype" autowire="byName">
		<property name="userDAO" ref="userDAO"></property>
	</bean>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值