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

4738 ワード

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.()
    ........................
    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.()
   ......................
構築方法がないことを示す
すべてのソースコードは次のとおりです.
        <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>