05-SpringBoot @ConfigurationProperties、@PropertySource、@Import

1.@ConfigurationProperties和@PropertySource

如何使用Java读取到properties文件中的内容,并且把它封装到JavaBean中,以供随时使用

05-SpringBoot @ConfigurationProperties、@PropertySource、@Import_第1张图片

user1.properties

user.id=1
user.username=lucy
user.password=111222

1.传统方法

public static void  getProperties(){
        Properties pps = new Properties();
        try {
            pps.load(new FileInputStream("D:\\java-oxygen\\IDEA CODE\\springboottest1\\src\\main\\resources\\user1.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Enumeration enum1 = pps.propertyNames();//得到配置文件的名字
        while(enum1.hasMoreElements()) {
            String strKey = (String) enum1.nextElement();
            String strValue = pps.getProperty(strKey);
            System.out.println(strKey + "=" + strValue);
            //封装到JavaBean。
            //......
        }
    }

05-SpringBoot @ConfigurationProperties、@PropertySource、@Import_第2张图片
这种传统方法真的很麻烦

2.注解方法

方法1.@ConfigurationProperties + @Component

User

package com.limi.entity;


import org.springframework.stereotype.Component;

//只有在容器中的组件,才会拥有SpringBoot提供的强大功能
@Component
public class User {

    private Integer id;

    private String userName;

    private String passWord;

    public User(){}

    public User(Integer id, String userName, String passWord) {
        this.id = id;
        this.userName = userName;
        this.passWord = passWord;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", passWord='" + passWord + '\'' +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    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;
    }
}

如果属性值不在application.properties中, 则需使用@PropertySources导入properties文件
MyConfig

package com.limi.config;


import com.limi.entity.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;


@Configuration
// 默认取application.properties, 需要读取多个properties可以用@PropertySources设置
@PropertySource(value = {"user1.properties"})
public class MyConfig {

    @Bean
    public User user1(){
        return new User();
    }
}

MainApplication

package com.limi;

import com.limi.entity.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;


@SpringBootApplication
public class MainApplication {

    public static void main(String[] args) {

        //1、返回我们IOC容器
        ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);

        User user1 = (User)run.getBean("user1");
        System.out.println(user1);

    }
}

05-SpringBoot @ConfigurationProperties、@PropertySource、@Import_第3张图片

方法2.@EnableConfigurationProperties + @ConfigurationProperties

User

package com.limi.entity;

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

@ConfigurationProperties(prefix = "user")
public class User {

    private Integer id;

    private String userName;

    private String passWord;

    public User(){}

    public User(Integer id, String userName, String passWord) {
        this.id = id;
        this.userName = userName;
        this.passWord = passWord;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", passWord='" + passWord + '\'' +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    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;
    }
}

MyConfig

package com.limi.config;


import com.limi.entity.User;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;


@Configuration
@EnableConfigurationProperties(User.class)
// 默认取application.properties, 需要读取多个properties可以用@PropertySources设置
@PropertySource(value = {"user1.properties"})
public class MyConfig {

    @Bean
    public User user1(){
        return new User();
    }
}

MainApplication

package com.limi;

import com.limi.entity.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;


@SpringBootApplication
public class MainApplication {

    public static void main(String[] args) {

        //1、返回我们IOC容器
        ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);

        User user1 = (User)run.getBean("user1");
        System.out.println(user1);

    }
}

05-SpringBoot @ConfigurationProperties、@PropertySource、@Import_第4张图片

2.@Import

这个注解的功能就是将某个类创建为组件导入到IOC容器中

  • @Bean、@Component、@Controller、@Service、@Repository,它们是Spring的基本标签,在Spring
    Boot中并未改变它们原来的功能。
  • @Import({User.class, DBHelper.class})给容器中自动创建出这两个类型的组件、默认组件的名字就是全类名

User

package com.limi.entity;

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

@ConfigurationProperties(prefix = "user")
public class User {

    private Integer id;

    private String userName;

    private String passWord;

    public User(){}

    public User(Integer id, String userName, String passWord) {
        this.id = id;
        this.userName = userName;
        this.passWord = passWord;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", passWord='" + passWord + '\'' +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    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;
    }
}

MyConfig

package com.limi.config;


import com.limi.entity.User;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;


@Configuration
@EnableConfigurationProperties(User.class)
// 默认取application.properties, 需要读取多个properties可以用@PropertySources设置
@PropertySource(value = {"user1.properties"})
@Import(User.class)
public class MyConfig {

}

MainApplication

package com.limi;

import com.limi.entity.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;


@SpringBootApplication
public class MainApplication {

    public static void main(String[] args) {

        //1、返回我们IOC容器
        ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);

        User user1 = (User)run.getBean("com.limi.entity.User");
        System.out.println(user1);

    }
}

05-SpringBoot @ConfigurationProperties、@PropertySource、@Import_第5张图片

3.项目完整代码

05-SpringBoot @ConfigurationProperties、@PropertySource、@Import_第6张图片
application.properties

# 设置端口号,如果不设置,默认是8080
server.port=8081

user1.properties

user.id=1
user.username=lucy
user.password=111222

User

package com.limi.entity;

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

@ConfigurationProperties(prefix = "user")
public class User {

    private Integer id;

    private String userName;

    private String passWord;

    public User(){}

    public User(Integer id, String userName, String passWord) {
        this.id = id;
        this.userName = userName;
        this.passWord = passWord;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", passWord='" + passWord + '\'' +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    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;
    }
}

MyConfig

package com.limi.config;


import com.limi.entity.User;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;


@Configuration
@EnableConfigurationProperties(User.class)
// 默认取application.properties, 需要读取多个properties可以用@PropertySources设置
@PropertySource(value = {"user1.properties"})
@Import(User.class)
public class MyConfig {

}

MainApplication

package com.limi;

        import com.limi.entity.User;
        import org.springframework.boot.SpringApplication;
        import org.springframework.boot.autoconfigure.SpringBootApplication;
        import org.springframework.context.ConfigurableApplicationContext;

        import java.io.FileInputStream;
        import java.io.IOException;
        import java.util.Enumeration;
        import java.util.Properties;


@SpringBootApplication
public class MainApplication {

    public static void main(String[] args) {

        //1、返回我们IOC容器
        ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);

        User user1 = (User)run.getBean("com.limi.entity.User");
        System.out.println(user1);

    }
}

你可能感兴趣的:(SpringBoot入门,spring,boot,java,intellij-idea)