引言: 最近在学 Spring5 ,但网上文档类的教程复杂难入门,但b站有不少视频教程,所以打算边学边写,做个记录
推荐及资料来源尚硅谷Spring5框架教程(idea版)
1.Spring5介绍1、Spring 是轻量级的开源的 JavaEE 框架
2、Spring 可以解决企业应用开发的复杂性
3、Spring 有两个核心部分:IOC 和 Aop
(1)IOC:控制反转,把创建对象过程交给 Spring 进行管理
(2)Aop:面向切面,不修改源代码进行功能增强
4、Spring 特点
(1)方便解耦,简化开发
(2)Aop 编程支持
(3)方便程序测试
(4)方便和其他框架进行整合
(5)方便进行事务操作
(6)降低 API 开发难度
Spring5 下载
3.Spring5 入门案例初探Spring5功能,方便解耦,xml,便于类管理
<1>.创建Students类package com.company.test;
public class Students {
private String name;
private String age;
public void setName(String name) {
this.name = name;
}
public void setAge(String age) {
this.age = age;
}
public void showStudents(){
System.out.println(this.name+" :: "+this.age);
}
}
<2>.创建xml文件
<3>.创建测试类,并测试
package com.company.testdemo;
import com.company.test.Students;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestStudents {
@Test
public void testShow() {
//1 加载 spring 配置文件
ApplicationContext context =
new ClassPathXmlApplicationContext("beanTest.xml");
//2 获取配置创建的对象
Students students = context.getBean("students", Students.class);
students.setName("张三");
students.setAge("19");
students.showStudents();
}
}
4.属性注入
<1>.有参数构造注入属性
[1].修改Students类
package com.company.test;
public class Students {
private String name;
private String age;
public Students(String name, String age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(String age) {
this.age = age;
}
public void showStudents(){
System.out.println(this.name+" :: "+this.age);
}
}
[2].修改xml文件
[3].修改测试类,并测试
package com.company.testdemo;
import com.company.test.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestUser {
@Test
public void testShowUser() {
//1 加载 spring 配置文件
ApplicationContext context =
new ClassPathXmlApplicationContext("beanTest.xml");
//2 获取配置创建的对象
User user = context.getBean("user", User.class);
// user.setUserName("张三");
// user.setAge("19");
user.showUser();
}
}
<2>.set 方法注入属性
[1].修改xml文件
```java5.类对象注入 <1>.外部注入 [1].创建Teachers类
package com.company.test;
public class Teachers {
private String name;
private String subjects;
public Teachers(String name, String subjects) {
this.name = name;
this.subjects = subjects;
}
public void showStudents(){
System.out.println(this.name+" :: "+this.subjects);
}
}
[2].修改Students类
package com.company.test;
public class Students {
private String name;
private String age;
private Teachers teachers;//新增
public Students(String name, String age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(String age) {
this.age = age;
}
public void setTeachers(Teachers teachers) {
this.teachers = teachers;
}
public void showStudents(){
System.out.println(this.name+" :: "+this.age);
}
public void showTeachers(){
this.teachers.showStudents();
}
}
[3].修改xml文件
[4].修改测试类,并测试
package com.company.testdemo;
import com.company.test.Students;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestStudents {
@Test
public void testShow() {
//1 加载 spring 配置文件
ApplicationContext context =
new ClassPathXmlApplicationContext("beanTest.xml");
//2 获取配置创建的对象
Students students = context.getBean("students", Students.class);
students.showTeachers();
}
}
<2>.内部注入
[1].修改xml文件



