目录
1 创建maven工程
在IDEA创建Maven工程
在pom.xml文件中添加Spring核心容器的jar包和Spring依赖jar包
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>chapter06</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</project>
2 创建Bean
Student.java
代码如下:
package com.qfedu.bean;
public class Student {
public Student() {
super();
System.out.println("Student对象创建了");
}
// 创建一个成员变量,给出get/set方法
private String msg = null;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
// 在study()方法中调用成员变量
public void study(){
System.out.println("学生在学习"+msg);
}
}
Teacher.java
代码如下:
package com.qfedu.bean;
public class Teacher {
public Teacher() {
super();
System.out.println("Teacher对象创建了");
}
private String msg = null;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public void teach(){
System.out.println("老师讲解"+msg+"知识");
}
}
3 创建配置文件
配置文件applicationContext.xml需要放置在resources文件夹下,在src根目录下不起做作用
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 将指定的类配置给Spring -->
<bean id="student" class="com.qfedu.bean.Student">
<property name="msg">
<value>Java</value>
</property>
</bean>
<bean id="teacher" class="com.qfedu.bean.Teacher" lazy-init="true">
<property name="msg">
<value>Spring</value>
</property>
</bean>
</beans>
4 编写测试类
TestSpring.java
代码如下:
package com.qfedu.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.qfedu.bean.Student;
import com.qfedu.bean.Teacher;
public class TestSpring {
public static void main(String[] args) throws Exception {
//通过读取配置信息获取ApplicationContext对象
ApplicationContext applicationContext = new
ClassPathXmlApplicationContext("applicationContext.xml");
//通过id值获取Student对象
Student student = applicationContext.getBean("student",Student.class);
//调用Student对象的方法
student.study();
//通过id值获取Teacher对象
Teacher teacher = applicationContext.getBean("teacher",Teacher.class);
//调用Teacher对象的方法
teacher.teach();
}
}
运行结果如下图: