IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> Spring IOC配置文件加载 -> 正文阅读

[Java知识库]Spring IOC配置文件加载

Spring配置文件加载

? ? ? ? 1、通过相对路径加载配置文件:
? ? ? ? ? ? ? ? new ClassPathXmlApplicationContext("配置文件名")

? ? ? ? ? ? ? ? 例如:

????????????????

BeanFactory factory = new ClassPathXmlApplicationContext("spring.xml");

? ? ? ? 2、通过绝对路径加载配置文件:

? ? ? ? ? ? ? ? new FileSystemXmlApplicationContext("配置文件的绝对路径")

? ? ? ? ? ? ? ? 右击其配置文件,单击Copy Path即可出现其所在的绝对路径。

? ? ? ? ? ? ? ? 例如:

????????????????

BeanFactory factory = new FileSystemXmlApplicationContext("C:\\Users\\mtx\\IdeaProjects\\spring03\\src\\main\\resources\\spring.xml");

? ? ? ? (1)单个配置文件的加载:

? ? ? ? ? ? ? ? new ClassPathXmlApplicationContext("配置文件名")

? ? ? ? (2)多个配置文件加载:

? ? ? ? ? ? ? ? 1、通过可变参数,设置多个配置文件:

? ? ? ? ? ? ? ? ? ? ? ??new ClassPathXmlApplicationContext("配置文件名1","配置文件名2"...)

? ? ? ? ? ? ? ? 2、设置一个总配置文件,在总配置文件中导入需要加载的配置文件:

? ? ? ? ? ? ? ? ? ? ? ? <import resource="配置文件名"/>(单双标签都可)

pom.xml:

????????

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>spring03</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>spring03</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!-- 添加Spring框架的核心依赖 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.2.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>5.2.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.7.RELEASE</version>
    </dependency>

  </dependencies>

  <build>
  </build>
</project>

spring.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/tool"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool.xsd">
    <!-- 实例化bean对象 -->
    <bean id="userService" class="org.example.UserService"></bean>
</beans>

UserService:

????????

package org.example;

public class UserService {
    public void test(){
        System.out.println("UserService...");
    }
}

Start:
????????

package org.example;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
 * spring配置文件加载
 * 1、通过相对路径加载配置文件
 *      new ClassPathXmlApplicationContext("配置文件名")
 * 2、通过绝对路径加载配置文件
 *      new FileSystemXmlApplicationContext("配置文件的绝对路径)
 *
 * 1、单个配置文件加载
 *      new ClassPathXmlApplicationContext("配置文件名")
 * 2、多个配置文件加载
 *      (1)通过可变参数,设置多个配置文件
 *          new ClassPathXmlApplicationContext("配置文件名1","配置文件2"...)
 *      (2)设置一个总配置文件,在总配置文件中导入需要加载的配置文件:
 *          <import resource="配置文件名"></import>(单双标签都可)
 */
public class Start {
    public static void main(String[] args) {
        //通过相对路径加载配置文件
//       BeanFactory factory = new ClassPathXmlApplicationContext("spring.xml");

        //通过绝对路径加载配置文件
//      BeanFactory factory = new FileSystemXmlApplicationContext("C:\\Users\\mtx\\IdeaProjects\\spring03\\src\\main\\resources\\spring.xml");

        //加载多个配置文件
       BeanFactory factory = new ClassPathXmlApplicationContext("spring.xml","beans.xml");
       UserService userService = (UserService) factory.getBean("userService");
       userService.test();

       userDao userDao = (userDao) factory.getBean("userDao");
       userDao.test();
    }
}

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

    <bean id="userDao" class="org.example.userDao">
        <!-- collaborators and configuration for this bean go here -->
    </bean>

</beans>

userDao:

????????

package org.example;

public class userDao {
    public void test(){
        System.out.println("userDao...");
    }
}

加载总配置文件

? ? ? ? 只加载一个service.xml,其中的多个配置文件即可使用。

????????service.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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--  导入配置文件  -->
    <import resource="beans.xml"></import>
    <import resource="spring.xml"></import>
</beans>

????????到时候就用这一段代码就可以调用多个配置文件:

????????

 BeanFactory factory = new ClassPathXmlApplicationContext("service.xml");

????????

package org.example;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;


public class Start {
    public static void main(String[] args) {
       
        //加载总配置文件
        BeanFactory factory = new ClassPathXmlApplicationContext("service.xml");
        UserService userService = (UserService) factory.getBean("userService");
        userService.test();

        userDao userDao = (userDao) factory.getBean("userDao");
        userDao.test();
    }
}

总结:

? ? ? 假如存在多个配置文件就可以用加载总配置文件方法,如果用加载多个配置文件方法的话,文件名容易出错,假如文件名有误,代码当时也不会报错,只会在运行后报错,所以用加载总配置文件方法会避免这样的情况出现。

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2021-08-13 11:49:15  更:2021-08-13 11:53:14 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/18 4:18:58-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码