首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >SpringBoot官方笔记2使用

SpringBoot官方笔记2使用

作者头像
dongfanger
发布2023-07-20 20:02:34
发布2023-07-20 20:02:34
2640
举报
文章被收录于专栏:dongfangerdongfanger

Build Systems

选择Maven or Gradle,而不要Ant(not particularly well supported)

In practice, you do not need to provide a version for any of these dependencies (spring-boot-dependencies) in your build configuration, as Spring Boot manages that for you. When you upgrade Spring Boot itself, these dependencies are upgraded as well in a consistent way.

Each release of Spring Boot is associated with a base version of the Spring Framework.

Starters are a set of convenient dependency descriptors that you can include in your application.

starter命名区别:

  • official spring-boot-starter-*
  • third-party thirdpartyproject-spring-boot-starter

Structuring

We generally recommend that you locate your main application class in a root package above other classes.

代码语言:javascript
复制
com
 +- example
     +- myapplication
         +- MyApplication.java
         |
         +- customer
         |   +- Customer.java
         |   +- CustomerController.java
         |   +- CustomerService.java
         |   +- CustomerRepository.java
         |
         +- order
             +- Order.java
             +- OrderController.java
             +- OrderService.java
             +- OrderRepository.java

实际项目见得多的是controller、service这样的package,然后里面放多个文件。

Configuration

Although it is possible to use SpringApplication with XML sources, we generally recommend that your primary source be a single @Configuration class.

The @Import annotation can be used to import additional configuration classes. Alternatively, you can use @ComponentScan to automatically pick up all Spring components, including @Configuration classes.

(因为@SpringBootApplication包含了@ComponentScan,所以能自动扫到@Configuration的配置类)

@ImportResource annotation to load XML configuration files.

Auto-configuration

Spring Boot auto-configuration attempts to automatically configure your Spring application based on the jar dependencies that you have added.

For example, if HSQLDB is on your classpath, and you have not manually configured any database connection beans, then Spring Boot auto-configures an in-memory database. If you add your own DataSource bean, the default embedded database support backs away.

exclude auto-configuration:

①annotation

代码语言:javascript
复制
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

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

}

If the class is not on the classpath, you can use the excludeName attribute of the annotation and specify the fully qualified name instead.

②property

spring.autoconfigure.exclude

Spring Beans and Dependency Injection

Spring Beans可以理解为功能模块

@ComponentScan to find beans,all of your application components (@Component@Service@Repository@Controller, and others) are automatically registered as Spring Beans.

constructor injection

代码语言:javascript
复制
import org.springframework.stereotype.Service;

@Service
public class MyAccountService implements AccountService {

    private final RiskAssessor riskAssessor;

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

    // ...

}

If a bean has more than one constructor, you will need to mark the one you want Spring to use with @Autowired:

代码语言:javascript
复制
import java.io.PrintStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@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;
    }

    // ...

}

(更常见的做法是直接在类变量定义时使用@Autowired或@Resource,省去constructor)

@SpringBootApplication

  • @EnableAutoConfiguration: enable Spring Boot’s auto-configuration mechanism
  • @ComponentScan: enable @Component scan on the package where the application is located (see the best practices)
  • @SpringBootConfiguration: enable registration of extra beans in the context or the import of additional configuration classes. An alternative to Spring’s standard @Configuration that aids configuration detection in your integration tests.
代码语言:javascript
复制
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

// Same as @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan
@SpringBootApplication
public class MyApplication {

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

}

Running

jar包:using an embedded HTTP server

war包:your server

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

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Build Systems
  • Structuring
  • Configuration
  • Auto-configuration
  • Spring Beans and Dependency Injection
  • @SpringBootApplication
  • Running
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档