springboot-构建

14. Structuring your code

14.1 Using the “default” package

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

14.2 Locating the main application class

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

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

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

@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的很好的候选sql

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配置api

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类上springboot

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)app

16.2 Disabling specific auto-configuration

若是有本身不想出现的某个自动配置, 可使用@EnableAutoConfiguration中的exclude属性将禁止它, 例以下dom

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的属性

相关文章
相关标签/搜索