Arefe
Arefe

Reputation: 12397

Spring filter as Java annotation based bean

I have a Spring filter in the XML format provided below,

    <filter>
    <filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

I load it as an XML file in the top of the class. Will it be possible to write Java instead of the XML definition?

Upvotes: 0

Views: 414

Answers (1)

Mykola Yashchenko
Mykola Yashchenko

Reputation: 5361

In case of Spring you should define a class that extends AbstractDispatcherServletInitializer and add following lines to it

@Override
protected Filter[] getServletFilters() {
    return new Filter[] {
        new OpenEntityManagerInViewFilter();
    };
}

You can get more information about filters in Java code from Spring Docs https://docs.spring.io/spring/docs/4.1.1.RELEASE/spring-framework-reference/html/mvc.html#mvc-container-config

Upvotes: 1

Related Questions