框架(Framework),是基于建筑概念,适用于理解软件等技术领域各种框架、架构概念。是用于承载一个系统必要功能的基础要素的集合。
2,常用的框架- 重量级框架ssh(spring+struct2+hibernate)
- 轻量级框架ssm(spring+springmvc+mybatis)
ps:博主主要学习ssm框架。
spring框架里包含spring framework ,spring boot ,spring cloud,……
4,Spring框架中包含的内容 5,Spring框架的核心内容 5.1,ioc 5.1.1,概念ioc: Inversion of Control 控制反转
以前在学习Java时侯通过new创建对象,而现在通过spring创建
public class Student01 {
private int id;
private String name;
private int age;
public Student01() {
}
public Student01(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
以Student01类为例
//第一种创建方式:通过new调用构造函数
Student01 st01 = new Student01();
//第二种:单例模式
private static Student01 instance = new Student01();
private Student01(){}
public static Student01 getInstance() {
return instance
}
//第三种:对象方法
public class Student01Factory {
public Student01 createStudent03(){
return new Student01();
}
}
Student01Factory student01Factory = new StudentFactory01();
Student01 student01 = student01Factory.createStudent01();
5.1.3,spring框架创建对象
①添加依赖
org.springframework spring-context 5.2.6.RELEASE
②添加配置
以student类为例举例:
③实现
@Test
public void testMethod01(){
// 加载spring框架配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationcontext-01.xml");
// 获取创建好的对象
Student01 student01 = ac.getBean("student01", Student01.class);
System.out.println(student01);
}
@Test
public void testMethod02(){
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationcontext-01.xml");
Student02 student02_01 = ac.getBean("student02_01", Student02.class);
Student02 student02_02 = ac.getBean("student02_02", Student02.class);
System.out.println(student02_01==student02_02);
}
@Test
public void testMethod03(){
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationcontext-01.xml");
Student03 student03 = ac.getBean("student03", Student03.class);
System.out.println(student03);
}
5.2,DI
5.2.1,概念
DI: Dependency injection 依赖注入
初始化成员变量,给成员变量赋值。
//第一种:通过构造函数
Student01 student01 = new Student01(1, "wjl", 1);
//第二种:通过set
Student01 student01 = new Student01();
student01.setId(2);
student01.setAge(18);
student01.setName("www");
5.2.3,spring的成员变量赋值方法
①添加配置
②测试
@Test
public void testMethod01() {
//加载配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:application-02.xml");
//获取对象
Student01 student01 = ac.getBean("student01", Student01.class);
//查看
System.out.println(student01);
}
@Test
public void testMethod02() {
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:application-02.xml");
//获取对象
Student02 student02 = ac.getBean("student02", Student02.class);
//查看
System.out.println(student02);
}
第一种结果测试
第二种结果测试



