1.准备工作
开发spring至少需要6个jar
- spring-aop.jar
- spring-beans.jar
- spring-context.jar
- spring-expression.jar
- commons-logging.jar
下载地址为:
https://repo.spring.io/release/org/springframework/spring/
选择4.3.9 -dist文件 下载并解压
在解压后的文件夹->lib>中找到上述5个jar
在idea中添加这5个jar包
2.创建一个javaproject
1.png
2.1 student类
package calvin.entity;
public class student {
private int stuNO;
private String stuName;
private int stuAge;
public int getStuNO() {
return stuNO;
}
public void setStuNO(int stuNO) {
this.stuNO = stuNO;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
//重载了object类中的to string 方法用于输出类内的信息
@Override
public String toString(){
return this.stuNO+","+this.stuName+","+this.stuAge;
}
}
2.2test类
package calvin.test;
import calvin.entity.student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
public static void main(String[] args) {
/* student stu=new student();
stu.setStuName("lch");
stu.setStuNO(1);
stu.setStuAge(22);
System.out.println(stu.tostring()); */
//spring上下文对象
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
student stu=(student)context.getBean("student");
System.out.println(stu);
}
}
2.3spring配置文件
<?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="calvin.entity.student">
<property name="stuNO" value="1"></property>
<property name="stuName" value="ls"></property>
<property name="stuAge" value="2"></property>
</bean>
</beans>
springioc容器 帮我们new了对象 并且给对象赋了值。