Home > other >  Spring - when the filter is registered @ Bean and @ the difference between Component?
Spring - when the filter is registered @ Bean and @ the difference between Component?

Time:10-30

I was in the Spring the Boot applications using custom filters, there seems to be two methods can register filter.


- & gt; Register the filter using the @ Bean

@ Bean
Public Filter AuthenticationFilter () {
Return new AuthenticationFilter ();
}


- & gt; Anotate the Filter using the @ Component

@ Component
Public class AuthenticationFilter implements Filter {}

I am very confused that what is the difference and why should I use one rather than another?

CodePudding user response:

It depends largely on personal preference.
Using the @ Component need to enable the Component scanning. Some people don't like using the Component scanning, because they find it difficult to determine where your beans from. @ Bean method is used to declare all content can avoid this kind of situation, but the price is to write (a bit) more Java configuration.
Using the @ the Bean's another reason you might not be able to control the Filter of the source, that you can't use @ Component on the comments, so using the @ Bean method statement it is your only choice.

CodePudding user response:

Maven springboot interceptor I really don't understand ah, do you have any clear to give directions
  • Related