spring加载属性(properties)文件

spring加载属性(properties)文件

 

 

 一、注解方式加载

jdbc.driver=org.mariadb.jdbc.Driver
jdbc.url=jdbc:mariadb://localhost:3306/kt
jdbc.user=root
jdbc.password=12345

创建配置类:

package com.wbg.springAnnotaion.config;

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

@Configuration
//ignoreResourceNotFound 为false时候找不到会忽略
@PropertySource(value = {"classpath:database-config.properties"},ignoreResourceNotFound = true)
public class ApplicationConfig {
}
View Code

测试:

  ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
        String url=context.getEnvironment().getProperty("jdbc.url");
        System.out.println(url);
View Code

使用PropertySourcesPlaceholderConfigurer注解来占位符

1、在原来类上加代码

package com.wbg.springAnnotation.annotation.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

@Configuration
@ComponentScan(basePackages = {"com.wbg.springAnnotation.annotation"})
@PropertySource(value = {"classpath:database-config.properties"},ignoreResourceNotFound = true)
public class ApplicationConfig {
    @Bean
    public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(){
        return new PropertySourcesPlaceholderConfigurer();
    }
}
View Code

 

2、创建DataSourceBean类:

类上的@Value注解使用$占位符

package com.wbg.springAnnotation.annotation.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class DataSourceBean {
    @Value("${jdbc.driver}")
    private String driver = null;
    @Value("${jdbc.url}")
    private String url = null;
    @Value("${jdbc.user}")
    private String user = null;
    @Value("${jdbc.password}")
    private String password = null;

    @Override
    public String toString() {
        return "DataSourceBean{" +
                "driver='" + driver + '\'' +
                ", url='" + url + '\'' +
                ", user='" + user + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

    @Bean("dataSource")
    public String getDataSourceBean(){
        System.out.println(toString());
        return this.toString();
    }
}
View Code

测试:

二、使用xml进行加载:

创建xml文件:

database-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.wbg.springAnnotation.annotation"/>
    <!--ignore-resource-not-found:true允许不存在,否则异常-->
    <context:property-placeholder
            ignore-resource-not-found="true"
                                  location="classpath:database-config.properties"/>
</beans>
View Code

测试:

 如果配置多个:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <array>
                <value> classpath:database-config.properties </value>
                <value> classpath:log4j.properties </value>
            </array>
        </property>
        <property name="ignoreResourceNotFound" value="true"/>
    </bean>
View Code

测试:

 

 demo:https://github.com/weibanggang/springannotationproperties.git

 

posted @ 2018-12-16 21:24 韦邦杠 阅读( ...) 评论( ...) 编辑 收藏
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值