Everything used in SpringDispatcherServlet is URL based, I donβt think you can do it with a controller.
You will need to use the Filter, look at the API here https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/filter/package-summary.html , you probably want to use OnePerRequestFilter.
public class MyFilter extends OncePerRequestFilter{ @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
you will need to add a filter in web.xml
<filter> <filter-name>requestFilter</filter-name> <filter-class>com.greg.MyFilter</filter-class> </filter> <filter-mapping> <filter-name>errorHandlerFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
Now the hacked bit, if you want to get Spring beans here, you can create a Bridge class in it with statics.
public class Bridge { private static PaymentService paymentService; public PaymentService getPaymentService() { return paymentService; } public void setPaymentService(PaymentService paymentService) { Bridge.paymentService = paymentService; } }
If you want to insert some Spring beans in this class
<bean id="paymentService" class="net.isban.example.service.PaymentService" /> <bean class="net.isban.example.util.Bridge"> <property name="paymentService" ref="paymentService" /> </bean>
Then in your filter (not Spring class).
PaymentService paymentService = new Bridge().getPaymentService();
Happy if someone shows me a less hacky way to do this.
source share