Spring Injection not working if i use “implements”

2019-09-04 08:35发布

This was my previous SO question Spring Injection not working in different service class

@Service("securityService")
@Transactional

    public class SecurityService implements UserDetailsService {

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

     @Autowired
     public RegistrationDAO registrationDAO;


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

In above code registrationDAO is not properly injected and give null pointer exception but Now i have found that if i remove implements from class then it works like below

@Service("securityService")
@Transactional

    public class SecurityService {

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

     @Autowired
     public RegistrationDAO registrationDAO;


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

I need to use that interface to use spring security authentication , so what should i do

Stack trace

enter code here
  java.lang.NullPointerException
com.vaannila.dao.RegistrationDAOimpl.findUserByID(RegistrationDAOimpl.java:63)
com.vaannila.service.SecurityService.loadUserByUsername(SecurityService.java:68)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

xml file

<?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"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <!-- Activates various annotations to be detected in bean classes -->


    <!-- Scans the classpath for annotated components that will be auto-registered as Spring beans.
     For example @Controller and @Service. Make sure to set the correct base-package-->
     <bean id="userService" class="com.vaannila.service.UserServiceImpl" />

    <bean id="userValidator" class="com.vaannila.validator.UserValidator" />

     <bean id="userDAO" class="com.vaannila.dao.UserDAO" />
     <bean id="registrationDAO" class="com.vaannila.dao.RegistrationDAO" />

    <!-- Configures the annotation-driven Spring MVC Controller programming model.
    Note that, with Spring 3.0, this tag works in Servlet MVC only!  -->


</beans>

1条回答
祖国的老花朵
2楼-- · 2019-09-04 09:00

I'm relatively new to Spring as well, but when I had this problem and it didn't work, I added a set method after the autowired, like this:

@Autowired
public RegistrationDAO registrationDAO;
public void setRegistrationDAO(RegistrationDAO registrationDAO) {
    this.registrationDAO = registrationDAO;
}

That's what worked for me, and I use it for the same thing, the DAOs that are declared in your servlet container xml file. In some cases I tried it without the setter and it did not work.

Good luck!

查看更多
登录 后发表回答