Spring Boot 基础配置方法

pom.xml
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
</parent>
......
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
	        <!--将应用打包成一个可执行jar-->
	        <!--maven package打包后的jar 可以java -jar xxx.jar 运行-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

mvn spring-boot:run

基本配置
入口类 和 @SpringBootAppication

Spring Boot 通常有一个名为*Application的入口类,入口类中有一个main方法,这个方法就是一个标准的Java应用的入口方法。

@SpringBootApplication
public class StartApplication {

	public static void main(String[] args) {
		//启动Spring Boot应用项目
		SpringApplication.run(StartApplication.class, args);
	}
}

@SpringBootApplication是Spring的核心注解,他是一个组合注解

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Document
@Inherited
@Configuration
@EnableAutoConfiguration // 让Spring Boot根据类路径中的jar包依赖为当前项目进行自动配置
@ComponentScan
public @interface SpringBootApplication {
	Class<?>[] exclude() default {};
	String[] excludeName() default {};
}

例如添加了spring-boot-starter-web依赖,会自动添加Tomcat和SpringMvc的依赖,那么Spring Boot会对Tomcat和SpringMvc进行自动配置。

关闭特定的自动配置应该使用@SpringBootApplication注解的exclude参数:
@SpringBootApplication(exclude={DataSourceAutoConfigutation.class})

SpringBoot 的配置文件

SpringBoot使用一个全局的配置文件 application.propertiesapplication.yml,放置在 src/main/resource 目录或者类路径的 /config 下。
修改Tomcat端口:

server.port=8082
server.context-path=/helloboot

context-path、contextPath、CONTEXT_PATH通用

可以通过Spring提供的@ImportSource来加载xml配置:

@ImportSource({"classpath:a-context.xml", "classpath:b-context.xml"})

打包成jar的程序可以通过下面命令运行:

java -jar xx.jar

可以通过以下命令修改Tomcat端口号:

java -jar xx.jar --server.port=8082

在Spring中,注入properties文件的值,可以通过@PropertySource指明properties文件的位置,然后通过@Value注入值。在Spring Boot中,只需要在application.properties中定义属性,直接用@Value注入即可。

book.author=wang
book.name=spring boot
@SpringBootApplication
public class bookApplication {
	@Value("${book.author}")
	private String bookAuthor;
	@Value("${book.name}")
	private String bookName;
}

Spring Boot还提供了基于类型安全的配置方式,通过@ConfigurationProperties将properties属性和一个Bean及其属性关联,从而实现类型安全配置
需要有setter方法

@ConfigurationProperties(prefix="book", locations={"classpath:book.properties"})
public class bookApplication {
	
	private String author;
	public void setAuthor(String author) {
		this.author = author;
	}
}
日志配置

Spring Boot支持Java Util Logging、Log4J、Log4J2和Logback作为日志框架,无论使用哪种日志框架、Spring Boot已为当前使用日志框架的控制台输出及文件输出做好了配置。

#配置日志文件
logging.file=/mylog/log.log
#配置日志级别,格式为logging.level.包名=级别
logging.level.org.springframework.web=DEBUG
Profile配置

Profile是Spring用来针对不同的环境对不同的配置提供支持的,全局Profile配置使用application-{profile}.properties(application-prod.properties)
通过在application.properties中设置spring.profiles.active=prod来指定活动的Profile

1


  1. p150 ↩︎

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值