2023-01-13
一 、Spring
1 、Spring简介
(1)Spring是一个为简化企业级开发而生的开源框架 。
(2)Spring是一个IOC(DI)和AOP容器框架 。
IOC:Inversion of Contriol(控制反转 ,即将对象的控制权交给Spring)
AOP:Aspect-Oriented Programming ,面向切面编程
DI:Dependency Injection(依赖注入)
(3)官方文档:
2 、搭建Spring框架步骤
(1)导入jar包
jar包可以在maven中查找
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.10</version>
</dependency>
(2)编写核心配置文件
将配置文件命名为“applicationContext.xml(或者beans.xml或者spring.xml ”
配置文件的路径在“src/main/resources ”
(3)使用核心类库
3 、创建Spring框架的步骤
(1)创建一个maven工程 ,命名为“day05_spring ”
在“day05_spring/pom.xml ”中添加jar包
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
(2)在“day05_spring/src/main/java/com.hh.spring.pojo ”下创建“Student ”类
public class Student {
private Integer stuId;
private String stuName;
public Student() {
}
public Student(Integer stuId, String stuName) {
this.stuId = stuId;
this.stuName = stuName;
}
public Integer getStuId() {
return stuId;
}
public void setStuId(Integer stuId) {
this.stuId = stuId;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
@Override
public String toString() {
return "Student{" +
"stuId=" + stuId +
", stuName=" + stuName + \ +
};
}
}
(3)在“day05_spring/src/main/resources ”中创建“applicationContext.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">
<!-- 将对象装配到IOC容器中-->
<bean id="stuZhangsan" class="com.hh.spring.pojo.Student">
<property name="stuId" value="101"></property>
<property name="stuName" value="zhangsan"></property>
</bean>
</beans>
(4)在“day05_spring/src/test/java ”中设置测试类
public class TestSpring {
@Test
public void testSpring(){
//创建容器对象
ApplicationContext iocObj = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过容器对象 ,获取需要对象
Student stuZhangsan = (Student)iocObj.getBean("stuZhangsan");
System.out.println("stuZhangsan = " + stuZhangsan);
}
}
4 、Spring的特性
(1)非侵入式:基于Spring开发的应用中的对象可以不依赖于Spring的API 。
(2)容器:Spring是一个容器 ,因为它包含并且管理应用对象的生命周期 。
(3)组件化:Spring实现了使用简单的组件配置组合成一个复杂的应用 ,在Spring中可以使用XML和java注解组合这些对象 。
(4)一站式:在IOC和AOP的基础上可以整合各种企业应用的开源框架和优秀的第三方类库(实际上Spring自身也提供了表述层的SpringMVC和持久层的JDBCTemplate) 。
5 、Spring中getBean三种方式
(1)方式一
getBean(String beanId):通过beanId获取对象
不足:需要强制类型转换 ,不灵活
(2)方式二
getBean(Class clazz):通过Class方式获取对象
不足:容器中有多个相同类型bean的时候 ,会报错“expected single matching bean but found 2 ”
(3)方式三
getBean(String beanId,Clazz clazz):通过beanId和Class获取对象
常用
测试类的关键代码
Student stuZhangsan = iocObj.getBean("stuZhangsan", Student.class);
System.out.println("stuZhangsan = " + stuZhangsan);
6 、Spring中的标签
(1)属性
①id:bean的唯一标识
②class:定义bean的类型(指定class全类名)
(2)子标签
①property:为对象中属性赋值(set注入)
name属性:设置属性名称
value属性:设置属性数值
声明:本站所有文章 ,如无特殊说明或标注 ,均为本站原创发布 。任何个人或组织 ,在未征得本站同意时 ,禁止复制 、盗用 、采集 、发布本站内容到任何网站 、书籍等各类媒体平台 。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理 。