11-23 程宗武
11-23 Spring01
1 Spring
1.1 Spring理念
使现有的技术更加容易使用,整合了现有的技术框架
1.2 下载地址
官方: https://repo.spring.io/release/org/springframework/spring/
github: https://github.com/spring-projects/spring-framework/releases
Maven仓库地址:
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
1.3 Spring的优点
1.开源的免费框架
2.轻量级的、非侵入式的框架(不会对其他资源产生影响)
3.控制反转(IOC),面向切面编程(AOP)
3.支持事务的处理,对框架整合的支持
总结: Spring就是一个轻量级的控制反转(IOC)和面向切面编程(AOP)的框架
1.4 使用Spring的IOC解耦
1.4.1 配置maven环境
<properties>
<spring.version>5.0.2.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
1.4.2 创建业务的接口和实现类
1.4.3 XML配Bean
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">
<!-- bean 标签:用于配置让 spring 创建对象,并且存入 ioc 容器之中
id 属性:对象的唯一标识。
class 属性:指定要创建对象的全限定类名-->
<bean id="IUserMapper" class="top.wutiaojian.spring.mapper.impl.IUserMapperImpl"/>
<bean id="IUserService" class="top.wutiaojian.spring.service.impl.IUserServiceImpl"/>
</beans>
<!--bean标签中的各个属性的作用
scope:指定对象的作用范围
singleton :默认值,单例的.
prototype :多例的.
request :WEB 项目中,Spring 创建一个 Bean 的对象,将对象存入到 request 域中.
session :WEB 项目中,Spring 创建一个 Bean 的对象,将对象存入到 session 域中.
global session :WEB 项目中,应用在 Portlet 环境(Portlet是基于Java的Web组件).如果没
有 Portlet 环境那么globalSession 相当于 session.
init-method:指定类中的初始化方法名称。
destroy-method:指定类中销毁方法名称。
-->
1.4.4 测试配置是否成功
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
IUserMapperImpl userMapper = (IUserMapperImpl) context.getBean("IUserMapper");
System.out.println(userMapper);//对象的地址
IUserServiceImpl userService1 = context.getBean(IUserServiceImpl.class);
IUserServiceImpl userService = context.getBean("IUserService", IUserServiceImpl.class);
System.out.println(userService==userService1);//true,因为默认使用的是单例模式创建的对象,因此两次创建的对象为同一个
}
获取Spring容器中BeanFactory和ApplicationContext的区别
创建对象的时间节点不同
ApplicationContext:只要一读取配置文件就会创建对象
BeanFactory:获取对应的Bean时才创建
ApplicationContext的三个实现类:
ClassPathXmlApplicationContext:从类的根目录下加载配置文件
FileSystemXmlApplicationContext:从磁盘上读取
AnnotationConfigApplicationContext:基于注解
1.4.5实例化Bean的三种方式
1.使用无参构造
<!--默认调用的时Man对象的无参构造,当Man中没有无参构造时会报错-->
<bean id="man" class="top.wutiaojian.spring.po.Man"></bean>
2.创建一个静态工厂来管理对象
public class BeanStaticFactory {
public static Person createInstance() {
return new Person();
}
}
//xml中的配置
<bean id="staticFactory" class="top.wutiaojian.spring.factory.BeanStaticFactory" factory-method="createInstance"</bean>
3.使用实例工厂来管理对象
<bean id="instanceFactory" class="top.wutiaojian.spring.factory.BeanInstanceFactory"></bean>
<bean id="instance" factory-bean="instanceFactory" factory-method="createInstance" lazy-init="false"></bean>
1.4.6懒加载
懒加载(Lazy)是指在启动Spring容器时不实例化bean对象,而是在需要对象时实例化bean对象。
设置方式: lazy-init="true"
<bean id="IUserMapper" class="top.wutiaojian.spring.mapper.impl.IUserMapperImpl" lazy-init="true"/>
在没有配置懒加载之前启动Spring容器时就会创建对象
配置过后只有在需要时才会调用并创建对象
2.Spring依赖注入
2.1 依赖注入概念
例如我们由原来的自己传入一个持久层的实现类对象,变成了由Spring帮我们注入
2.2 注入方式
2.2.1通过构造函数注入
//业务层
@Data
public class IUserServiceImpl implements IUserService {
private String name;
private Integer age;
private Date birthday;
private IUserMapperImpl iUserMapper;
public IUserServiceImpl(String name, Integer age, Date birthday) {
this.name = name;
this.age = age;
this.birthday = birthday;
}
public int selectUser() {
System.out.println(name + age + birthday);
return 0;
}
}
//xml
<bean id="IUserMapper" class="top.wutiaojian.spring.mapper.impl.IUserMapperImpl" lazy-init="true"/>
<bean id="IUserService" class="top.wutiaojian.spring.service.impl.IUserServiceImpl" lazy-init="true">
<property name="IUserMapper" ref="IUserMapper"></property>
<constructor-arg name="name" value="马保国"/>
<constructor-arg name="age" value="56"/>
<constructor-arg name="birthday" ref="date"/>
</bean>
<bean id="date" class="java.util.Date"></bean>
<!--标签说明-->
constructor-arg:用来给构造函数设置值
index: 参数在构造函数中的位置
type: 参数在构造函数中的数据类型
name: 参数在构造函数中的变量名
value:给指定的参数赋值,类型为基本数据类型和引用数据类型
ref: 其他Bean类型,参数类对应Bean类型的id
2.2.2 Set方法注入
<bean id="IUserService" class="top.wutiaojian.spring.service.impl.IUserServiceImpl" lazy-init="true">
<property name="IUserMapper" ref="IUserMapper"></property>
<property name="name" value="马保国"></property>
<property name="age" value="99"></property>
<property name="birthday" ref="date"></property>
</bean>
<bean id="date" class="java.util.Date"></bean>
2.2.3 P标签方式注入
<bean id="IUserService" class="top.wutiaojian.spring.service.impl.IUserServiceImpl" lazy-init="true"
p:name="太一" p:age="10" p:birthday-ref="date" p:IUserMapper-ref="IUserMapper">
</bean>
2.2.3 注入集合
private List<String> list;
private Map<String, Object> map;
<bean id="IUserService" class="top.wutiaojian.spring.service.impl.IUserServiceImpl" lazy-init="true">
<property name="list">
<list>
<value>1</value>
<value>2</value>
</list>
</property>
<property name="map">
<map>
<entry key="八神太一" value="亚古兽"></entry>
<entry key="阿和" value-ref=""></entry><!--这里的值填对应的id-->
</map>
</property>
</bean>
近期评论