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

相关推荐
CodeAmaz13 分钟前
自定义限流方案(基于 Redis + 注解)
java·redis·限流·aop·自定义注解
Felix_XXXXL27 分钟前
Plugin ‘mysql_native_password‘ is not loaded`
java·后端
韩立学长30 分钟前
【开题答辩实录分享】以《基于SpringBoot在线小说阅读平台》为例进行答辩实录分享
java·spring boot·后端
悟能不能悟36 分钟前
jsp怎么拿到url参数
java·前端·javascript
KWTXX37 分钟前
组合逻辑和时序逻辑的区别
java·开发语言·人工智能
高山上有一只小老虎40 分钟前
字符串字符匹配
java·算法
程序猿小蒜1 小时前
基于SpringBoot的企业资产管理系统开发与设计
java·前端·spring boot·后端·spring
jzhwolp1 小时前
从基本链表到侵入式链表,体会内核设计思路
c语言·后端·设计模式
纪莫1 小时前
技术面:MySQL篇(为啥会有非关系型数据库?MySQL的数据存储一定在磁盘吗?)
java·数据库·java面试⑧股
suzumiyahr1 小时前
用awesome-digital-human-live2d创建属于自己的数字人
前端·人工智能·后端