Spring Injection not working if i use “implements” in class

戏子无情 提交于 2020-01-16 18:32:18

问题


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:


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!



来源:https://stackoverflow.com/questions/5440335/spring-injection-not-working-if-i-use-implements-in-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!