springboot使用properties

一、方式1:

1.1.配置类:

package cn.zyq.stater.config;

import cn.zyq.stater.bean.User4;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

@Configuration
@PropertySource(value = "classpath:jdbc.properties",ignoreResourceNotFound = false)
public class Config4_PropertySource {


    @Value("${test4.username}")
    private String name;

    @Value("${test4.password}")
    private String password;




    //用Environment对象的getProperty()可以获取@PropertySource导入的properties文件内容
    //(获得运行环境中的变量<包括jvm及操作系统中的一些变量信息>)
    @Autowired
    Environment environment;

    @Bean
    public User4 createUser4(){
        String name2=environment.getProperty("test4.username");
        String password2=environment.getProperty("test4.password");
        User4 user=new User4(name2,password2);
        System.out.println(name+"::"+password);
        System.out.println(user);
        return user;
    }


}

1.2.properties文件:

 二、properties文件自动赋值:

自动将properties文件的 key-value赋值给实体类:

2.1.properties文件

springboot使用properties_第1张图片

 2.2.读取配置文件的实体类:

package cn.zyq.stater.config3.autoproperties;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
//@Value反射赋值的,没有set方法也可以, @ConfigurationProperties是使用的setter方法,并提供多级属性赋值
@ConfigurationProperties(prefix = "zyq")//用来读取一个properties配置文件
@EnableConfigurationProperties
/**
 * 当前程序启动,JDBCPropertyData一旦被扫描,
 * 由于属性读取注解生效,会自动读取 properties文件中的如下信息
 * zyq.name
 * zyq.password
 * zyq.driverClassName
 */
public class JDBCPropertyData {

    private String name;
    private String password;
    private String driverClassName;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getDriverClassName() {
        return driverClassName;
    }

    public void setDriverClassName(String driverClassName) {
        this.driverClassName = driverClassName;
    }
}

2.3.配置类扫描实体类:

主要是要给本配置类添加@PropertySource来加载jdbc.properties配置文件(否则项目的启动类自动就会扫描根包下的JDBCPropertyData类)。

package cn.zyq.stater.config3.autoproperties;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@ComponentScan(basePackageClasses = {cn.zyq.stater.config3.autoproperties.JDBCPropertyData.class})//@ComponentScan("cn.zyq.stater.bean")
//@ComponentScan配合@Component或@Service等注解创建并扫描对象到容器
// (目前因为只有User1添加了@Component所以这个扫描只针对User1生效)(User2和User3采用的是@Bean方式创建)
@PropertySource(value = "classpath:jdbc.properties",ignoreResourceNotFound = false)
public class Config8_ComponentScan {

}

你可能感兴趣的:(springboot,spring,boot,后端,java)