0

I have a project where im using the annotation @Autowired. I got some endpoints, services and respositories. Beside that I wanted to create a javax.servlet.Filter to put a filter in front of it. Everything works, except the filter.

My code is like this:

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

Anybody has an idea why userservice and tokenservice in filter are null?

If more info is needed just tell me. Thanks in advance!

ProCx
  • 199
  • 5
  • 18

2 Answers2

3

It is because that object is not initialized via Spring. In order to get Spring autowire your beans for you you need to (super simple version):

  • instantiate the autowired bean via Spring
  • instansiate the bean you are wiring beans into via Spring.

In your case the second condition is not fulfilled.

A more detailed explanation is available here:

DelegatingFilterProxy explained

Community
  • 1
  • 1
Gergely Bacso
  • 14,243
  • 2
  • 44
  • 64
0

The answer is indeed DelegatingFilterProxy. (thanks Gergely Basco)..

My filter in web.xml looks like this and it works:

  <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>
ProCx
  • 199
  • 5
  • 18