SpringBoot的基础(自动配置)

@SpringBootApplication注解

是一个组合注解,其中@EnableAutoConfiguration让SpringBoot根据类路径中的jar包依赖为当前项目进行自动配置

例如:添加了spring-boot-starter-web依赖,会自动添加Tomcat和SpringMVC的依赖

添加了spring-boot-starter-data-jpa依赖,会自动进行JPA相关的配置

关闭特定的自动配置:@SpringBootApplication(exclude={DataSourceautoConfiguration.class})

定制Banner

①、在src/main/resources新建一个banner.txt

②、通过http://patorjk.com/software/taag网站生成字符,如输入"HELLO",将网站生成的字符复制到banner.txt中

③、再启动程序即可看到

关闭banner

方式一:修改main内容

java 复制代码
SpringApplication app = new SpringApplication(Ch522Application.class);
app.setShowBanner(false);
app.run(args);

方式二:使用fluentAPI修改

java 复制代码
new SpringApplicationBuilder(Ch522Application.class)
	.showBanner(false)
	.run(args);

导入xml配置

java 复制代码
@ImportResource({"classpath:some-context.xml","classpath:another-context.xml"})

常规的Spring环境下注入properties文件里的值通过@PropertySource指明properties文件的位置,通过@Value注入

在SpringBoot中,只需要application.properties定义属性,直接使用@Value注入即可

SpringBoot还提供了基于类型安全的配置,通过@ConfigurationProperties将propertiees属性和一个Bean及属性关联

①、application.properties

shell 复制代码
author.name=syf
author.age=12

也可以新建一个properties文件,需要在@ConfigurationProperties的属性locations里指定properties的位置,且需要在入口类上配置

②、类型安全的Bean

java 复制代码
@Component
//locations=({"classpath:config/author.properties"})可以省略
@ConfigurationProperties(prefix="author")//加载properties文件内的配置,并指定文件前缀
public class AuthorSettings{
	
	private String name;
	private Long age;

	public String getName(){
		return name;
	}
	public void setName(){
		this.name = name;
	}

	public Long getAge(){
		return age;
	}

	public void setAge(Long age){
		this.age = age;
	}
}

③、控制器

java 复制代码
@RestController
@SpringBootApplication
public class Ch623Application{
	
	@Autowired
	private AuthoriSetting authorSettings;

	@RequestMapping("/")
	public String index(){
		return "author name is " + authorSettings.getName() + " and other age is" + authorSettings.getAge();
	}
}

访问http:localhost:8080/

日志配置

支持Java Util Logging,Log4J,Log4J2和Logback作为日志框架

SpringBoot为当前日志框架的控制台输出以及文件输出做了配置,默认使用Logback作为日志框架

配置日志级别

logging.file=D://mylog/log.log

配置日志文件,格式为logging.level.包名=级别:

logging.level.org.springframework.web=DEBUG

Profile配置

全局配置使用application-{profile}.properties 如:application-prod.properties

通过在application.properties中设置spring.profiles.active=prod来指定活动的Profile

①、生产环境和开发环境的配置

application-prod.properties

bash 复制代码
server.port=80

application-dev.properties

bash 复制代码
server.port=8888

②、全局配置application.properties

bash 复制代码
spring.profiles.active=dev

查看当前项目已启用和未启用的自动配置报告

①、运行jar时增加--debug参数

java -jar xx.jar --debug

②、在application.properties中设置属性

debug=true

③、在STS中设置

④、启动项目,控制台输出

Positive matches 已启用的自动配置

Negative matches 未启用的自动配置

自动配置案例

①、依赖

spring-boot-autoconfigure 属于SpringBoot自身的自动配置作为依赖

junit

②、属性配置(类型安全的属性获取)

在application.properties中通过hello.msg=来设置,若不设置,默认为hello.msg=world

java 复制代码
@ConfigurationProperties(prefix="hello")
public class HelloServiceProperties{
	
	private static final String MSG = "world";

	private String msg = MSG;
	
	public String getMsg(){
		return msg;
	}

	public void setMsg(String msg){
		this.msg = msg;
	}
}

③、判断依据类

根据此类的存在与否,来创建这个类的Bean,这个类可以是第三方类库

java 复制代码
public class HelloService{
	
	private String msg;

	public String sayHello(){
		return "Hello" + msg;
	}

	public String getMsg(){
		return msg;
	}

	public void setMsg(String msg){
		this.msg = msg;
	}

}

④、自动配置类

java 复制代码
@Configuration
@EnableConfigurationProperties(HelloServiceProperties.class)
@ConditionalOnClass(HelloService.class)//判断HelloService这个类路径是否存在,且当容器中没有这个Bean情况下自动配置这个Bean
@ConditionalOnProperty(prefix="hello",value="enabled",matchIfMissing=true)
public class HelloServiceAutoConfiguration{
	
	@Autowired
	private HelloServiceProerties helloServiceProperties;

	@Bean
	@ConditionalOnMissingBean(HelloService.class)
	public HelloService helloService(){
		HelloService helloService = new HelloService();
		helloServcie.setMsg(helloServiceProperties.getMsg());
		return helloService;
	}
}

⑤、注册配置

src/main/resources新建META-INF/spring.factories

bash 复制代码
org.springframework.boot.autoconfigure.enableAutoConfiguration=\
com.wisely.spring_boot_starter_hello.HelloServiceAutoConfiguration

若有多个自动配置,则用","隔开,此处"\"是为了换行后仍能读到属性

⑥、使用starter,新建SpringBoot项目,并将刚创建的starter作为依赖

xml 复制代码
<dependency>
	<groupId>com.wisely</groupId>
	<artifactId>spring-boot-starter-hello</artifactId>
	<version>0.0.1-SHAPSHOT</version>
</dependency>

开发阶段,引入依赖,在starter稳定后将spring-boot-starter-hello通过mvn install安装到本地库,或者发布到私服上

java 复制代码
//运行类
@RestController
@SpringBootApplication
public class CH65Application{
	
	@Autowired
	HelloService helloService;//代码中可以直接注入,但项目中没有配置这个Bean,这是通过自动配置完成的

	@RequestMapping("/")
	public String index(){
		return helloService.sayHello();
	}

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

如果在application.properties中配置 hello.msg=jordan

再次访问http://localhost:8080返回的是Hello jordan

在application.properties中添加debug属性,查看自动配置报告

debug=true

相关推荐
君子剑mango8 分钟前
Spring @Import
java·spring
Jason-河山10 分钟前
Java爬虫抓取数据的艺术
java·爬虫·python
百成Java14 分钟前
基于springboot的旅游网站
java·spring boot·后端·mysql·spring·智能家居·旅游
昭著24 分钟前
优先级队列(堆)
java·数据结构
cc77521032 分钟前
【常见框架漏洞】ThinkPHP、struts2、Spring、Shiro
java·struts·spring
Pandaconda35 分钟前
【计算机网络 - 基础问题】每日 3 题(二十三)
开发语言·网络·笔记·后端·计算机网络·面试·职场和发展
IT杨秀才36 分钟前
Go语言的Context妙用
后端·go
野生派蒙38 分钟前
IDEA 关闭自动补全功能(最新版本)
java·开发语言·ide·后端·学习·intellij-idea
林小果139 分钟前
观察者模式
java·开发语言·观察者模式·设计模式
xplidelphi41 分钟前
Idea开发Gradle多模块项目踩坑纪实之一:Java模块无法执行
java·ide·intellij-idea