springboot的配置变量

获取配置信息

如果是被spring容器所管理,一般采用@Value注解就可以读取到application.yml文件的内容。

但是不被spring容器管理,使用@Value是无效的。注入时会是NULL。但就是会有这样的场景,此时可采用在springboot启动类中将环境注入到某一个公共配置类,该类所有属性为公共静态属性攻外部类直接使用。

java 复制代码
ConfigurableApplicationContext run = SpringApplication.run(DemoStarterTestApplication.class, args);
        ConfigurableEnvironment environment = run.getEnvironment();
        String address = environment.getProperty("server.address1");
        int age = Integer.parseInt(environment.getProperty("spring.user.age"));

原文链接:https://blog.csdn.net/weixin_42740540/article/details/124348851

继承PropertyResolver,里面可以操作配置参数

JDBC操作获取系统变量

    InputStream is =        ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
	Properties pros = new Properties();
	pros.load(is);
	String user = pros.getProperty("user");
	String password = pros.getProperty("password");
	String url = pros.getProperty("url");
	String driverClass = pros.getProperty("driverClass");


@SpringBootApplication
public class MybatisPlusDemoApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext run= SpringApplication.run(MybatisPlusDemoApplication.class, args);
        ConfigurableEnvironment environment = run.getEnvironment();
        String address = environment.getProperty("server.address1");
        String username = environment.getProperty("spring.datasource.username");
        int age = Integer.parseInt(environment.getProperty("spring.user.age"));
        String name = environment.getProperty("server.name");
        EnvConfig envConfig = new EnvConfig();
        envConfig.setAddress(address);
        envConfig.setAge(age);
        System.out.println("地址为"+EnvConfig.address+",年龄为"+EnvConfig.age+"名字是:"+name);

        //获取ioc容器中的组件名字并打印出来
        String[] userBeanNames = run.getBeanNamesForType(User.class);
        for (String s : userBeanNames){
            System.out.println("---user:"+s);
        }
        // 判断id为user01的对象是否存在
        boolean b = run.containsBean("user01");
        System.out.println(b);
        // 直接获取容器当中的user对象
        User bean = run.getBean(User.class);
        System.out.println(bean);


        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

        context.getEnvironment().setActiveProfiles("like");
        context.register(AppConfigWithActiveProfile.class, AppConfigWithInactiveProfile.class);
        context.refresh();
        Subject subject = (Subject) context.getBean("subject");
        System.out.println("subject = " + subject);

        // ------------------------------ 测试 DependsOn  ------------------------------
        AnnotationConfigApplicationContext contextDependsOn = new AnnotationConfigApplicationContext(AppConfigWithDependsOn.class);
        contextDependsOn.getBean(FirstBean.class);
        contextDependsOn.close();

    }

}

SecondBean Initialized via Constuctor

ThirdBean Initialized via Constuctor

FirstBean Initialized via Constuctor

Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.

Registered plugin: 'com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor@45792847'

Registered plugin: 'com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor@706cb08'

Parsed mapper file: 'file [D:\idea\gitDowload\WangPai1Zu\code\MybatisPlusDemo\target\classes\mapper\UserMapper.xml]'

_ _ |_ _ |. ___ _ | _

| | |\/|)(| | |\ |)|||\

/ |

3.5.1

地址为aa,年龄为19名字是:aa

---user:user01

true

User(id=null, name=null, createTime=null, updateTime=null, age=null, email=null)

subject = Subject{like='物理', wenke='null'}

SecondBean Initialized via Constuctor

ThirdBean Initialized via Constuctor

FirstBean Initialized via Constuctor

public class EnvConfig {
    public static String address;

    public static int age;

    public  void setAddress(String address) {
        EnvConfig.address = address;
    }

    public  void setAge(int age) {
        EnvConfig.age = age;
    }

}

@Configuration
public class MyConfig {

    // 给容器中加入组件,以方法名作为组件id
    @Bean
    public User user01() {
        User user = new User();
        return user;
    }
}


@Profile("like")
@Configuration
public class AppConfigWithActiveProfile {

    @Bean
    public Subject subject(){
        Subject subject = new Subject();
        subject.setLike("物理");
        return subject;
    }

}

@Profile("wenke")
@Configuration
public class AppConfigWithInactiveProfile {

    @Bean
    public Subject subject(){
        Subject subject = new Subject();
        subject.setWenke("历史");
        return subject;
    }
}


@Data
// 学科
public class Subject {

    // 理科
    private String like;
    // 文科
    private String wenke;



    @Override
    public String toString() {
        return "Subject{" +
                "like='" + like + '\'' +
                ", wenke='" + wenke + '\'' +
                '}';
    }
}

public class FirstBean {

    @Autowired
    private SecondBean secondBean;

    @Autowired
    private ThirdBean thirdBean;

    public FirstBean() {
        System.out.println("FirstBean Initialized via Constuctor");
    }
}


@Configuration
public class AppConfigWithDependsOn {

    @Bean("firstBean")
    @DependsOn(value = {
            "secondBean",
            "thirdBean"
    })
    public FirstBean firstBean() {
        return new FirstBean();
    }

    @Bean("secondBean")
    public SecondBean secondBean() {
        return new SecondBean();
    }

    @Bean("thirdBean")
    public ThirdBean thirdBean() {
        return new ThirdBean();
    }
}

public class SecondBean {

    public SecondBean() {
        System.out.println("SecondBean Initialized via Constuctor");
    }
}
相关推荐
_oP_i1 分钟前
Pinpoint 是一个开源的分布式追踪系统
java·分布式·开源
mmsx4 分钟前
android sqlite 数据库简单封装示例(java)
android·java·数据库
武子康30 分钟前
大数据-258 离线数仓 - Griffin架构 配置安装 Livy 架构设计 解压配置 Hadoop Hive
java·大数据·数据仓库·hive·hadoop·架构
豪宇刘2 小时前
MyBatis的面试题以及详细解答二
java·servlet·tomcat
秋恬意2 小时前
Mybatis能执行一对一、一对多的关联查询吗?都有哪些实现方式,以及它们之间的区别
java·数据库·mybatis
FF在路上2 小时前
Knife4j调试实体类传参扁平化模式修改:default-flat-param-object: true
java·开发语言
真的很上进2 小时前
如何借助 Babel+TS+ESLint 构建现代 JS 工程环境?
java·前端·javascript·css·react.js·vue·html
众拾达人3 小时前
Android自动化测试实战 Java篇 主流工具 框架 脚本
android·java·开发语言
皓木.3 小时前
Mybatis-Plus
java·开发语言
不良人天码星3 小时前
lombok插件不生效
java·开发语言·intellij-idea