springspring-bootspring-securityservlet-filtersspring-filter

How does DelegatingFilterProxy know which filter to call to delegate the doFilter call?


From Spring's Java docs, web.xml will usually contain a DelegatingFilterProxy definition, with the specified filter-name corresponding to a bean name in Spring's root application context.

Does DelegatingFilterProxy use reflection to know the name of its object and use this name to find the name of the filterchainproxy bean and thus delegate the request to the correct filterchain class?

(But then won't two beans with the same name result into conflict?)


Solution

  • Does DelegtaingFilterProxy use reflection to know the name of it's object and use this name to find the name of the filterchainproxy bean and thus delegate the request to the correct filterchain class ?

    Not really. It just query Spring ApplicationContext to return a bean which the name is the same as what configured in <filter-name> , which finally boils down to calling :

    Filter filter = applicationContext.getBean(targetBeanName, Filter.class);
    

    Behind the scenes , it does not need to use reflection to get a bean by the bean name as internally Spring will index all Singleton beans by its bean name in a map.So looking up a bean by its name is just a key look up from this map.

    But then won't two beans with the same name result into conflict

    You are right. That 's why if you define two beans with the same name, Spring will throw exception and it cannot start.