1,框架是什么?
框架(Framework),是基于建筑概念,适用于理解软件等技术领域各种框架、架构概念。是用于承载一个系统必要功能的基础要素的集合。
2,常用的框架
- 重量级框架ssh(spring+struct2+hibernate)
- 轻量级框架ssm(spring+springmvc+mybatis)
ps:博主主要学习ssm框架。
3,Spring框架是什么?
spring框架里包含spring framework ,spring boot ,spring cloud,……
4,Spring框架中包含的内容
5,Spring框架的核心内容
5.1,ioc
5.1.1,概念
ioc: Inversion of Control 控制反转
以前在学习Java时侯通过new创建对象,而现在通过spring创建
5.1.2,j2se阶段创建对象
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框架创建对象
①添加依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
②添加配置
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--第一种:构造器创建-->
<bean id="exampleBean" class="examples.ExampleBean"/>
<bean name="anotherExample" class="examples.ExampleBeanTwo"/>
<!--第二种:工厂模式创建-->
<bean id="clientService"
class="examples.ClientService"
factory-method="createInstance"/>
</beans>
<!--第三种:单例模式创建-->
<bean id="serviceLocator" class="examples.DefaultServiceLocator"></bean>
<bean id="clientService" factory-bean="serviceLocator" factory-method="createClientServiceInstance"/>
以student类为例举例:
<bean id="student01" class="com.op.ssm.ioc.Student01"></bean>
<bean id="student02_01" class="com.op.ssm.ioc.Student02" factory-method="getInstance"></bean>
<bean id="student02_02" class="com.op.ssm.ioc.Student02" factory-method="getInstance"></bean>
<bean id="student03Factory" class="com.op.ssm.ioc.Student03Factory"></bean>
<bean id="student03" factory-bean="student03Factory" factory-method="createStudent03"></bean>
③实现
@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 依赖注入
初始化成员变量,给成员变量赋值。
5.2.2,j2se阶段成员变量赋值方法
//第一种:通过构造函数
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的成员变量赋值方法
①添加配置
<!--第一种:构造注入
index 变量位置,从0开始
value将要赋的值
-->
<bean id="student01" class="com.openlab.ssm.di.Student01">
<constructor-arg index="0" value="1"></constructor-arg>
<constructor-arg index="1" value="www"></constructor-arg>
<constructor-arg index="2" value="18"></constructor-arg>
</bean>
<!--第二种:set注入
name写的是set方法去掉字母set后,第一个字母小写
-->
<bean id="student02" class="com.openlab.ssm.di.Student02">
<property name="id" value="2"></property>
<property name="name" value="wj"></property>
<property name="age" value="20"></property>
</bean>
②测试
@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);
}
第一种结果测试
第二种结果测试