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();
}
}
总结:
? ? ? 假如存在多个配置文件就可以用加载总配置文件方法,如果用加载多个配置文件方法的话,文件名容易出错,假如文件名有误,代码当时也不会报错,只会在运行后报错,所以用加载总配置文件方法会避免这样的情况出现。
|