官方建议使用反向域名定义包结构,好比com.example.project
若是一个类没有被定义在一个包中,它会被放在一个default package中,当这个类应用@ComponentScan
@EntityScan
@SpringBootApplication
这几个注解的时候会对所有依赖jar包进行扫描,应该避免这种状况java
主启动类通常在全部类的外面, 位于一个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); } }
(汗颜,没有认真读过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
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类
本身经过代码实现来理解数据库
即使必须使用xml源, 官方仍然建议使用@Configuration类, 而后使用一个注解@ImportResource
来加载xml配置api
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
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
若是有本身不想出现的某个自动配置, 可使用@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
来进行指定. 注解跟配置文件两种方式可同时定义使用布局
你能够自由使用任何标准的spring framework来定义beans以及注入依赖, 好比使用@ComponentScan
来扫描beans, 使用@Autowired
构造器来注入效果就不错.
若是代码构造如以前所推荐的(在根目录加载Application类), 你能够毫无争议的使用@ComponentScan
, 全部带有(@Component @Service @Repository @Controller etc.)注解的类均可以自动注册为spring beans.
以下是一个使用构造器注入的@Service
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; } // ... }
若是bean有一个构造器, 能够省略@Autowired
@Service public class DatabaseAccountService implements AccountService { private final RiskAssessor riskAssessor; public DatabaseAccountService(RiskAssessor riskAssessor) { this.riskAssessor = riskAssessor; } // ... }
这里用构造器注入, 容许RiskAssessor被标记成final, 后续RiskAssessor不可改变
由于@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
的属性