1
votes

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 :) -
3

3 Answers

2
votes

Add the constructor argument to the configuration file

<bean id="usersDao" class="it.trew.prove.model.dao.UsersDao">
  <constructor value="it.trew.prove.model.dao.User" />
</bean>

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"/>

1
votes

No default constructor found; nested exception is java.security.PrivilegedActionException: java.lang.NoSuchMethodException:

Just add default constructor

0
votes

To get your @Autowire annotation to work you must add <context:annotation-config /> .E.g.,

<?xml version="1.0" encoding="UTF-8"?>
   <beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:annotation-config/>

</beans>