Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Analog of spring @Controller annotation in Java config

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?

like image 627
Kivan Avatar asked Mar 15 '16 13:03

Kivan


People also ask

What is the use of @controller annotation in Java?

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.

What is the annotation used for Java configuration?

You can use the @Bean annotation in a @Configuration -annotated or in a @Component -annotated class.

Which of these are used in Java based configuration @import bean config?

@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.

What is @configuration in Java?

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.


1 Answers

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?

like image 111
mh-dev Avatar answered Nov 15 '22 05:11

mh-dev