Spring的概述:
(容器框架,可以管理所有的组件)
- 是一个开源软件(半成品软件;高度抽取可重用代码的一种设计=高通用性)
- 为简化企业级开发的
- 是一个IOL(DI)和AOP的容器框架
- spring的优良特性:
非侵入式:不依赖于spring的api
依赖注入(DI)
面向切面编程(AOP)
容器:包含管理应用对象的生命周期
组件化:抽取所有的代码抽出成一个组件
一站式:一个框架用到底
Spring模块:
Spring=IOL(容器)+AOP(面向切面编程) :
IOL:strats+Hibermte+myBtis
AOP:声明事务<-Spring-jdbcTemplate(玩具版)
IOC[控制反转]:主动获取变为被动接受:
资源获取的方式只有主动、被动两种方式
BookServelt{
//主动:要什么创什么,简单
BooKServelt bs=new BooKServelt ();
//被动:资源的获取不是我们自己创建,而是交给一个容器来创建和设置
BooKServelt cs;
cs.checkout();
}
容器:管理所有的组件(有功能的类);主动的new变为被动的接受资源
DI:依赖注入;容器能知道那个组件(类)运行的时候,需要另外一个组件(类);容器通过反射的形式,将容器中准备好的BookServrlt对象注入(;利用反射给属性赋值)到BookSelvelt中
只要容器管理的组件,都能使用容器提供的强大功能
通过各种方法给容器注册对象,以前是自己new对象,现在所有的对象交给容器创建,给
容器中注册组件.
框架编写流程:
1. 导包:
创建一个文件夹,把架包复制进去,然后选中五个架包并右键选择Bulid Path导包:
(核心容器)
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
commons-logging-1.1.1.jar//日志包,必须有
2. 写配置:
Spring的配置文件中,集合了Spring的IOC容器管理的所有组件;
创建一个Spring Bean ConfigurationFile(Spring的bean配置的文件);
package com.ssm.bean;
public class Persion {
private String lastname;
private Integer age;
private String gender;
private String email;
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Persion [lastname=" + lastname + ", age=" + age + ", gender=" + gender + ", email=" + email + "]";
}
}
<?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">
<!-- 注册一个Persion对象,Spring会自动创建这个Persion对象 -->
<!--
一个Bean标签可以注册一个组件(对象、类)
class,写要注册的组件的全类名
id,这个对象的唯一标识
-->
<bean id="one" class="com.ssm.bean.Persion">
<!-- 使用property标签为Persion对象的属性赋值 -->
<property name="lastname" value="张三"></property>
<property name="age" value="18"></property>
<property name="email" value="com.pengzehong"></property>
<property name="gender" value="男"></property>
</bean>
</beans>
3. 测试:
package com.ssm.text;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ssm.bean.Persion;
class IOCtext {
/**
* 从容器中拿到这个组件
*/
@Test
void test() {
//ApplicationContext代表IOC容器
//当前应用的xml配置文件在 ClassPath下
ApplicationContext ioc=new ClassPathXmlApplicationContext("ioc.xml");
Persion bean=(Persion)ioc.getBean("one");
System.out.println(bean);
}
}
细节问题:
1): ApplicationContext(IOC容器的接口)
new ClassPathXmlApplicationContext("ioc.xml"):ioc容器的配置文件在类路径下面
new FileSystemXmlApplicationContext("D://ioc.xml"):ioc容器的配置文件在磁盘路径下
2):给容器中注册一个组件,并从其容器中根据id拿到了这个组件的对象
组建的创建工作是Spring容器完成的
3):同一个组件在ioc容器中是单实例的,容器启动完成时创建准备好的
4):容器中如果没有这个组件,获取会报:
org.springframework.beans.factory.NoSuchBeanDefinitionException:
No bean named 'tow' is defined
5):ioc容器在创建这个组件对象的时候,(property)会利用set方法为javabean的属性进行赋值
6):JavaBean的属性名由get/set方法决定的,set后面那一串去掉首字母小写就是属性名,所有的get/set都是自动生成的
常见知识:
1)src:源码包开始的路径,成为类路径的开始,
所有源码包里面的东西都会合并放在类路径里面。
2)导包:commons不能忘(这是一个依赖)
3)先导包再创建配置文件
4)Spring的容器接管了标志小s