首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >关于SpringBoot开发的更多细节

关于SpringBoot开发的更多细节

作者头像
dongfanger
发布2021-12-16 11:20:39
3470
发布2021-12-16 11:20:39
举报
文章被收录于专栏:dongfangerdongfanger

在上篇文章《开发你的第一个SpringBoot应用》已经对SpringBoot基本开发流程有了大体了解,本文将继续对SpringBoot官网进行学习,发现关于SpringBoot开发的更多细节。

依赖管理

推荐使用Maven或者Gradle进行依赖管理,其他工具比如Ant,SpringBoot对它们的支持不是很好。

SpringBoot提供了很多starter来批量添加依赖,比如spring-boot-starter-web。官方定义的starter都是spring-boot-starter-*这种命名方式,由org.springframework.boot提供:

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using.build-systems.starters

而自定义的第三方starter命名方式往往会把名字放在前面,像这样:thirdpartyproject-spring-boot-starter

代码目录

官方推荐的代码目录是这样:

com
 +- example
     +- myapplication
         +- MyApplication.java
         |
         +- customer
         |   +- Customer.java
         |   +- CustomerController.java
         |   +- CustomerService.java
         |   +- CustomerRepository.java
         |
         +- order
             +- Order.java
             +- OrderController.java
             +- OrderService.java
             +- OrderRepository.java
  • 包名采用com.example.myapplication这种方式。
  • main启动入口类MyApplication.java放在根目录,便于查找其他文件。
  • 其他模块跟MyApplication.java并列。

配置

尽量不使用xml进行配置,而是定义一个配置类加上@Configuration注解。如果非得用xml,那么可以用@ImportResource进行导入。配置类也不用写成一个,多个配置类可以使用多个@Configuration,也可以拆成多个使用@Import相互导入。

xml一直被很多人吐槽,其实SpringBoot也是反对使用xml的。

@SpringBootApplication注解已经包含了@EnableAutoConfiguration注解,会对配置进行自动导入,比如在classpath中有一个HSQLDB,如果没有手动配置数据库连接,那么SpringBoot会自动进行配置。一般只添加@SpringBootApplication就可以了,不需要再重复添加@EnableAutoConfiguration

通过启动应用时加上--debug可以查看有哪些自动配置类,也可以对自动配置类进行移除,比如:

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class MyApplication {

}

还可以使用excludeNamespring.autoconfigure.exclude属性来指定要移除的自动配置类。

依赖注入

SpringBoot可以添加@ComponentScan来自动查找bean,比如@Component@Service@Repository@Controller会被自动注册为Spring Beans。@SpringBootApplication已经包含了@ComponentScan,所以不需要重复添加。

比如@Service使用构造器来包含一个bean:

@Service
public class MyAccountService implements AccountService {

    private final RiskAssessor riskAssessor;

    public MyAccountService(RiskAssessor riskAssessor) {
        this.riskAssessor = riskAssessor;
    }

    // ...

}

如果有多个构造器,那么可以使用@Autowired让Spring来自动注入:

@Service
public class MyAccountService implements AccountService {

    private final RiskAssessor riskAssessor;

    private final PrintStream out;

    @Autowired
    public MyAccountService(RiskAssessor riskAssessor) {
        this.riskAssessor = riskAssessor;
        this.out = System.out;
    }

    public MyAccountService(RiskAssessor riskAssessor, PrintStream out) {
        this.riskAssessor = riskAssessor;
        this.out = out;
    }

    // ...

}

@SpringBootApplication

一般在main方法上面会有这个注解,它实际上包含了3个注解:

  • @EnableAutoConfiguration
  • @ComponentScan
  • @SpringBootConfiguration
@SpringBootApplication // same as @SpringBootConfiguration @EnableAutoConfiguration
                        // @ComponentScan
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

}

如果只想包含其中部分注解,那么可以不用@SpringBootApplication

@SpringBootConfiguration(proxyBeanMethods = false)
@EnableAutoConfiguration
@Import({ SomeConfiguration.class, AnotherConfiguration.class })
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

}

启动方式

①IDEA使用Run as Java Application。

②命令行启动jar包。

$ java -jar target/myapplication-0.0.1-SNAPSHOT.jar

开启远程调式

$ java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n \
       -jar target/myapplication-0.0.1-SNAPSHOT.jar

③maven

$ mvn spring-boot:run

参考资料: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-12-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 依赖管理
  • 代码目录
  • 配置
  • 依赖注入
  • @SpringBootApplication
  • 启动方式
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档