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

相关推荐
Chen-Edward16 分钟前
有了Spring为什么还有要Spring Boot?
java·spring boot·spring
magic3341656335 分钟前
Springboot整合MinIO文件服务(windows版本)
windows·spring boot·后端·minio·文件对象存储
开心-开心急了1 小时前
Flask入门教程——李辉 第一、二章关键知识梳理(更新一次)
后端·python·flask
掘金码甲哥1 小时前
调试grpc的哼哈二将,你值得拥有
后端
陈小桔1 小时前
idea中重新加载所有maven项目失败,但maven compile成功
java·maven
小学鸡!1 小时前
Spring Boot实现日志链路追踪
java·spring boot·后端
xiaogg36781 小时前
阿里云k8s1.33部署yaml和dockerfile配置文件
java·linux·kubernetes
逆光的July2 小时前
Hikari连接池
java
微风粼粼2 小时前
eclipse 导入javaweb项目,以及配置教程(傻瓜式教学)
java·ide·eclipse
番茄Salad2 小时前
Spring Boot临时解决循环依赖注入问题
java·spring boot·spring cloud