springboot实战二-springboot关键配置

余生颓废 提交于 2020-03-17 10:56:39

依赖管理

  1. 注意上篇文章我们搭建的springboot项目pom.xm文件有如下配置:

    继承了 spring-boot-starter-parent

    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.4.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
    </parent>
    

    spring-boot-starter-parent 提供了项目默认的java版本 1.8,默认的编码UTF8,同时依赖 spring-boot-dependencies ,提供了dependency Management管理依赖的版本号,这样子项目在依赖具体jar包时无需指定具体版本号,统一继承parent的版本号;

  2. 但是不是所有的项目都希望继承 spring-boot-starter-parent,由于自己公司的项目可能还依赖自己的parent,此时还想使用dependency Management管理依赖的版本号,可以引入如下依赖且scope设置成scope=import;无需引入spring-boot-starter-parent

    a. 引入spring-boot-dependencies 如下:

<dependencyManagement>
		<dependencies>
		<dependency>
			<!-- Import dependency management from Spring Boot -->
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-dependencies</artifactId>
			<version>2.1.4.RELEASE</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement> 					

​ b.

在一个没有 dependencyManagement的pom中添加Spring Boot Maven Plugin

<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
	</plugins>
</build>

​ c. 添加Java版本插件:

 <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.7.0</version>
                    <configuration>
                        <!-- or whatever version you use -->
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
   </plugin>

​ d. 添加编码配置:

 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

@SpringBootApplication

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
//@ImportResource(locations = {"classpath:beans.xml"})
@PropertySource("classpath:person.properties")
public class SpringbootDemoApplication {

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

}

​ @SpringBootApplication注解整合了以下三个注解:

  • @EnableAutoConfiguration:启动springboot的自动配置机制
  • @ComponentScan:扫描@Component标注的类,也会扫描@configuration的类,默认扫描当前类所在包的下面,所以启动类一般位于项目根包中
  • @SpringBootConfiguration:实际上是一个@Configuration,表明这是一个配置类,可以配置bean;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {

SpringBootConfiguration 源码,其实就是一个@configuration配置类

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
}

@import &@EnableAutoConfiguration&@Configuration

@Configuration
@EnableAutoConfiguration
@Import({ MyConfig.class, MyAnotherConfig.class })
public class Application {

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

}

@Import :导入自定义的beans

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!