Spring_Xml配置
# 1、导入Spring包坐标
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
</dependencies>
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 2、创建Spring配置文件
路径:src/main/resources/applicationContext.xml
Demo
|__src
|__main
| |_java
| |_resources
| |__applicationContext.xml
|__test
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
内容:
<?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">
</beans>
1
2
3
4
5
6
2
3
4
5
6
# 3、一个简单的类和对应配置
类:
package com.study;
public class Student {
public void study() {
System.out.println("always study...");
}
}
1
2
3
4
5
6
7
2
3
4
5
6
7
在Spring配置文件中配置:
<?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">
<bean id="student" class="com.study.Student"/>
</beans>
1
2
3
4
5
6
7
2
3
4
5
6
7
# 4、依赖注入,Bean实例化
# 4.1 ClassPathXmlApplicationContext
路径参数:
对于 ClassPathXmlApplicationContext 的使用:
- 默认使用:项目的 classpath 路径;
classpath:
前缀是可加可不加的 。 - 如果要使用绝对路径 , 需要加上
file:
, 前缀表示这是绝对路径。 - 如果是两个以上 , 可以使用字符串数组 ;或者使用通配符(正则表达)
代码:
package com.study;
import javafx.application.Application;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main1 {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//ApplicationContext applicationContext = new ClassPathXmlApplicationContext("file:E:/applicationContext.xml");
//ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","SpringTest.xml"});
//ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:/*.xml");
Student student = (Student) applicationContext.getBean("student");
student.study();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 4.2 FileSystemXmlApplicationContext
路径参数:
- 默认使用:项目的根路径,没有盘符的是项目工作路径
- 有盘符表示的是文件绝对路径 ,
file:
可加可不加。 - 如果要使用 classpath 路径 , 需要前缀
classpath:
。 - 如果是两个以上 , 可以使用字符串数组 ;或者使用通配符(正则表达)
代码:
package com.study;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Main2 {
public static void main(String[] args) {
ApplicationContext applicationContext = new FileSystemXmlApplicationContext("src/main/resources/applicationContext.xml");
//ApplicationContext applicationContext = new FileSystemXmlApplicationContext("classpath:applicationContext.xml");
//ApplicationContext applicationContext = new FileSystemXmlApplicationContext(new String[]{"classpath:applicationContext.xml","classpath:SpringTest.xml"});
//ApplicationContext applicationContext = new FileSystemXmlApplicationContext("classpath:/*.xml");
Student student = (Student) applicationContext.getBean("student");
student.study();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 5、Git
仓库位置:https://github.com/su-dd/demo.git (opens new window)
代码位置:Spring相关/2Demo (opens new window)
Demo路径:https://github.com/su-dd/KnowledgeStack.git
具体路径:JavaWeb/TestCode/_03_Spring/Demo1
编辑 (opens new window)
上次更新: 2023/03/17, 15:46:47