In Spring DI I love to use @Bean with factory method in @Configuration class instead of using @Component annotation explicitly. Is there a way to do the same with @Controller annotation for Spring Web? In another words, could I declare controller via factory method in some @Configuration class instead of explicit annotation?
The @Controller annotation indicates that a particular class serves the role of a controller. Spring Controller annotation is typically used in combination with annotated handler methods based on the @RequestMapping annotation. It can be applied to classes only. It's used to mark a class as a web request handler.
You can use the @Bean annotation in a @Configuration -annotated or in a @Component -annotated class.
@Configuration and @Bean annotation. Using @Configuration annotation indicates that Spring IoC container can use it as a source of Beans definitions. Using the @Bean tells Spring that method will return an object which should be registered as a bean in Spring application context. You can define @Configuration as below.
A Configuration object is responsible for specifying which LoginModules should be used for a particular application, and in what order the LoginModules should be invoked.
Thats not possible since @Controller can only be placed on types. But more important are the methods. I assume you have multiple methods with @RequestMethod annotations. You can place as much methods as you like in this controller with differnet paths. Which should end up in around the same thing as you want?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With