spring的属性注入

Posted 西以北偏北

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring的属性注入相关的知识,希望对你有一定的参考价值。

 

1.指定配置文件,创建jdbc.properties

jdbc.driver = com.mysql.jdbc.driver
jdbc.url = jdbc:mysql://localhost:3306/demo
jdbc.username = root
jdbc.password = root

2.创建一个配置类,配置类中完成属性的注入

//完成自动配置的三种形式,xml,注解,配置
@Configuration
//将配置文件的值读取到spring value值是文件路径
@PropertySource(value = "jdbc.properties")
public class DruidConf {

//从spring环境中读取每一个键值对,并且赋值于成员变量,使用value取值,取值方式spel,spring的el表达式
@Value("${jdbc.driver}")
String driver;
@Value("${jdbc.url}")
String url;
@Value("${jdbc.username}")
String username;
@Value("${jdbc.password}")
String password;



//@Bean 将结果交给工厂管理
@Bean
public DruidDataSource getDruidDataSources(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
}

3.处理器中的使用

@RestController
public class TestController {

@Autowired
DruidDataSource druidDataSource;

@RequestMapping("/test")
public void test(){
System.out.println("==============");
}
}

SpringBoot属性的注入

1.yml文件的书写

jdbc:
driver: com.mysql.jdbc.driver
url: jdbc:mysql://localhost:3306/demo
username: root
password: root

2.属性配置类

package com.nylg.conf;

import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.Objects;
//属性配置类,
prefix前置配置
@ConfigurationProperties(prefix = "jdbc") public class DruidConfSpringBoot { String driver; String url; String username; String password; public String getDriver() { return driver; } public void setDriver(String driver) { this.driver = driver; } 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; } public DruidConfSpringBoot() { } public DruidConfSpringBoot(String driver, String url, String username, String password) { this.driver = driver; this.url = url; this.username = username; this.password = password; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; DruidConfSpringBoot that = (DruidConfSpringBoot) o; return Objects.equals(driver, that.driver) && Objects.equals(url, that.url) && Objects.equals(username, that.username) && Objects.equals(password, that.password); } @Override public int hashCode() { return Objects.hash(driver, url, username, password); } @Override public String toString() { return "DruidConfSpringBoot{" + "driver=\'" + driver + \'\\\'\' + ", url=\'" + url + \'\\\'\' + ", username=\'" + username + \'\\\'\' + ", password=\'" + password + \'\\\'\' + \'}\'; } }

 

3.引入jar,重启项目

<!--    配置文件提示-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-configuration-processor</artifactId>
      <optional>true</optional>
    </dependency>

 

4.核心配置类

@Configuration//引入哪个属性配置类
@EnableConfigurationProperties(DruidConfSpringBoot.class)
public class DruidConf {
   @Autowired
   DruidConfSpringBoot druidConfSpringBoot;

    //@Bean 将结果交给工厂管理
    @Bean
    public DruidDataSource getDruidDataSources(){
        DruidDataSource dataSource = new DruidDataSource();
     //通过对象获取属性 dataSource.setDriverClassName(druidConfSpringBoot.getDriver()); dataSource.setUrl(druidConfSpringBoot.getUrl()); dataSource.setUsername(druidConfSpringBoot.getUsername()); dataSource.setPassword(druidConfSpringBoot.getPassword());
return dataSource; } }

 

属性配置过程

 

 



以上是关于spring的属性注入的主要内容,如果未能解决你的问题,请参考以下文章

spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段

spring autowired怎么按照name注入

Spring依赖注入的方式

Spring配置文件中注入复杂类型属性

spring使用注解,自动扫描类的时候怎么注入

Spring根据XML配置文件注入对象类型属性