springboot属性注入

方法一:

1.创建application.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/leyou
jdbc.username=root
jdbc.password=123

2.创建属性类JdbcProperties

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@ConfigurationProperties(prefix = "jdbc")  //指明前缀
@Data
public class JdbcProperties {
    String url;
    String driverClassName;
    String username;
    String password;
}

@ConfigurationProperties:加载外部配置文件,可以把同类的配置信息自动封装成实体类

                                          类的属性名称必须与外部属性的名称匹配

                                          类的字段必须有公共 setter 方法

@Data:这个注解是lombok包下的一个注解,只要你的类上写了这个注解,那就不需要再生成get、set、toString等方法了。使用前要安装插件!


3.装载配置信息

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfig {
     @Bean
     public DataSource dataSource(JdbcProperties prop) {
         DruidDataSource dataSource = new DruidDataSource();
         dataSource.setDriverClassName(prop.getDriverClassName());
         dataSource.setUrl(prop.getUrl());
         dataSource.setUsername(prop.getUsername());
         dataSource.setPassword(prop.getPassword());
         return dataSource;
     }
}

@Configuration:相当于xml中的beans

如果一个配置类只配置@ConfigurationProperties注解,而没有使用@Component,那么在IOC容器中是获取不到properties 配置文件转化的bean。说白了 @EnableConfigurationProperties 相当于把使用 @ConfigurationProperties 的类进行了一次注入。

4.测试 编写Controller与引导类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.sql.DataSource;

@Controller
public class HelloController {

    @Autowired
    private DataSource dataSource;

    @GetMapping("hello")
    @ResponseBody
    public String hello(){
        return "hello";
    }
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

结果:

方法二:

@Data
public class JdbcProperties {
    String url;
    String driverClassName;
    String username;
    String password;
}
@Configuration  //相当于xml中<beans>
public class JdbcConfig {
     @Bean //相当于<bean>
     @ConfigurationProperties(prefix = "jdbc")
     public DataSource dataSource() {
         return new DruidDataSource();
     }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值