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>()
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>
本文分析了Spring框架中因UserService类缺少默认构造方法而导致的Bean创建异常问题,并提供了修改建议及示例代码。
7632

被折叠的 条评论
为什么被折叠?



