SpringBoot拦截器注入方式


基础架构

application.properties文件内容

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db_test
jdbc.username=root
jdbc.password=root

JdbcProperties.java文件

//  读取application.properties文件中前缀为jdbc的值按属性名赋值给当前类;需要提供get和set方法
@ConfigurationProperties(prefix = "jdbc")
public class JdbcProperties {
    private String url;
    private String driverClassName;
    private String username;
    private String password;
    //  省略get和set
}

HelloController.java文件

@RestController
public class HelloController {

    @Autowired
    private JdbcConfiguration jdbcConfiguration;

    @GetMapping("hello")
    public String test(){   
        return "Hello";
    }
}

Application.java启动类

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

@Autowired按照类型注入

JdbcConfiguration.java文件

@Configuration  //声明当前类为配置类
@EnableConfigurationProperties(JdbcProperties.class)    //  读取的配置文件为JdbcProperties.class
public class JdbcConfiguration {
    @Autowired
    private JdbcProperties jdbcProperties;

    @Bean
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(jdbcProperties.getUrl());
        dataSource.setDriverClassName(jdbcProperties.getDriverClassName());
        dataSource.setUsername(jdbcProperties.getUsername());
        dataSource.setPassword(jdbcProperties.getPassword());
        return dataSource; //debug调试当前dataSource中有没有数据源配置
    }
}

按照构造方法注入

JdbcConfiguration.java文件

@Configuration  //声明当前类为配置类
@EnableConfigurationProperties(JdbcProperties.class)    //  读取的配置文件为JdbcProperties.class
public class JdbcConfiguration {
    
    private JdbcProperties jdbcProperties;

    public JdbcConfiguration(JdbcProperties jdbcProperties) {
        this.jdbcProperties = jdbcProperties;
    }
    @Bean
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(jdbcProperties.getUrl());
        dataSource.setDriverClassName(jdbcProperties.getDriverClassName());
        dataSource.setUsername(jdbcProperties.getUsername());
        dataSource.setPassword(jdbcProperties.getPassword());
        return dataSource;  //debug调试当前dataSource中有没有数据源配置
    }
}

参数注入

@Configuration  //声明当前类为配置类
@EnableConfigurationProperties(JdbcProperties.class)    //  读取的配置文件为JdbcProperties.class
public class JdbcConfiguration {

    @Bean
    public DataSource dataSource(JdbcProperties jdbcProperties) {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(jdbcProperties.getUrl());
        dataSource.setDriverClassName(jdbcProperties.getDriverClassName());
        dataSource.setUsername(jdbcProperties.getUsername());
        dataSource.setPassword(jdbcProperties.getPassword());
        return dataSource;//debug调试当前dataSource中有没有数据源配置
    }
}

按照ConfigurationProperties注入


文章作者: zrh
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 zrh !
  目录