自学内容网 自学内容网

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内容

SpringApplication app = new SpringApplication(Ch522Application.class);
app.setShowBanner(false);
app.run(args);

方式二:使用fluentAPI修改

new SpringApplicationBuilder(Ch522Application.class)
.showBanner(false)
.run(args);

导入xml配置

@ImportResource({"classpath:some-context.xml","classpath:another-context.xml"})

常规的Spring环境下注入properties文件里的值通过@PropertySource指明properties文件的位置,通过@Value注入
在SpringBoot中,只需要application.properties定义属性,直接使用@Value注入即可

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

①、application.properties

author.name=syf
author.age=12

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

②、类型安全的Bean

@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;
}
}

③、控制器

@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

server.port=80

application-dev.properties

server.port=8888

②、全局配置application.properties

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

@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,这个类可以是第三方类库

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;
}

}

④、自动配置类

@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

org.springframework.boot.autoconfigure.enableAutoConfiguration=\
com.wisely.spring_boot_starter_hello.HelloServiceAutoConfiguration

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

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

<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安装到本地库,或者发布到私服上

//运行类
@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


原文地址:https://blog.csdn.net/usa_washington/article/details/142552623

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!