骄阳房地产网站,昆山住房和城乡建设部网站,邯郸网站设计怎么申请,支付网站建设教学视频
黑马程序员SpringBoot3Vue3全套视频教程#xff0c;springbootvue企业级全栈开发从基础、实战到面试一套通关
springboot基础
搭建项目 修改配置文件 修改application.yml#xff08;后缀名不对#xff0c;可以改成这个#xff09;#xff0c;配置数据库
spr…教学视频
黑马程序员SpringBoot3Vue3全套视频教程springbootvue企业级全栈开发从基础、实战到面试一套通关
springboot基础
搭建项目 修改配置文件 修改application.yml后缀名不对可以改成这个配置数据库
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/test?useUnicodetruecharacterEncodingutf-8useSSLfalseserverTimezoneUTCusername: rootpassword: 123abc运行
配置文件
书写
# 普通属性
email:host: smtp.qq.comusername: 123456789qq.compassword: 123456# 数组
hobbies:- 1- 2- 3- 4值前边必须用空格作为分隔符使用空格作为缩进表示层级关系相同的层级左侧对齐
获取值 1、使用Value(${键名})
Component
public class EmailProperties {public String getHost() {return host;}public void setHost(String host) {this.host host;}public String getUsername() {return username;}public void setUsername(String username) {this.username username;}public String getPassword() {return password;}public void setPassword(String password) {this.password password;}Value(${email.host})private String host;Value(${email.username})private String username;Value(${email.password})private String password;
}Controller
public class HelloController {AutowiredEmailProperties emailProperties;RequestMapping(/hello)ResponseBodypublic String hello() {return emailProperties.getHost();}
}2、在类上使用ConfigurationProperties
Component
ConfigurationProperties(prefix email)
public class EmailProperties {public String getHost() {return host;}public void setHost(String host) {this.host host;}public String getUsername() {return username;}public void setUsername(String username) {this.username username;}public String getPassword() {return password;}public void setPassword(String password) {this.password password;}private String host;private String username;private String password;
}其他不变
bean管理
Bean扫描
在springboot项目的启动类中使用了SpringBootApplication这个注解是SpringBootConfiguration、EnableAutoConfiguration和ComponentScan三个注解的组合。
SpringBootApplication默认会对应用主类所在包及其子包进行扫描寻找带有Component、Service、Repository和Controller等注解的类并将它们注册为Spring容器中的Bean。
注意
默认会加载启动类所在包及其子包进行扫描包之外的可以在启动类上使用注解ComponentScan(basePackages {org.example})进行配置
bean注册
Component声明注解不属于以下三类时使用ControllerComponent的衍生注解标注在控制器类上ServiceComponent的衍生注解标注在业务类上RepositoryComponent的衍生注解标注在数据访问类上由于与mybatis整合用的少