spring核心配置文件引入外部properties文件和另外的xml配置文件

29 篇文章 0 订阅

spring核心配置文件引入外部properties文件和另外的xml配置文件

为什么要引入外部文件

我们使用jdbc的时候,会创建一个jdbc.properties配置文件,如果我需要在spring的IOC中存入连接池对象呢?
是不是需要给链接对象的属性url username password driver ,我们应该如何在spring的配置文件中引入外部文件呢?

引入properties文件

⚫ Spring提供了读取外部properties文件的机制,使用读取到的数据为bean的属性赋值
⚫ 操作步骤

  1. 准备外部properties文件
  2. 开启context命名空间支持
    xmlns:context=“http://www.springframework.org/schema/context”
  3. 加载指定的properties文件
    < context:property-placeholder location=“classpath:filename.properties”>
  4. 使用加载的数据
    < property name=“propertyName” value=" p r o p e r t i e s N a m e " / > ⚫ 注 意 : 如 果 需 要 加 载 所 有 的 p r o p e r t i e s 文 件 , 可 以 使 用 ∗ . p r o p e r t i e s 表 示 加 载 所 有 的 p r o p e r t i e s 文 件 ⚫ 注 意 : 读 取 数 据 使 用 {propertiesName}"/> ⚫ 注意:如果需要加载所有的properties文件,可以使用*.properties表示加载所有的properties文件 ⚫ 注意:读取数据使用 propertiesName"/>properties使.propertiesproperties使{propertiesName}格式进行,其中propertiesName指properties文件中的属性名
    在这里插入图片描述

代码演示

实体类准备

我使用一个User类,有两个属性,我这个两个属性的值,从properties配置文件中读取值,通过IOC 的DI依耐注入值

package com.fs.demo;

public class User {
    private String username;
    private String password;

//    set方法让spring可以DI依耐注入属性
    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

properties配置文件

username=xiaofu
password=1234
**注意:**配置文件中username前面要加个前缀,不然赋值的时候会把你当前计算机的用户名取出来,而不是从properties中读取的,原因是因为计算机的环境变量中有个值也是username,而spring启动的时候会读取环境变量中的值,从而导致和properties中的值冲突

user.username=xiaofu
user.password=1234
applicationContext.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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">


<!--    加载context命名空间
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
-->

    <!--    引入外部properties文件
            properties中的key不要和本机的环境变量中的值冲突,因为spring在加载的时候获取计算机的环境变量中的值
           若与环境中的值冲突,读取出来的值就是计算机环境变量中设置的值
            所以,我们配置jdbc的时候一般就是jdbc.username.不会直接username

            注意:若要导入多个properties,需要在主配置文件中配置,classpath:*.properties
              <context:property-placeholder location="classpath:*.properties"/>
    -->
    <context:property-placeholder location="classpath:data.properties"/>

<!--    把user交给ioc管理-->
    <bean id="user" class="com.fs.demo.User">
    <!--      ${properties中的key名}  -->
        <property name="username" value="${user.username}"/>
        <property name="password" value="${user.password}"/>
    </bean>
</beans>
测试代码
    @Test
    public void testProperties(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        User user = (User) applicationContext.getBean("user");
        System.out.println(user);
        /*
        properties配置信息
        username=xiaofu
        password=1234
        User{username='93422', password='1234'}
        若输出结果不为properties中配置的值,这个值是我的电脑用户名,因为spring在启动的时候会读取计算机的环境变量,环境变量中会有这个username
        所以拿到的值是你环境变量中配置的值,而不是你properties中设置的值
        所以,我们在写properties的key的时候,一般会jdbc.username,以免和系统的环境变量中的username冲突,导致取出的值不是properties中的值
         */
        /*
        properties配置信息是这样的就会正常读取到值
        user.username=xiaofu
        user.password=1234
        //打印输出
        User{username='xiaofu', password='1234'}
         */
    }
控制台运行结果

在这里插入图片描述

spring核心配置文件中导入其他的.xml配置文件

团队开发
⚫ 名称:import
⚫ 类型:标签
⚫ 归属:beans标签
⚫ 作用:在当前配置文件中导入其他配置文件中的项
⚫ 格式:
< beans>
< import />
< /beans>
⚫ 基本属性:
< import resource=“config.xml"/>
◆ resource:加载的配置文件名

⚫ Spring容器加载多个配置文件
new ClassPathXmlApplicationContext(“config1.xml”,“config2.xml”);
⚫ Spring容器中的bean定义冲突问题
◆ 同id的bean,后定义的覆盖先定义的
◆ 导入配置文件可以理解为将导入的配置文件复制粘贴到对应位置
◆ 导入配置文件的顺序与位置不同可能会导致最终程序运行结果不同

代码演示

实体类

创建一个类,这个类有个实体类属性,一个set方法,用于DI依耐注入

package com.fs.demo;

public class Demo {
    private TestDemo testDemo;

