详解springboot的多种配置方式
猪猪の陳さん 人气:0java配置主要靠java类和一些注解,比较常用的注解有:
@Configuration :声明一个类作为配置类,代替xml文件
@Bean :声明在方法上,将方法的返回值加入Bean容器,代替 标签
@Value :基本类型或String属性注入
@PropertySource :指定外部属性文件
后面以Druid连接池配置为例,数据库名称为springboot_test
方式一
<!--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)
@Bean将 dataSource() 方法声明为一个注册Bean的方法,Spring会自动调用该方法,将方法的返回值加入Spring容器中。
方式二
<!--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. 后面部分一致。
@EnableConfigurationProperties()声明要使用的属性读取类,使用该类有三种注入方式
@Autowired注入
//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; } }
@Bean的方法参数注入(本例使用)
//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中读取信息
加载全部内容