【问题标题】:Spring Injection not working if i use "implements" in class如果我在课堂上使用“工具”,Spring Injection 不起作用
【发布时间】:2012-04-12 05:24:08
【问题描述】:

这是我之前的 SO 问题 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";
      }

在上面的代码中,registrationDAO 没有正确注入并给出空指针异常,但现在我发现如果我从类中删除实现,那么它的工作方式如下所示

@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";
      }

我需要使用那个接口才能使用spring安全认证,我该怎么办

堆栈跟踪

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文件

<?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>

【问题讨论】:

  • 请给出 NullPointerException 的堆栈跟踪,我们无法为您猜测 :)
  • 我们需要查看您的 spring 配置 XML。
  • 我有点困惑 - 你的文字说你得到了 NullPointerException,但你已经发布了来自 HibernateException 的堆栈跟踪。你的 spring 配置中没有任何东西可以初始化 Hibernate,所以我认为我们仍然缺少一些信息。
  • 我改变了堆栈,实际上我修改了@Transactional,所以我得到了那个错误,现在我已经放回了mon安全服务并再次得到空指针错误
  • 看起来自动连线工作正常,因为registrationDAOImpl正在连线,但NPE发生在第63行的registrationDAOImpl.findByUserId()中。

标签: java dependency-injection spring-security


【解决方案1】:

我对 Spring 也比较陌生,但是当我遇到这个问题并且它不起作用时,我在 autowired 之后添加了一个 set 方法,如下所示:

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

这对我有用,我将它用于同样的事情,即在您的 servlet 容器 xml 文件中声明的 DAO。在某些情况下,我在没有设置器的情况下尝试了它,但它不起作用。

祝你好运!

【讨论】:

    猜你喜欢
    • 2013-08-03
    • 2016-11-15
    • 1970-01-01
    • 2016-03-23
    • 2012-08-08
    • 1970-01-01
    • 2015-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多