    public void setTestDemo(TestDemo testDemo) {
        this.testDemo = testDemo;
    }

    @Override
    public String toString() {
        return "Demo{" +
                "testDemo=" + testDemo +
                '}';
    }
}

创建一个类,这个类是上一个类的属性

package com.fs.demo;

public class TestDemo {
    public void testDemo(){
        System.out.println("TestDemo测试语句输出");
    }
}

applicationContext.xml配置文件

applicationContext.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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

<!--    引入外部的spring的配置文件-->
    <import resource="classpath:applicationContext02.xml"/>

    <bean id="demo" class="com.fs.demo.Demo">
<!--        这里就体现了引入外部的applicationContext02.xml文件,因为ref="testDemo这就是在applicationContext02配置的-->
        <property name="testDemo" ref="testDemo"/>
    </bean>
</beans>

applicationContext02.xml其他的.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!--    把test交给springIOC管理-->
    <bean id="testDemo" class="com.fs.demo.TestDemo"/>

</beans>
测试代码
    //测试引入其他的配置文件
    @Test
    public void testDemo(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //在主配置文件中获取出dao
        Demo demo = (Demo) applicationContext.getBean("demo");
        //Demo{testDemo=com.fs.demo.TestDemo@544a2ea6}因为在applicationContext02.xml中吧testDemo交给IOC管理了,所以demo中的set注入成功
        System.out.println(demo);
    }

控制台运行效果

在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,可以使用application.yml或application.properties文件配置Spring Cache。以下是一个在yml文件配置Spring Cache的示例: ```yaml spring: cache: type: caffeine ``` 在以上示例中,配置Spring Cache的类型为Caffeine,也可以改为其他类型,如Ehcache,Redis等。例如,配置Ehcache作为缓存类型的示例: ```yaml spring: cache: type: ehcache ehcache: config: classpath:ehcache.xml ``` 在以上示例中,配置Spring Cache的类型为Ehcache,并指定了Ehcache配置文件的位置为classpath:ehcache.xml。如果不想使用外部的Ehcache配置文件,也可以直接在yml文件配置Ehcache的参数,如下所示: ```yaml spring: cache: type: ehcache ehcache: config: # Ehcache的配置参数 maxEntriesLocalHeap: 1000 timeToLiveSeconds: 3600 ``` 在以上示例中,配置了Ehcache的maxEntriesLocalHeap参数为1000,timeToLiveSeconds参数为3600秒。这些参数可以根据具体的需求进行调整。 除了缓存类型的配置,还可以在yml文件配置缓存的具体实现,如Caffeine的缓存大小和过期时间等。例如: ```yaml spring: cache: caffeine: spec: maximumSize=500,expireAfterAccess=5m ``` 在以上示例中,指定了Caffeine的缓存大小为500,过期时间为5分钟。 需要注意的是,配置不同的缓存类型需要引入不同的依赖包,并且需要在代码中使用对应的注解来使用缓存。更多关于Spring Cache的配置和使用可以参考Spring官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值