springboot如何开启注解扫描
时间: 2023-04-07 18:01:32 浏览: 144
Spring Boot 可以通过在主类上添加 @ComponentScan 注解来开启注解扫描。在 @ComponentScan 注解中可以指定要扫描的包路径,也可以通过 @SpringBootApplication 注解来自动开启注解扫描。例如:
```
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
```
在这个例子中,@SpringBootApplication 注解自动开启了注解扫描,扫描的包路径为主类所在的包及其子包。如果需要指定其他包路径,可以在 @SpringBootApplication 注解中添加 @ComponentScan 注解,例如:
```
@SpringBootApplication
@ComponentScan(basePackages = {"com.example.controller", "com.example.service"})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
```
这个例子中,@ComponentScan 注解指定了要扫描的包路径为 com.example.controller 和 com.example.service。
阅读全文
相关推荐


















