问题
How do I merge the two controllers below with different annotations into one controller? I am trying to set up a Spring Boot app that uses AngularJS on the front end with MySQL-based Spring Security authentication on the back end.
Towards this end, I have been studying the code in this example which links AngularJS with Spring Boot Security and authentication from server-side config and from this other example, which uses MySQL-based authentication to a Spring Boot backend of an app that uses Thymeleaf as the front end. Both example apps are up and running in my devbox, but now I need to merge the back ends of both apps so that the /user url pattern is available for backend authentication by the front-end app, while also causing authentication to be done using the dataSource bean from the MySQL backend example.
The problem is that the controller in one app uses @SpringBootApplication and @Controller annotations, while the controller in the other app uses @Configuration, @EnableAutoConfiguration, and @ComponentScan annotations. How do I reconcile these annotations so that one controller can contain code that is currently segregated across both apps?
Here are the annotations for the controller in the first app:
@SpringBootApplication
@Controller
public class UiApplication {
//lots of code
}
And here are the annotations for the controller in the second app:
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application extends WebMvcConfigurerAdapter {
//lots of code
}
回答1:
@SpringBootApplication is actually a meta annotation, which consists of @EnableAutoConfiguration, @Configuration and @ComponentScan. So you can just make your UiApplication extend WebMvcConfigurerAdapter and copy all code from Application to UiApplication (of course check if you then have duplicate beans and fix that appropriately).
来源:https://stackoverflow.com/questions/34443843/how-do-i-merge-these-two-spring-boot-controllers