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>