I’ll let my code talk for me, first, here’s my root-context.xml:
<context:component-scan base-package="it.trew.prove" />
<bean id="usersDao" class="it.trew.prove.model.dao.UsersDao" />
<bean id="usersService" class="it.trew.prove.server.services.UsersServiceImpl" />
Some of my users dao:
public class UsersDao extends ObjectifyDao<User> {
protected UsersDao(Class<User> clazz) {
super(User.class);
}
static {
ObjectifyService.register(User.class);
}
}
And my users service (implementation):
public class UsersServiceImpl implements UsersService {
private final UsersDao usersDao;
@Autowired
public UsersServiceImpl(UsersDao usersDao) {
this.usersDao = usersDao;
}
@Override
public List<User> listUsers() {
return usersDao.list();
}
@Override
public void saveUser(User user) {
usersDao.add(user);
}
}
Now my log is:
AVVERTENZA: Nested in
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name ‘usersController’ defined in file
[/home/fabio/stsworkspace/TestGAE/target/TestGAE-1.0-SNAPSHOT/WEB-INF/classes/it/trew/prove/web/UsersController.class]: Unsatisfied dependency expressed through constructor argument with
index 0 of type [it.trew.prove.server.services.UsersService]: : Error
creating bean with name ‘usersService’ defined in ServletContext
resource [/WEB-INF/spring/root-context.xml]: Unsatisfied dependency
expressed through constructor argument with index 0 of type
[it.trew.prove.model.dao.UsersDao]: : Error creating bean with name
‘usersDao’ defined in ServletContext resource
[/WEB-INF/spring/root-context.xml]: Instantiation of bean failed;
nested exception is
org.springframework.beans.BeanInstantiationException: Could not
instantiate bean class [it.trew.prove.model.dao.UsersDao]: No default
constructor found; nested exception is
java.security.PrivilegedActionException:
java.lang.NoSuchMethodException:
it.trew.prove.model.dao.UsersDao.(); nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name ‘usersDao’ defined in ServletContext resource
[/WEB-INF/spring/root-context.xml]: Instantiation of bean failed;
nested exception is
org.springframework.beans.BeanInstantiationException: Could not
instantiate bean class [it.trew.prove.model.dao.UsersDao]: No default
constructor found; nested exception is
java.security.PrivilegedActionException:
java.lang.NoSuchMethodException:
it.trew.prove.model.dao.UsersDao.(); nested exception is
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name ‘usersService’ defined in ServletContext
resource [/WEB-INF/spring/root-context.xml]: Unsatisfied dependency
expressed through constructor argument with index 0 of type
[it.trew.prove.model.dao.UsersDao]: : Error creating bean with name
‘usersDao’ defined in ServletContext resource
[/WEB-INF/spring/root-context.xml]: Instantiation of bean failed;
nested exception is
org.springframework.beans.BeanInstantiationException: Could not
instantiate bean class [it.trew.prove.model.dao.UsersDao]: No default
constructor found; nested exception is
java.security.PrivilegedActionException:
java.lang.NoSuchMethodException:
it.trew.prove.model.dao.UsersDao.(); nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name ‘usersDao’ defined in ServletContext resource
[/WEB-INF/spring/root-context.xml]: Instantiation of bean failed;
nested exception is
org.springframework.beans.BeanInstantiationException: Could not
instantiate bean class [it.trew.prove.model.dao.UsersDao]: No default
constructor found; nested exception is
java.security.PrivilegedActionException:
java.lang.NoSuchMethodException:
it.trew.prove.model.dao.UsersDao.():
java.lang.NoSuchMethodException:
it.trew.prove.model.dao.UsersDao.()
So… in your honest opinion, how can I change my code to make it work correctly?
- Sorry for being so verbose 🙂 –
Add the constructor argument to the configuration file
or much better, remove the useless paramter from the UserDao constructor!
and add
autowire="constructor"for the user service bean declaration.<bean id="usersService" class="it.trew.prove.server.services.UsersServiceImpl" autowire="constructor"/>