java - Autowired 在自定义注销处理程序和自定义用户详细信息中给出 null

标签 java spring spring-mvc spring-security autowired

我的 Autowiring 在 Controller 和 Controller 下面的所有子类中工作正常。但是当我尝试从 Spring Security 模块 Autowiring 任何 bean(使用注释)时,例如通过 UserDetails 或通过我的自定义注销处理程序。

我已阅读并找到了两种解决方法 1. 删除 2. 将一些代码移至父context.xml。但看起来这对我没有帮助。

我的 web.xml ...

<servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- The definition of the Root Spring Container shared by all Servlets 
        and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/database-context.xml,
            /WEB-INF/spring/application-security.xml

        </param-value>
    </context-param>

我的 servlet-context 文件具有以下内容:....

    <mvc:resources mapping="/resources/**"  location="/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
        in the /WEB-INF/views directory -->
    <beans:bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

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

    <beans:bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <beans:property name="basename" value="classpath:messages"></beans:property>
        <beans:property name="defaultEncoding" value="UTF-8"></beans:property>
    </beans:bean>

请帮忙..

最佳答案

这表明您的 Autowiring 注释尚未激活。 包括

    <context:annotation-config>

在您的database-context.xml、application-security.xml 文件中。

关于java - Autowired 在自定义注销处理程序和自定义用户详细信息中给出 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22884096/

相关文章:

java - Spring JDBCTemplate ResultSetExtractor 和 Spring Batch ItemReader 之间的区别

hibernate - 在Spring MVC中,在何处捕获数据库异常

java - 从 ArrayList 中删除对象而不(隐式)循环遍历它

java - 当包含 ajax 关闭事件监听器时,Primefaces 对话框不会重新打开

java - 使用构造函数注入(inject)模式对 spring boot 进行集成测试

java - 创建 Map 并将其传递给请求范围的属性

java - 使用Spring MVC如何获取用户提交后的表单数据?

java - 我需要使用在不同类的类内的方法中创建的多个字符串

java - 在 "10 println "行中查找注释的正则表达式是什么 Testing if then"//Test Comment

java - 如何在 spring-boot 中设置 hibernate.format_sql?