为什么在这种特定情况下自动装配为空?

时间:2015-12-17 19:32:21

标签: java rest jax-rs

我有一个使用注释@Autowired的项目。我有一些端点,服务和存储库。除此之外,我想创建一个javax.servlet.Filter来在它前面放置一个过滤器。一切都有效,除了过滤器。

我的代码是这样的:

(滤波器)

package com.mysportslife.web.auth;

<import>

@Provider
@Component
public class TokenFilter implements Filter {

@Autowired
private UserService userService;

@Autowired
private TokenService tokenService;
....
....

(TokenService)

package com.mysportslife.web.auth;
<import>

@Service
public class TokenService {

@Autowired
private UserService userService;
....
....

(UserService)

package com.mysportslife.services;

<import>


@Service
public class UserService {

@Autowired
private UserRepository userRepository;
....
....

(web.xml)中

<web-app xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>mysportslife</display-name>
<description>Backend for MySportslife</description>

<servlet>
    <servlet-name>jersey-servlet</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.mysportslife.web</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>jersey-servlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<filter>
    <filter-name>TokenFilter</filter-name>
    <filter-class>com.mysportslife.web.auth.TokenFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>TokenFilter</filter-name>
    <url-pattern>/rest/*</url-pattern>
</filter-mapping>

<!-- Algemene configuratie -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:ApplicationContext.xml
    </param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

(applicationContext.xml中)

<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"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<context:component-scan base-package="com.mysportslife"/>
<import resource="ApplicationContext-persistence.xml"/>

</beans>

任何人都知道为什么filterer中的userservice和tokenservice为空?

如果需要更多信息,请告诉我。提前谢谢!

2 个答案:

答案 0 :(得分:3)

这是因为该对象没有通过Spring初始化。为了让你的bean自动为你的bean自动化(超级简单的版本):

  • 通过Spring实现自动装配的bean
  • 通过Spring实现你将bean连接到的 bean。

在您的情况下,第二个条件未得到满足。

这里有更详细的解释:

DelegatingFilterProxy explained

答案 1 :(得分:0)

答案确实是DelegatingFilterProxy。 (感谢Gergely Basco)..

我在web.xml中的过滤器看起来像这样,它可以工作:

  <filter>
    <filter-name>tokenFilter</filter-name>
    <filter-class> org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>tokenFilter</filter-name>
    <url-pattern>/rest/*</url-pattern>
</filter-mapping>