springBoot屬性注入的四種方法:
以注入jdbc數據源爲例
1.首先在resources下面創建application.properties文件,並添加jdbc數據源屬性
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/test
jdbc.username=root
jdbc.password=test
2.配置需要讀取數據源的properties類
創建類JdbcProperties
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties("jdbc")
public class JdbcProperties {
private String driverClassName;
private String url;
private String username;
private String password;
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
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;
}
}
這裏需要引入@ConfigurationProperties("jdbc"),也就是你在applican中設置的前綴,也可以寫成@ConfigurationProperties(prefix="jdbc")
在引入的文件中其實就是按照set方式注入的屬性,同時取值的時候,通過get方法取值,所以get和set方法不能忘記添加。
3.然後就是四種注入屬性的方式了:
3.1第一種方式就是按照自動注入的方式,也就是@Autowired
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import javax.sql.DataSource;
@Configuration
@EnableConfigurationProperties(JdbcProperties.class)
public class JbdcConfiguration {
@Autowired
private JdbcProperties jdbcProperties;
@Bean
public DataSource dataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(this.jdbcProperties.getDriverClassName());
dataSource.setUrl(this.jdbcProperties.getUrl());
dataSource.setUsername(this.jdbcProperties.getUsername());
dataSource.setPassword(this.jdbcProperties.getPassword());
return dataSource;
}
}
注意需要引入@EnableConfigurationProperties(JdbcProperties.class),這個JdbcProperties是在上面創建的那個類
這樣就實現了第一種屬性注入的方式。
3.2第二種屬性注入的方式爲構造方法注入的方式
package com.w.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import javax.sql.DataSource;
@Configuration
@EnableConfigurationProperties(JdbcProperties.class)
public class JConfiguration {
private JdbcProperties jdbcProperties;
public JConfiguration(JdbcProperties jdbcProperties) {
this.jdbcProperties = jdbcProperties;
}
@Bean
public DataSource dataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(this.jdbcProperties.getDriverClassName());
dataSource.setUrl(this.jdbcProperties.getUrl());
dataSource.setUsername(this.jdbcProperties.getUsername());
dataSource.setPassword(this.jdbcProperties.getPassword());
return dataSource;
}
}
3.3第三種就是通過形參引入的方法注入屬性
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import javax.sql.DataSource;
@Configuration
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfiguration {
@Bean
public DataSource dataSource(JdbcProperties jdbcProperties){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(jdbcProperties.getDriverClassName());
dataSource.setUrl(jdbcProperties.getUrl());
dataSource.setUsername(jdbcProperties.getUsername());
dataSource.setPassword(jdbcProperties.getPassword());
return dataSource;
}
}
3.4第四種方式就是通過引入標籤的方式,這是最簡單的方式,但是前提是對應的類中需要有set方法將值可以注入進去纔可以。
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import javax.sql.DataSource;
@Configuration
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfiguration {
@Bean
@ConfigurationProperties("jdbc")
public DataSource dataSource(){
DruidDataSource dataSource = new DruidDataSource();
return dataSource;
}
}