读取配置文件方式

方式一: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);
    }
相关推荐
helloworld工程师13 分钟前
Spring AI应用:利用DeepSeek+嵌入模型+Milvus向量数据库实现检索增强生成--RAG应用(超详细)
人工智能·spring·milvus
我命由我123451 小时前
35.Java线程池(线程池概述、线程池的架构、线程池的种类与创建、线程池的底层原理、线程池的工作流程、线程池的拒绝策略、自定义线程池)
java·服务器·开发语言·jvm·后端·架构·java-ee
CopyLower2 小时前
分布式ID生成方案的深度解析与Java实现
java·开发语言·分布式
_zsw5 小时前
Spring三级缓存学习
学习·spring·缓存
m0_684598535 小时前
如何开发英语在线训练小程序:从0到1的详细步骤
java·微信小程序·小程序·小程序开发
ml130185288745 小时前
开发一个环保回收小程序需要哪些功能?环保回收小程序
java·大数据·微信小程序·小程序·开源软件
Charlie__ZS6 小时前
SpringCloud - 分布式事务
分布式·spring·spring cloud
zybishe6 小时前
免费送源码:Java+ssm+MySQL 酒店预订管理系统的设计与实现 计算机毕业设计原创定制
java·大数据·python·mysql·微信小程序·php·课程设计
anlogic7 小时前
Java基础 4.12
java·开发语言
weisian1518 小时前
Java常用工具算法-7--秘钥托管云服务2(阿里云 KMS)
java·安全·阿里云