Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java config for spring interceptor where interceptor is using autowired spring beans

I want to add spring mvc interceptor as part of Java config. I already have a xml based config for this but I am trying to move to a Java config. For interceptors, I know that it can be done like this from the spring documentation-

@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LocaleInterceptor());
    }

}

But my interceptor is using a spring bean autowired into it like follows-

public class LocaleInterceptor extends HandlerInterceptorAdaptor {

    @Autowired
    ISomeService someService;

    ...
}

The SomeService class looks like follows-

@Service
public class SomeService implements ISomeService {

   ...
}

I am using annotations like @Service for scanning the beans and have not specified them in the configuration class as @Bean

As my understanding, since java config uses new for creating the object, spring will not automatically inject the dependencies into it.

How can I add the interceptors like this as part of the java config?

like image 319
user3565529 Avatar asked Apr 28 '14 19:04

user3565529


People also ask

How do you call an interceptor from a spring boot?

To work with interceptor, you need to create @Component class that supports it and it should implement the HandlerInterceptor interface. preHandle() method − This is used to perform operations before sending the request to the controller. This method should return true to return the response to the client.

Where is bean configuration done in spring?

@Configuration & @Bean Annotations.

How do you make a spring interceptor for spring Restful web services?

@Configuration public class AppConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry. addInterceptor(new HttpInterceptor()); // registry. addInterceptor(new HttpInterceptor()). addPathPatterns("/account/login"); you can add specific end point as well. } }

What is the difference between filter and interceptor in spring?

Filters can modify inbound and outbound requests and responses including modification of headers, entity and other request/response parameters. Interceptors are used primarily for modification of entity input and output streams. You can use interceptors for example to zip and unzip output and input entity streams.


3 Answers

Just do the following:

@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    @Bean
    LocaleInterceptor localInterceptor() {
         return new LocalInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeInterceptor());
    }

}

Of course LocaleInterceptor needs to be configured as a Spring bean somewhere (XML, Java Config or using annotations) in order for the relevant field of WebConfig to get injected.

The documentation for general customization of Spring's MVC configuration can be found here, and specifically for Interceptors see this section

like image 121
geoand Avatar answered Oct 06 '22 19:10

geoand


When you handle the object creation for yourself like in:

registry.addInterceptor(new LocaleInterceptor());

there is no way the Spring container can manage that object for you and therefore make the necessary injection into your LocaleInterceptor.

Another way that could be more convenient for your situation, is to declare the managed @Bean in the @Configuration and use the method directly, like so:

@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    @Bean
    public LocaleInterceptor localeInterceptor() {
        return new LocaleInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor( localeInterceptor() );
    }
}
like image 19
José Andias Avatar answered Oct 06 '22 17:10

José Andias


Try to inject your service as a constructor parameter. It is simple.

@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

   @Autowired
   ISomeService someService;

   @Override
   public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LocaleInterceptor(someService));
   }

}

Then reconfigure your interceptor,

public class LocaleInterceptor extends HandlerInterceptorAdaptor {


     private final ISomeService someService;

     public LocaleInterceptor(ISomeService someService) {
         this.someService = someService;
     }


}

Cheers !

like image 14
oko Avatar answered Oct 06 '22 19:10

oko