-1

I created a Filter and definitely it will be called every request. I have this database call: dbService.getNeeded(). How can I make this call once only and use it in the entire calling of filter?

public class MyFilter extends OncePerRequestFilter {
    @Autowired
    private DBService dbService;

    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
         boolean isNeeded = dbService.getNeeded();
         if (isNeeded) {
               // do something
         }
    }

}
  • 2
    What do you mean by 'entire calling of filter'? Do you want to invoke this only once across all the requests? If not, what is the issue are getting? – KnockingHeads Jun 16 '21 at 06:27
  • @Ashish this filter will be called on every request. meaning i will be calling the `dbService.getNeeded()` every request. I want to call that method only once. How can i do that? –  Jun 16 '21 at 07:11
  • You say that ```dbService.getNeeded``` will be called for each request which definitely it will. And, then you say, call it only once. Don't you think its contradictory statement. Even if your filter is executed again, in case its a ```response.sendRedirect``` or ```requestDispatcher.forward```, then only the filter will be called once i.e. when the first request arrived. And, that because you have OncePerRequestFilter as parent. What else do you need? – KnockingHeads Jun 16 '21 at 07:17
  • If the response for this boolean of ```dbService.getNeeded``` is the same for all requests, then why not have a singleton class and initialize it there and reference its value directly from that class or having it as a static property of your filter. – KnockingHeads Jun 16 '21 at 07:19
  • @Ashish how can i make it static property? –  Jun 16 '21 at 07:22
  • I have added my answer below. I think I have got you correctly. My +1 if that helped. – KnockingHeads Jun 16 '21 at 07:34

1 Answers1

0

My advice would be to set the value of isNeeded in application.properties file rather than your DBService. That way, it will always be a single value for your complete application until its running.

You can use @Value annotation into your filter and have a value in src/main/resources/application.properties as:

application.properties

isNeeded=true

Updated Code with @Value annotation

public class MyFilter extends OncePerRequestFilter {
    @Value("${isNeeded}")
    private boolean isNeeded;

    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
         
         if (isNeeded) {
               // do something
         }
    }

}

There are other options as well to read properties from other properties file as well. Read here:

How to read data from java properties file using Spring Boot

KnockingHeads
  • 1,569
  • 1
  • 22
  • 42