零配置原理:
零配置就是将原本的每个xml配置文件写成了对应的配置类
通过编写一个类继承AbstractAnnotationConfigDispatcherServletInitializer来在项目启动的时候进行加载这些配置类。
准备工作
db.properties
输入数据库四大参数信息
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
创建DbProperties.java类
package com.zhuoyue.properties;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Data
@Configuration
@PropertySource("classpath:/db.properties")//引入配置信息
public class DbProperties {
@Value("${spring.datasource.driver-class-name}")
private String driverClass;
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
}