SpringBoot官方筆記2使用

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.

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

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

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:

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.

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章