zoukankan      html  css  js  c++  java
  • springBoot的四种属性注入

      一、Autowired注入方式(以注入连接池为例

             1. 创建一个属性读取类:JdbcProperties 

    /**
     * 属性读取类
     *  prefix: 为application.properties文件中的前缀 
     */
    @ConfigurationProperties(prefix = "jdbc")  //读取配置文件,声明一个类是读取配置类
    public class JdbcProperties {
        private String driverClassName;  //配置文件中的属性名称
        private String url;
        private String username;
        private String password;
       
        //get、set方法
    }

                 *  在类上通过@ConfigurationProperties注解声明当前类为属性读取类

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

                 *  在类上定义各个属性,名称必须与属性文件中jdbc.后面部分一致,并且必须具有getter和setter方法

                 * SpringBoot默认会读取文件名为application.properties的资源文件,所以配置文件名必须为application.properties

             2.创建另一个类JdbcConfiguration使用这个属性

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

                  *  然后你可以通过以下方式在JdbcConfiguration类中注入JdbcProperties: 

                     1)  @Autowired注入                       

    @Configuration //声明一个类为Java配置类,相当于一个xml文件
    @EnableConfigurationProperties(JdbcProperties.class)  //启用属性读取类
    public class JdbcConfiguration {
        @Autowired
        private JdbcProperties jdbcProperties;  //注入读取类
    
        @Bean        //把方法返回值注入到spring容器
        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;
        }
    }

                  2)构造函数注入

              

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

               3)@Bean方法的参数注入          

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

               4)@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;
        }
    }

                    

  • 相关阅读:
    prase arraylist where in to ParamsQuery on sql server 2008 using TVB: tvpdemo.cs(where in 转sql 参数化查询)
    手动依赖性注入 NInject(1) (转载)
    单件(单态,Singleton)模式部分
    详解Javascript中的Url编码/解码
    学习Web应用漏洞最好的教程WebGoat(转载)
    百度网盘 邀请码
    colorbox去除close关闭按钮,附上colorbox的基本使用方法
    P3974 [TJOI2015]组合数学
    P1772 [ZJOI2006]物流运输
    P1434 [SHOI2002]滑雪
  • 原文地址:https://www.cnblogs.com/cqyp/p/13229957.html
Copyright © 2011-2022 走看看