SpringBoot2.0 (二) ——使用SpringBoot

1. 构建系统(MAVEN)

  1. 继承 starter parent
1、 配置你的项目继承spring-boot-starter-parent 的配置


    org.springframework.boot
    spring-boot-starter-parent
    2.0.0.BUILD-SNAPSHOT

2、 不使用parent POM 进行配置

     
        
        
            org.springframework.data
            spring-data-releasetrain
            Fowler-SR2
            import
            pom
        
        
            
            org.springframework.boot
            spring-boot-dependencies
            2.0.0.BUILD-SNAPSHOT
            pom # 这个地方很重要
            import # 这个地方很重要
        
    

  1. 使用 Spring Boot Maven plugin

    
        
            org.springframework.boot
            spring-boot-maven-plugin
        
    

如果使用了Spring Boot starter parent pom,你仅仅需要添加plugin,不需要配置它除非你想改变parent 中的默认配置。

2. 构建代码

Spring Boot 不需要任何特殊的代码布局,但是也有一些最好的实践。
当一个类不包括一个包的声明,它被认为在默认的包。我们应该避免这种情况。当使用@ComponentScan、@EntityScan、@SpringBootApplication 注解的时候,它可能引起一些问题。因为来自每个jar 包的类都将被读取。
典型的布局:

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

Application.java 文件将声明main方法,包含基本的@Configuration。

package com.example.myproject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

3. 配置类(Configuration classes)

Spring Boot 支持基于JAVA的配置。尽管他可能用一个XML资源调用SpringApplication.run( ) 方法,我们通常任我你的主要资源是@Configuration类。通常这个定义在main方法中。

  1. 导入额外的配置类
    你不需要把所有的配置放在一个类中,@Import 注解可以用于导入额外的配置类。另外你可以使用@ConponentScan 去自动挑选出所有包括@Configuration 的类。
  2. 导入 XML配置类
    如果你必须使用XML进行配置,我们建议仍然以@Configuration类开始,你之后可以使用@ImportResource 注解去加载你的XML文件。

4. 自动配置 (Auto-Configuration)

Spring Boot 自动配置尝试去自动的配置基于你添加的jar依赖的Spring Application 。例如,如果你添加HSQLDB在你classpath,你没有手动的配置任何数据库链接beans,那么我们自动配置一个 in-memory数据库。
你需要缺省auto-configuration通过添加@EnableAutoConfiguration 或者@SpringBootApplication 注解在你一个@Configuration类

  1. 逐渐取代自动配置
    自动配置是无损伤的,在任何时候你可以定义自己的配置来取代部分的自动配置。例如你添加你自己的DataSource bean,默认的将被放弃。如果你需要找出目前正在应用的自动配置以及原因,你可以debug。日志会把信息报告到控制台。
  2. 禁止特定的自动配置
import org.springframework.boot.autoconfigure.*;
import org.springframework.boot.autoconfigure.jdbc.*;
import org.springframework.context.annotation.*;
@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MyConfiguration {
    // TODO
}

5. Spring Beans 和 DI(dependency injection)

你可以自由使用任何标准的Spring 框架技术及其依赖注入。为了简单起见,我们发现使用@ConponentScan找到你的bean,结合@autowired 构造函数注入。
如果你的代码结构是上面建议的,你可以添加@ComponentScan 没有任何参数。你所有的应用程序组件(@Component,@Service,@,@controller 等等 )将自动注册为Spring bean。

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;
    }
    // ...
}
@Service
public class DatabaseAccountService implements AccountService {
    private final RiskAssessor riskAssessor;
    public DatabaseAccountService(RiskAssessor riskAssessor) {
        this.riskAssessor = riskAssessor;
    }
    // ...
}

6. 打包生产应用程序

7. 下一步学习内容

你可能感兴趣的:(SpringBoot2.0 (二) ——使用SpringBoot)