springboot-构建

14. Structuring your code

14.1 Using the “default” package

官方建议使用反向域名定义包结构,比如com.example.project
如果一个类没有被定义在一个包中,它会被放在一个default package中,当这个类应用@ComponentScan @EntityScan @SpringBootApplication这几个注解的时候会对全部依赖jar包进行扫描,应该避免这种情况

14.2 Locating the main application class

主启动类一般在所有类的外面, 位于一个root package中
注解@EnableAutoConfiguration经常被用在main application class,并且默认指定一个特定搜索范围(search package, 但是具体没有说清楚,看读到后面有没说吧)
文档中举了个栗子,如果你是在写一个JPA的程序,@EnableAutoConfiguration会被指定扫描带@Bean注解的类
如果启动类在root package中,那@ComponentScan就可以不用定义具体的basePackage属性,也可以直接使用@SpringBootApplication
经典布局(直接复制官网):

com
 +- example
     +- myproject
         +- Application.java
         |
         +- domain
         |   +- Customer.java
         |   +- CustomerRepository.java
         |
         +- service
         |   +- CustomerService.java
         |
         +- web
             +- CustomerController.java

Application.java是主启动类,定义了启动main方法

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {

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

}

15. Configuration classes

(汗颜,没有认真读过spring api,不知道@Configuration的具体用法,
没用过, 将就着看这段吧, 读完springboot后去补spring)

springboot倾向于基于java进行配置,所以尽管可以通过XML文件配置启动main方法SpringApplication.run(), 官方还是建议使用@Configuration注解
Usually the class that defines the main method is also a good candidate as the primary @Configuration.
这段不是很明白, 翻译过来是通常定义main方法的类也是主@Configuration的很好的候选

15.1 Importing additional configuration classes

You don’t need to put all your @Configuration into a single 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.
能看得懂, 但不理解, 硬翻不需要将所有的@Configuration放到一个class中, 可以使用@Import注解导入额外的配置类. 也可以使用@ComponentScan自动扫描spring组件, 包括@Configuration类
自己通过代码实现来理解

15.2 Importing XML configuration

即便必须使用xml源, 官方仍然建议使用@Configuration类, 然后使用一个注解@ImportResource来加载xml配置

16. Auto-configuration

springboot的auto-configuration会尝试根据你添加的jar包配置spring.
For example, If HSQLDB is on your classpath, and you have not manually configured any database connection beans, then we will auto-configure an in-memory database.
比如hsqldb在classpath路径下, 如果没有手动配置数据库连接, springboot会自动配置一个内嵌的数据库.
你需要添加注解@EnableAutoConfiguration或者@SpringBootApplication到其中一个@Configuration类上来实现自动配置.
其实只需要添加一个@EnableAutoConfiguration注解到主要的那个@Configuration类上

16.1 Gradually replacing auto-configuration

auto-configuration并没有太大的侵入性, 你可以在任何时候手动设置替代特定的自动配置.
如果想知道现在哪些auto-configuration正在被使用以及为什么, 可以使用--debug, 这回生成自动配置报告并输出到控制台(This will enable debug logs for a selection of core loggers and log an auto-configuration report to the console)

16.2 Disabling specific auto-configuration

如果有自己不想出现的某个自动配置, 可以使用@EnableAutoConfiguration中的exclude属性将禁止它, 例如下

import org.springframework.boot.autoconfigure.*;
import org.springframework.boot.autoconfigure.jdbc.*;
import org.springframework.context.annotation.*;

@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MyConfiguration {
}

如果类没有在classpath路径下, 可以使用excludeName属性来指定有效的名字, 同样可以使用property属性spring.autoconfigure.exclude来进行指定. 注解跟配置文件两种方式可同时定义使用

17. Spring Beans and dependency injection

你可以自由使用任何标准的spring framework来定义beans以及注入依赖, 比如使用@ComponentScan来扫描beans, 使用@Autowired构造器来注入效果就不错.
如果代码构造如之前所推荐的(在根目录加载Application类), 你可以毫无争议的使用@ComponentScan, 所有带有(@Component @Service @Repository @Controller etc.)注解的类都可以自动注册为spring beans.
如下是一个使用构造器注入的@Servicebean

package com.example.service;

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

@Service
public class DatabaseAccountService implements AccountService {

    private final RiskAssessor riskAssessor;

    @Autowired
    public DatabaseAccountService(RiskAssessor riskAssessor) {
        this.riskAssessor = riskAssessor;
    }

    // ...

}

如果bean有一个构造器, 可以省略@Autowired

@Service
public class DatabaseAccountService implements AccountService {

    private final RiskAssessor riskAssessor;

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

    // ...

}

这里用构造器注入, 允许RiskAssessor被标记成final, 后续RiskAssessor不可改变

18. Using the @SpringBootApplication annotation

因为@ComponentScan @EnableAutoConfiguration @Configuration太经常组合使用了, 所以有一个@SpringBootApplication用来替代它们三个, 与这三个注解的默认属性配置相同, 例如下

package com.example.myproject;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
public class Application {

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

}

@SpringBootApplication还提供了别名来定制@ComponentScan @EnableApplicationConfiguration的属性

原文地址:https://www.cnblogs.com/simple-huang/p/8176919.html