John
John

Reputation:

Spring Injection not working in different service class

I have class

@Service("registrationService")
@Transactional
public class RegistrationService {

@Resource(name="registrationDAO")
 public RegistrationDAO registrationDAO;

In the Controller i can access registrationService and registrationDAO with no problem.

I have another class

@Service("securityService")
public class SecurityService implements UserDetailsService {

 protected static Logger logger = Logger.getLogger("service");

 @Resource(name="registrationDAO")
 public RegistrationDAO registrationDAO;


  public String test(){
        logger.debug(registrationDAO.findUserByID(1) );
    return "Testing";
  }

Now if i call test function in controller then it gives null pointer exception on registrationDAO

Upvotes: 2

Views: 3319

Answers (1)

Johan Sjöberg
Johan Sjöberg

Reputation: 49237

All your @Service, @Repository, @Controller, @Component (etc.) annotated class must be spring-managed for autowiring to work. Make sure they are picked up by spring classpath scanning:

<context:component-scan base-package="com.company" />

In some cases @Autowire, which does autowiring by type, can be useful to avoid the name argument you're supplying with the @Resource.

Upvotes: 3

Related Questions