zoukankan      html  css  js  c++  java
  • spring boot 注入属性的几种方式

    1.@PropertySource:指定属性文件的路径  如 classpath:jdbc.properties

    @Configuration
    @PropertySource("classpath:jdbc.properties")
    public class JdbcConfiguration {
    
        @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.setUrl(url);
            dataSource.setDriverClassName(driverClassName);
            dataSource.setUsername(username);
            dataSource.setPassword(password);
            return dataSource;
        }
    }

    2. 通过@ConfigurationProperties注解声明当前类为属性读取类  

          prefix="jdbc"读取属性文件中,前缀为jdbc的值。

    没有指定属性文件的地址,SpringBoot默认会读取文件名为application.properties的资源文件

    @ConfigurationProperties(prefix = "jdbc")
    public class JdbcProperties {
        private String url;
        private String driverClassName;
        private String username;
        private String password;
      
    }

       通过@EnableConfigurationProperties(JdbcProperties.class)来声明要使用JdbcProperties这个类的对象

       Autowired 注入 

    @Configuration
    @EnableConfigurationProperties(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;
        }
    
    }

      构造函数注入 

    @Configuration
    @EnableConfigurationProperties(JdbcProperties.class)
    public class JdbcConfiguration {
    
        private JdbcProperties jdbcProperties;
    
        public JdbcConfiguration(JdbcProperties jdbcProperties){
            this.jdbcProperties = jdbcProperties;
        }
    
        @Bean
        public DataSource dataSource() {
            //
        }
    
    }

    @Bean方法的参数注入

    @Configuration
    @EnableConfigurationProperties(JdbcProperties.class)
    public class JdbcConfiguration {
    
        @Bean
        public DataSource dataSource(JdbcProperties jdbcProperties) {
            // ...
        }
    }

    3    我们直接把@ConfigurationProperties(prefix = "jdbc")声明在需要使用的@Bean的方法上,然后SpringBoot就会自动调用这个Bean(此处是DataSource)的set方法,然后完成注入。使用的前提是:该类必须有对应属性的set方法!

    @Configuration
    public class JdbcConfiguration {
        
        @Bean
        // 声明要注入的属性前缀,SpringBoot会自动把相关属性通过set方法注入到DataSource中
        @ConfigurationProperties(prefix = "jdbc")
        public DataSource dataSource() {
            DruidDataSource dataSource = new DruidDataSource();
            return dataSource;
        }
    }

        

  • 相关阅读:
    javascript中replace()
    防止IE6出现BUG的十种常见解决方法
    IE6 重复字符的bug
    IE6 BUG大全
    display:inline
    JavaScript 图片上传预览效果
    用一行代码让w3wp进程崩溃,如何查找w3wp进程崩溃的原因
    近期学习任务
    气死我的存储过程和用户定义函数
    Damn,China Mobile!!!!
  • 原文地址:https://www.cnblogs.com/qin1993/p/12639456.html
Copyright © 2011-2022 走看看