【问题标题】:How do I merge these two Spring Boot controllers?如何合并这两个 Spring Boot 控制器?
【发布时间】:2016-03-30 08:53:27
【问题描述】:

如何将下面带有不同注释的两个控制器合并到一个控制器中? 我正在尝试设置一个 Spring Boot 应用程序,该应用程序在前端使用 AngularJS,并在基于 MySQL 的 Spring Security 身份验证后端。

为此,我一直在研究the code in this example which links AngularJS with Spring Boot Security and authentication from server-side configthis other example, which uses MySQL-based authentication to a Spring Boot backend of an app that uses Thymeleaf as the front end. 这两个示例应用程序都已在我的devbox 中启动并运行,但现在我需要合并两个应用程序的后端,以便/user url 模式可用于前端应用程序的后端身份验证,同时还导致使用 M​​ySQL 后端示例中的 dataSource bean 完成身份验证。

问题是一个应用中的控制器使用@SpringBootApplication@Controller注解,而另一个应用中的控制器使用@Configuration@EnableAutoConfiguration@ComponentScan注解。 如何协调这些注释,以便一个控制器可以包含当前在两个应用程序之间隔离的代码?

这是第一个应用程序中控制器的注释:

@SpringBootApplication
@Controller
public class UiApplication {
    //lots of code
}

这是第二个应用程序中控制器的注释:

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application extends WebMvcConfigurerAdapter {
    //lots of code
}

【问题讨论】:

    标签: java spring spring-mvc spring-security spring-boot


    【解决方案1】:

    @SpringBootApplication实际上是一个元注解,由@EnableAutoConfiguration@Configuration@ComponentScan组成。所以你可以让你的UiApplication扩展WebMvcConfigurerAdapter并将所有代码从Application复制到UiApplication(当然检查你是否有重复的bean并适当地修复它)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-12
      • 1970-01-01
      • 2016-09-17
      • 2018-09-13
      • 1970-01-01
      • 2019-12-05
      • 1970-01-01
      相关资源
      最近更新 更多