【Spring boot】运行Controller的两种方式

方式一:当只需要运行一个控制器时

通过在UserController中加上@EnableAutoConfiguration开启自动配置,然后通过SpringApplication.run(UserController.class);运行这个控制器;
这里写图片描述

方式二:可运行多个控制器

通过@Configuration+@ComponentScan开启注解扫描并自动注册相应的注解Bean
Java代码
这里写图片描述
这里写图片描述
也可以将Application.java中的

@Configuration
@ComponentScan
@EnableAutoConfiguration

换为

@SpringBootApplication

猜你喜欢

转载自blog.csdn.net/u013451157/article/details/80389295