目录
- [1 关于单例多例说明](#1 关于单例多例说明)
-
- [1.1 关于Spring容器对象创建说明](#1.1 关于Spring容器对象创建说明)
- [1.2 多例对象配置](#1.2 多例对象配置)
- [1.3 编辑测试方法](#1.3 编辑测试方法)
- [2 懒加载](#2 懒加载)
- [3 Spring生命周期](#3 Spring生命周期)
-
- [3.1 生命周期的过程:](#3.1 生命周期的过程:)
- [3.2 编辑文件](#3.2 编辑文件)
- [3.3 编辑测试案例](#3.3 编辑测试案例)
- [3.4 关于IOC小结](#3.4 关于IOC小结)
- [4 依赖注入(DI)](#4 依赖注入(DI))
-
-
- [4.1.1 依赖注入介绍](#4.1.1 依赖注入介绍)
- [4.1.2 构建POJO属性](#4.1.2 构建POJO属性)
- [4.1.3 编辑xml配置文件](#4.1.3 编辑xml配置文件)
- [4.4.4 测试案例](#4.4.4 测试案例)
- [4.4.5 关于DI注入说明](#4.4.5 关于DI注入说明)
- [4.2 属性注入高级用法](#4.2 属性注入高级用法)
-
- [4.2.1 编辑POJO属性](#4.2.1 编辑POJO属性)
- [4.2.2 编辑xml配置文件](#4.2.2 编辑xml配置文件)
- [4.3 抽取集合类型](#4.3 抽取集合类型)
-
- [4.3.1 编辑头标签](#4.3.1 编辑头标签)
- [4.3.2 实现对象引用](#4.3.2 实现对象引用)
-
- [5 Spring容器管理3层代码结构](#5 Spring容器管理3层代码结构)
-
- [5.1 代码结构](#5.1 代码结构)
-
- [5.1.1 定义User POJO对象](#5.1.1 定义User POJO对象)
- [5.1.2 定义dao接口/实现类](#5.1.2 定义dao接口/实现类)
- [5.1.3 定义Service接口/实现类](#5.1.3 定义Service接口/实现类)
- [5.1.4 编辑UserController](#5.1.4 编辑UserController)
- [5.1.5 3层代码结构关系](#5.1.5 3层代码结构关系)
- [5.1.6 编辑xml配置文件](#5.1.6 编辑xml配置文件)
- 5.1.7编辑测试类
- [5.2 关于特殊字符说明](#5.2 关于特殊字符说明)
1 关于单例多例说明
1.1 关于Spring容器对象创建说明
- Spring容器中默认的对象都是单例对象(通过构造方法实例化对象)
- 有时需要通过多例对象为用户提供服务(数据源链接)
1.2 多例对象配置
- 创建com.jt.pojo包
java
public class User {
public User() {
System.out.println("创建对象");
}
public void say(){
System.out.println("我是用户");
}
}
- 修改配置文件
xml
<!--4.测试单例多例 通过scope属性控制对象的单例和多例
scope="prototype" 多例设置
scope="singleton" 缺省值 单例
-->
<bean id="user" class="com.jt.pojo.User" scope="prototype"></bean>
1.3 编辑测试方法
java
@Test
public void testUser(){
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
context.getBean("user");
context.getBean("user");
context.getBean("user");
}
如果是单例模式,那构造方法只会执行一次
如果是多例模式。那构造方法会执行多次
2 懒加载
- 懒加载:
- 当用户需要获取对象时,容器才创建对象,称之为懒加载
- 说明:
- Spring容器中默认的规则是:容器创建则对象创建.
- 如果需要配置懒加载 则需要添加额外的属性
xml
<!--4.测试单例多例 通过scope属性控制对象的单例和多例
scope="prototype" 多例设置
scope="singleton" 缺省值 单例
lazy-init="true" 开启懒加载
lazy-init="false"/lazy-init="default" 懒加载不生效
原则: 只要是多例对象 都是懒加载. 懒加载只对单例对象有效
关于懒加载说明: 一般服务器对象应该先行创建,用户直接使用即可.
多例对象: 用户使用时创建,同时将对象的生命周期交给使用者管理,
Spring不负责维护对象的生命周期
(随用随销)
-->
<bean id="user" class="com.jt.pojo.User" scope="prototype" lazy-init="true"></bean>
3 Spring生命周期
3.1 生命周期的过程:
1.实例化对象
2.初始化操作 (一般对对象的属性赋值)
3.用户使用对象(调用其中的方法)
4.对象销毁 (一般都是释放资源)
3.2 编辑文件
java
public class User {
private String conn;//数据库链接
//1.实例化对象
public User() {
System.out.println("创建对象");
}
//2.初始化方法
public void init() {
this.conn = "赋值数据源链接";
System.out.println(this.conn);
}
//3.用户调用的方法
public void say() {
System.out.println("我是用户");
}
//4.销毁方法
public void destory() {
this.conn = null;
System.out.println("释放资源" + this.conn + "~~~~");
}
}
xml
<!--
测试对象的生命周期
init-method="init" 初始化方法
destroy-method="destroy" 销毁方法
-->
<bean id="user" class="com.jt.pojo.User" init-method="init" destroy-method="destroy"/>
3.3 编辑测试案例
这时ApplicationContext里面没有close方法,我们要使用其子类ClassPathXmlApplicationContext
java
//测试生命周期运行
@Test
public void testlife(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
//获取对象
User user = context.getBean(User.class);
//3.用户调用方法
user.say();
//4.只要容器关闭,则对象销毁
context.close();
}

3.4 关于IOC小结
核心: 对象的创建
- 通过bean标签进行创建 属性 id/class
- 工厂模式
- 单例多例/懒加载
- 生命周期
4 依赖注入(DI)
4.1.1 依赖注入介绍
当某个角色(可能是一个Java实例,调用者)需要另一个角色(另一个Java实例,被调用者)的协助时,在 传统的程序设计过程中,通常由调用者来创建被调用者的实例。但在Spring里,创建被调用者的工作不再由调用者来完成,因此称为控制反转;创建被调用者 实例的工作通常由Spring容器来完成,然后注入调用者,因此也称为依赖注入。
对象中的属性 ,应该由spring容器动态赋值.
4.1.2 构建POJO属性
java
public class User {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User() {
}
public User(Integer id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
4.1.3 编辑xml配置文件
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">
<!--管理user对象-->
<bean id="user" class="com.jt.pojo.User">
<!--调用对象的set方法实现赋值 set方法必须添加-->
<property name="id" value="101"></property>
<property name="name" value="李元芳"></property>
</bean>
</beans>
4.4.4 测试案例

4.4.5 关于DI注入说明
- Set注入
- 构造注入
xml
<!--构造方法进行注入-->
<bean id="user" class="com.jt.pojo.User">
<constructor-arg name="id" value="102"/>
<constructor-arg name="name" value="兰陵王"/>
</bean>
4.2 属性注入高级用法
4.2.1 编辑POJO属性

4.2.2 编辑xml配置文件
xml
<!--为集合赋值-->
<bean id="user" class="com.jt.pojo.User">
<property name="id" value="101"/>
<property name="name" value="李元芳"/>
<property name="list">
<list>
<value>张三</value>
<value>李四</value>
<value>王五</value>
</list>
</property>
<property name="set">
<set>
<value>1</value>
<value>2</value>
<value>3</value>
</set>
</property>
<property name="map">
<map>
<entry key="id" value="1000"/>
<entry key="name" value="tomcat猫"/>
</map>
</property>
<property name="pro">
<props>
<prop key="proId">110</prop>
<prop key="proName">米老鼠</prop>
</props>
</property>
</bean>
- 测试结果

4.3 抽取集合类型
4.3.1 编辑头标签

xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
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
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
</beans>
4.3.2 实现对象引用

- 测试结果

5 Spring容器管理3层代码结构
5.1 代码结构
5.1.1 定义User POJO对象
java
public class User { //通过容器为对象的属性赋值
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
5.1.2 定义dao接口/实现类

5.1.3 定义Service接口/实现类

5.1.4 编辑UserController
java
import com.jt.pojo.User;
import com.jt.service.UserService;
public class UserController {
//spring容器负责注入Service对象
private UserService userService;
private User user;//代替用户传入的数据
public void setUser(User user) {
this.user = user;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public void addUSer(){
userService.addUser(user);
}
}
5.1.5 3层代码结构关系

5.1.6 编辑xml配置文件
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">
<!--1.构建user对象-->
<bean id="user" class="com.jt.pojo.User">
<property name="id" value="100"/>
<property name="name" value="springMVC设计模式"/>
</bean>
<!--2.构建Dao对象
根据面向接口编程
Id:接口的名称
class:实现类的包路径
-->
<bean id="userDao" class="com.jt.dao.UserDaoImpl"/>
<!--3.构建Service-->
<bean id="userService" class="com.jt.service.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
<!--4.构建Controller-->
<bean id="userController" class="com.jt.controller.UserController">
<property name="userService" ref="userService"/>
<property name="user" ref="user"/>
</bean>
</beans>
5.1.7编辑测试类
java
public class TestSpring {
@Test
public void testSpringMVC(){
ApplicationContext context =
new ClassPathXmlApplicationContext("application.xml");
UserController userController = (UserController) context.getBean("userController");
userController.addUser();
System.out.println("恭喜学会MVC结构");
}
}
- 测试结果

5.2 关于特殊字符说明

由于由于业务需要xml配置文件中 可能会有特殊字符,但是该特殊字符与xml关键字(标签)形成冲突,所以我们要 实现字符串转义
- 特殊转义字符:
<
小于<>
大于>&
和号&'
单引号'"
引号"

- 万能转义字符: <![CDATA[XXX任意字符]]>
