读取配置文件方式

方式一:XML

配置方式一:

java 复制代码
    <!--引入外部属性文件1-->
    <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
        <property name="locations" value="classpath:jdbc.properties"/>
    </bean>

配置方式一:

java 复制代码
<!--引入外部属性文件2-->
    <context:property-placeholder location="classpath:jdbc.properties"/>

配置文件:

bash 复制代码
prop.driverClass=com.mysql.jdbc.Driver
prop.url=jdbc:mysql://localhost:3306/userDb
prop.userName=root
prop.password=abc123

\

动态取值:

bash 复制代码
    <!--配置连接池--> 
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="url" value="${prop.url}"/>
        <property name="username" value="${prop.userName}"/>
        <property name="password" value="${prop.password}"/>
        <property name="driverClassName" value="${prop.driverClass}"/>
    </bean>

方式一:@ConfigurationProperties(prefix = "xxx")+@Component

java 复制代码
@ConfigurationProperties(prefix = "person")
@Component
@Data
public class Person {
    private String userName;
    private Boolean boss;
    private Date birth;
    private Integer age;
    private Pet pet;
    private String[] interests;
    private List<String> animal;
    private Map<String, Object> score;
    private Set<Double> salarys;
    private Map<String, List<Pet>> allPets;
}

配置文件:application.properties

bash 复制代码
person.user-name=zhangsan
person.boss=true
person.birth=2019/12/9
person.age=18
person.pet.name=阿狗
person.pet.weight=99.99
person.interests=篮球,足球
person.animal=阿猫,阿狗
person.score.english=80
person.score.math=80
person.salarys=9999.98,9999.99
person.all-pets.sick[0].name=阿狗;
person.all-pets.sick[0].weight=99.99
person.all-pets.health[0].name=阿花
person.all-pets.health[1].name=阿明
person.all-pets.health[0].weight=199.99
person.all-pets.health[1].weight=200

#https://blog.csdn.net/qq_35754073/article/details/136606186

测试:

java 复制代码
    @Autowired
    Person person;
    
    @Test
    void test() {
        System.out.println(person);
    }

方式三:@ConfigurationProperties+@EnableConfigurationProperties(xxx.class)

java 复制代码
@ConfigurationProperties(prefix = "person")
@Data
public class Person {
    private String userName;
    private Boolean boss;
    private Date birth;
    private Integer age;
    private Pet pet;
    private String[] interests;
    private List<String> animal;
    private Map<String, Object> score;
    private Set<Double> salarys;
    private Map<String, List<Pet>> allPets;
}
java 复制代码
@EnableConfigurationProperties(Person.class)
@Configuration
public class MyConfig {
    
    @Bean
    public Person person(Person person) {
        System.out.println(person);
        return person;
    }
}

测试:

java 复制代码
@Autowired
MyConfig myConfig;

方式四:@Value+application.properties

配置文件:

bash 复制代码
myconfig.name=1111
myconfig.pwd=2222

测试:application.properties

java 复制代码
    @Value("${myconfig.name}")
    private String name;

    @Value("${myconfig.pwd}")
    private String pwd;

    @Test
    void test2() {
        System.out.println(name);
        System.out.println(pwd);
    }

方式四:@Value+xxx.properties自定义配置文件

配置:other.properties

bash 复制代码
other.name=3333
other.pwd=4444
java 复制代码
@PropertySources({
        @PropertySource("classpath:other.properties")
})
@Configuration
public class OtherConfig {
   

测试:

java 复制代码
    @Value("${other.name}")
    private String otherName;

    @Value("${other.pwd}")
    private String otherPwd;

    @Test
    void test3() {
        System.out.println(otherName);
        System.out.println(otherPwd);
    }
相关推荐
我命由我123452 小时前
Kotlin 数据容器 - List(List 概述、创建 List、List 核心特性、List 元素访问、List 遍历)
java·开发语言·jvm·windows·java-ee·kotlin·list
武子康4 小时前
Java-80 深入浅出 RPC Dubbo 动态服务降级:从雪崩防护到配置中心秒级生效
java·分布式·后端·spring·微服务·rpc·dubbo
YuTaoShao7 小时前
【LeetCode 热题 100】131. 分割回文串——回溯
java·算法·leetcode·深度优先
源码_V_saaskw7 小时前
JAVA图文短视频交友+自营商城系统源码支持小程序+Android+IOS+H5
java·微信小程序·小程序·uni-app·音视频·交友
超浪的晨7 小时前
Java UDP 通信详解:从基础到实战,彻底掌握无连接网络编程
java·开发语言·后端·学习·个人开发
双力臂4048 小时前
Spring Boot 单元测试进阶:JUnit5 + Mock测试与切片测试实战及覆盖率报告生成
java·spring boot·后端·单元测试
心之语歌8 小时前
Spring AI MCP 客户端
人工智能·spring·github
Edingbrugh.南空8 小时前
Aerospike与Redis深度对比:从架构到性能的全方位解析
java·开发语言·spring
QQ_4376643149 小时前
C++11 右值引用 Lambda 表达式
java·开发语言·c++
永卿0019 小时前
设计模式-迭代器模式
java·设计模式·迭代器模式