标签:文件中 cimage factor ogr res mys 自动调用 ruid name
springboot的多种配置方式java配置主要靠java类和一些注解,比较常用的注解有:
@Configuration :声明一个类作为配置类,代替xml文件
@Bean :声明在方法上,将方法的返回值加入Bean容器,代替 标签
@Value :基本类型或String属性注入
@PropertySource :指定外部属性文件
<!--pom.xml -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
# src/resources/jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/bos
jdbc.username=root
jdbc.password=123456
//src\main\java\com\itheima\config\DruidConfig.java
@Configuration
@PropertySource("classpath:jdbc.properties")
public class DruidConfig {
@Value("${jdbc.url}")
String url;
@Value("${jdbc.driverClassName}")
String driverClassName;
@Value("${jdbc.username}")
String username;
@Value("${jdbc.password}")
String password;
@Bean
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
}
解读:
@Configuration :声明我们 DruidConfig是一个配置类
@PropertySource :指定属性文件的路径是: classpath:jdbc.properties
@Value 为属性注入值(只能是基本类型或String)
<!--pom.xml -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- ============不添加在IDEA 会报红,但并不影响功能 ================= -->
<dependency>
<groupId> org.springframework.boot </groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--============================================================== -->
# src/resources/application.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/bos
jdbc.username=root
jdbc.password=123456
//src\main\java\com\itheima\config\DruidConfig.java
@ConfigurationProperties(prefix = "jdbc")
public class DruidProperties {
private String url;
private String driverClassName;
private String username;
private String password;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
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;
}
}
//src\main\java\com\itheima\config\DruidConfig.java
@Configuration
@EnableConfigurationProperties(DruidProperties.class)
public class DruidConfig {
@Bean
public DataSource dataSource(DruidProperties dp) {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(dp.getDriverClassName());
dataSource.setUrl(dp.getUrl());
dataSource.setUsername(dp.getUsername());
dataSource.setPassword(dp.getPassword());
return dataSource;
}
}
解读:
@ConfifigurationProperties注解声明当前类为属性读取类,在类上定义各个属性,名称必须与属性文件中 jdbc. 后面部分一致。
//src\main\java\com\itheima\config\DruidConfig.java
@Configuration
@EnableConfigurationProperties(DruidProperties.class)
public class DruidConfig {
@Autowired
private DruidProperties dp;
@Bean
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
//setter
return dataSource;
}
}
```
//src\main\java\com\itheima\config\DruidConfig.java
@Configuration
@EnableConfigurationProperties(DruidProperties.class)
public class DruidConfig {
private DruidProperties dp;
public DruidConfig(DruidProperties dp){ this.dp = dp; }
@Bean
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
//setter
return dataSource;
}
}
```
//src\main\java\com\itheima\config\DruidConfig.java
@Configuration
@EnableConfigurationPerProperties(DruidProperties.class)
public class DruidConfig {
@Bean
public DataSource dataSource(DruidProperties dp) {
DruidDataSource dataSource = new DruidDataSource();
//setter
return dataSource;
}
}
方式二通过属性读取类解决了@Value不能读取对象属性(如user.friend.name)的问题,但似乎就更加麻烦了
事实上,如果一段属性只有一个Bean需要使用,我们无需将其注入到一个类。
<!--pom.xml -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- ============不添加在IDEA 会报红,但并不影响功能 ================= -->
<dependency>
<groupId> org.springframework.boot </groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--============================================================== -->
# src/resources/application.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/bos
jdbc.username=root
jdbc.password=123456
@Configuration
public class DruidConfig {
@Bean
@ConfigurationProperties(prefix = "jdbc")
public DataSource dataSource() {
return new DruidDataSource();
}
}
<!--pom.xml -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
# src/resources/application.properties
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/bos
spring.datasource.username=root
spring.datasource.password=123456
启动类跑main方法时候,查看SpringApplication构造方法,如下追踪
容易发现,它是从 META-INF/spring.factories 中获取类名信息,存储在一键多值的Map中,打开spring.factories,debug对比
发现键是文件蓝色部分,值是绿色部分,往回看不难发现它将这些获取的类都生成了实例,注入到IOC容器中。
打开 DataSourceProperties 发现这不是方式二吗?
点进DataSourceProperties.class
当我们添加依赖后,执行启动类时自动加载DataSourceAutoConfiguration,读取DataSourceProperties类,根据默认的前缀spring.datasource在application.xml中读取信息
掌握这些springboot的配置方式,会让你在工作中处理事情来更轻松,觉得文章对你有帮助的话还请给我点个赞,你的支持,就是我创作最大的动力!
掌握这些springboot的配置方式,让你工作效率翻个倍!
标签:文件中 cimage factor ogr res mys 自动调用 ruid name
原文地址:https://blog.51cto.com/14966465/2542899