如果我使用“实现",则Spring Injection无法正常工作在班上 [英] Spring Injection not working if i use "implements" in class

查看:62
本文介绍了如果我使用“实现",则Spring Injection无法正常工作在班上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我以前的问题 Spring Injection在不同的服务类中不起作用

@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并给出了空指针异常,但是现在我发现,如果我从类中删除了工具,则其工作方式如下

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

我需要使用该接口来使用spring安全认证,所以我应该怎么做

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

堆栈跟踪

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>

推荐答案

我对Spring也比较陌生,但是当我遇到此问题并且它不起作用时,我在自动装配后添加了set方法,例如这个:

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

这就是对我有用的方法,我将它用于同一件事,即在您的servlet容器xml文件中声明的DAO.在某些情况下,我在没有二传手的情况下尝试了该方法,但该方法不起作用.

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.

祝你好运!

这篇关于如果我使用“实现",则Spring Injection无法正常工作在班上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