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

相关推荐
黎雁·泠崖3 小时前
【魔法森林冒险】5/14 Allen类(三):任务进度与状态管理
java·开发语言
qq_12498707534 小时前
基于SSM的动物保护系统的设计与实现(源码+论文+部署+安装)
java·数据库·spring boot·毕业设计·ssm·计算机毕业设计
Coder_Boy_4 小时前
基于SpringAI的在线考试系统-考试系统开发流程案例
java·数据库·人工智能·spring boot·后端
Mr_sun.4 小时前
Day06——权限认证-项目集成
java
瑶山4 小时前
Spring Cloud微服务搭建四、集成RocketMQ消息队列
java·spring cloud·微服务·rocketmq·dashboard
abluckyboy5 小时前
Java 实现求 n 的 n^n 次方的最后一位数字
java·python·算法
2301_818732065 小时前
前端调用控制层接口,进不去,报错415,类型不匹配
java·spring boot·spring·tomcat·intellij-idea
2501_941982055 小时前
深度对比:Java、Go、Python 实现企微外部群推送,哪个效率更高?
java·golang·企业微信
马猴烧酒.5 小时前
【面试八股|JAVA多线程】JAVA多线程常考面试题详解
java·服务器·数据库
掘金者阿豪5 小时前
关系数据库迁移的“暗礁”:金仓数据库如何规避数据完整性与一致性风险
后端