赞
踩
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
application.yml 配置文件
server:
port: 8080
spring:
profiles:
active: datasource
application-datasource.yml配置文件
spring:
jdbc:
datasource:
driver: com.mysql.jdbc.Driver
url: jdbc:mysql:///springboot_01
userName: root
password: root
@Component
public class DataSourceProperties {
@Value("${spring.jdbc.datasource.driver}")
private String driver;
@Value("${spring.jdbc.datasource.url}")
private String url;
@Value("${spring.jdbc.datasource.userName}")
private String userName;
@Value("${spring.jdbc.datasource.password}")
private String password;
// 省略getter和setter.....
}
@Component
@ConfigurationProperties(prefix = "spring.jdbc.datasource")
public class DataSourceProperties {
private String driver;
private String url;
private String userName;
private String password;
// 省略getter和setter.....
}
@Controller
@EnableConfigurationProperties(DataSourceProperties.class)
public class HelloController {
@Autowired
private DataSourceProperties dataSourceProperties;
}
1.新建类(属性文件的bean对象)
2.新建 JdbcConfig配置类
3.对象属性引导
4.4. controller层调用
@Component
public class DataSourceProperties {
private String driver;
private String url;
private String userName;
private String password;
// 省略getter和setter.....
}
@SpringBootConfiguration
public class
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。