spring学习笔记(2)—— 配置文件
创建java文件
导入jar包
File - > Project Structure
Modules
加号 java
导入jar包
ok
测试
创建一个spring config的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">
<bean id = "student" class="com.test.code.Student"/>
</beans>
创建一个学生类
代码
package com.test.code;
public class Student {
private String name;
private String id;
public void studentDoing(){
System.out.println("这是个学生类");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
创建一个测试类
package com.test.code;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student)applicationContext.getBean("student");
student.studentDoing();
}
}
运行main