zoukankan      html  css  js  c++  java
  • SpringBoot 加载 properties

    加载一个

    xml

    <context:property-placeholder ignore-unresolvable="true" location="classpath:abc.properties"/>

    java

    @PropertySource("classpath:app.properties")

    加载多个

    xml

    <context:property-placeholder location="classpath:/prop/*.properties" ignore-resource-not-found="true"/>
    
    
    <context:property-placeholderlocation="classpath:a.properties,classpath:b.properties" />
    
    
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:/prop/a.properties</value>
                <value>classpath:/prop/b.properties</value>
                <value>classpath:/prop/c.properties</value>
            </list>
        </property>
        <property name="ignoreResourceNotFound" value="true"/>
    </bean>

    java

    @PropertySources({
            @PropertySource(value = "classpath:/prop/a.properties", ignoreResourceNotFound = true),
            @PropertySource(value = "classpath:/prop/b.properties", ignoreResourceNotFound = true),
            @PropertySource(value = "classpath:/prop/c.properties", ignoreResourceNotFound = true)
    })

    取值

    xml

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">  
        <property name="driverClass" value="#{jdbc.driverClass}" />
        <property name="jdbcUrl" value="#{jdbc.jdbcUrl}" />
        <property name="user" value="#{jdbc.user}" />
        <property name="password" value="#{jdbc.password}" />
    </bean>
    
    
    
    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
        <include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
    
        <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <fileNamePattern>${catalina.base:-.}/logs/app.%d{yyyy-MM-dd}.log</fileNamePattern>
                <maxHistory>30</maxHistory>
            </rollingPolicy>
            <encoder>
                <pattern>${FILE_LOG_PATTERN}</pattern>
                <charset>UTF-8</charset>
            </encoder>
        </appender>
    
        <root level="DEBUG">
            <appender-ref ref="CONSOLE"/>
        </root>
        <root level="INFO">
            <appender-ref ref="FILE"/>
        </root>
    </configuration>

    java

    /**
     * @Value 赋值;
     * 1、基本数值
     * 2、可以写 SpEL; #{}
     * 3、可以写 ${};取出配置文件【properties】中的值(在运行环境变量里面的值)
     */
     
    @Value("#{jdbc.url}")
    private String url;
    
    @Value("#{jdbc}")
    public void setJdbcConf(Properties jdbc){
        url= sys.getProperty("url");
    }
    
    @Value("张三")
    private String name;
    
    @Value("#{20-2}")
    private Integer age;
    
    @Value("${people.nickName}")
    private String nickName;
    
    @Value("${db.driver:com.microsoft.sqlserver.jdbc.SQLServerDriver}")
    private String driver;
    
    public static void main(String[] args) {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(PropertyValuesConfig.class);
    
        printBeans(applicationContext);
    
        People person = (People) applicationContext.getBean("people");
        System.out.println(person);
    
        Environment environment = applicationContext.getEnvironment();
        String property = environment.getProperty("people.nickName");
        System.out.println(property);
    
        ((AnnotationConfigApplicationContext) applicationContext).close();
    }
    
    private static void printBeans(ApplicationContext applicationContext) {
        // 打印 IOC 容器中所有 bean 的名称
        String[] definitionNames = applicationContext.getBeanDefinitionNames();
        for (String name : definitionNames) {
            System.out.println(name);
        }
        System.out.println("===================================");
    }

    https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-value-annotations

    https://blog.csdn.net/f641385712/article/details/84452191

  • 相关阅读:
    (转)OpenCV提取视频每一帧及将连续图片合成视频
    OpenCV对图像的性能测试
    (转)OpenCV中的常用函数
    EasyX-加载图像实现人物行走
    “三行情书”——给你三行代码的爱恋~
    EasyX—模拟小球自由落体
    maven的pom.xml配置文件
    spring常用注解
    sql优化
    idea快捷键
  • 原文地址:https://www.cnblogs.com/jhxxb/p/13588045.html
Copyright © 2011-2022 走看看