google / google/guice

Servlet Module handles abstract filter classes incorrectly

Open
#1,041 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
12.7k
Forks
1.7k
Avg merge
11m
Merged PRs (30d)
2

Description

Hi,

I am using Guice (and Guice Servlets) 4.1.0. I have a server system based on Guice. One of my filters is an abstract class that extends `Filter` :

```
public abstract class FilterBase implements Filter { .. }
```

The application binds it to one of several possible implementations according to some configuration. This is an example of a concrete filter:

```
@Singleton
public class FilterImpl extends FilterBase { ... }

public class FilterModule extends AbstractModule {
@Override
protected void configure() {
bind(FilterBase.class).to(FilterImpl.class);
}
}
```

I route requests through my abstract filter class, assuming the injector will bind it correctly much like any other injected dependency:

```
public class MyServletModule extends ServletModule {
@Override
protected void configureServlets() {
filter("/*").through(FilterBase.class);
}
}
```

However **this does not work**. Guice keeps trying to use `FilterBase.class` as the concrete binding, first complaining about a missing `@Singleton` and later claiming that no implementation was found after I added `@Singleton` to `FilterBase`. The only way to make it work is to use `.through(FilterImpl.class)` directly, which somewhat contradicts the actual concept of Guice.

This happens only in ServletModule filter bindings, other areas in my application can use inject `FilterBase` correctly with the same injector.

Please help,
_thank you_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.