SpringBoot报错提示“Spring Boot Application in default package“

异常

原因

MainApplication在默认包下,则没有定义包,所以会报错。没有包则不存在子包的概念。(注:这里的默认包就是maven项目的java文件夹)

我们使用@SpringBootApplication代替@EnableAutoConfiguration和@ComponentScan("controller"),如果Controller在MainApplication的子包下面,则不需要扫描包,但这里MainApplication没有在自定义包下。

解决

解决方案有两种:

第一种,仍然使用@EnableAutoConfiguration和@ComponentScan("controller")注解,其中@ComponentScan注解的扫描包指向控制器类所在的包。

第二种,创建包,将MainAppplication放进去,如下图所示:

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,可以通过编写主程序类来启动应用。在该类上使用@SpringBootApplication注解标注说明这是一个Spring Boot应用。具体的例子如下所示: ```java @SpringBootApplication public class HelloWorldMainApplication { public static void main(String[] args) { SpringApplication.run(HelloWorldMainApplication.class, args); } } ``` 此外,根据引用中的内容,可以在项目的pom.xml文件中配置相关的依赖项。例如,可以添加spring-boot-starter-web依赖项来启用web功能。具体的pom.xml配置如下所示: ```xml <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> ``` 在Spring Boot应用中,还可以通过yml文件来配置环境。例如,可以在resources目录下创建一个名为application.yml的文件,并在其中配置端口号、上下文路径、数据源等信息。具体的配置如下所示: ```yaml server: port: 8989 servlet: context-path: /mvc spring: datasource: type: com.mchange.v2.c3p0.ComboPooledDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/hr username: root password: root freemarker: suffix: .ftl content-type: text/html charset: UTF-8 template-loader-path: classpath:/views/ devtools: restart: enabled: true additional-paths: src/main/java poll-interval: 3000 quiet-period: 1000 cache: ehcache: config: classpath:ehcache.xml mybatis: mapper-locations: classpath:/mappers/*.xml type-aliases-package: com.xxxx.springboot.po configuration: map-underscore-to-camel-case: true pagehelper: helper-dialect: mysql logging: level: com.xxxx.dao: debug ``` 最后,根据引用中的内容,还可以在项目中编写SQL映射文件来配置相关的数据库操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [springboot环境配置](https://blog.csdn.net/leaf_leaf__/article/details/126718902)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [SpringBoot环境配置](https://blog.csdn.net/gaozhen666/article/details/117130267)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值