Spring是什么
- Spring是分层的JavaSE/EE应用 full-stack轻量级开源框架
Spring体系结构
- 底层是核心容器
- Beans
- Core
- Context
- SpringEL表达式
- 中间层技术
- AOP
- Aspects
- 应用层技术
- 数据访问与数据集成
- Web集成
- Web实现
- 基于Test测试
IOC
- IOC(Inversion Of Control)控制反转,Spring反向控制应用程序所需要使用的外部资源
IOC入门案例
-
导入maven坐标
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.9.RELEASE</version> </dependency>
-
编写Spring配置文件(将需要的对象存储到spring容器)
<?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">
<!-- 1.创建spring控制的资源-->
<bean id="userService" class="com.project.service.impl.UserServiceImpl"/>
</beans>
- 创建容器对象并使用容器中的资源
public class Test{
public static void main(String[] args) {
//2.加载配置文件
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//3.通过id获取资源,也可以通过类型获取
UserService userService = (UserService) ctx.getBean("userService");
userService.save();
}
}
IoC配置(XML格式)
- bean标签
- 作用:定义spring中的资源,使用此标签定义的资源受spring控制
<beans>
<!--id:bean的名称,name:bean的别名,class:指定当前需要创建对象的权限定类名,
scope:bean的作用范围,默认是singleton,单列容器中当前实列对象永远只有一个
init-method:在构造方法执行完成后执行初始化方法
destroy-method:在容器正常关闭的时候执行此方法-->
<bean id="beanId" name="beanName1,beanName2" class="ClassName" scope="singleton"/>
</beans>
DI
- DI(Dependency Injection)依赖注入,应用程序运行依赖的资源由Spring为其提供,资源进入应用程序的方式称为注入
- IoC与DI的关系
IoC与DI是同一件事站在不同角度看待问题
依赖注入
- set注入
- 名称:propertry
- 作用:使用set方式为bean注入资源
<!--
name:对应bean的属性名,该属性必须提供可访问的set方法
value:设定非引用类型的数据,不能喝ref通用
ref:设定应用类型属性对应的id
一个bean可以用多个propertry
-->
<property name="propertyName" value="propertyValue" ref="beanId"/>
- 构造器注入
- 名称:constructor-arg
- 作用:使用构造方法为其注入资源
<!--
ref:设定引用类型构造方法参数对应bean的id ,不能与value同时使用
type :设定构造方法参数的类型,用于按类型匹配参数或进行类型校验
index :设定构造方法参数的位置,用于按位置匹配参数,参数index值从0开始计数
-->
<constructor-arg index="arg-index" type="arg-type" ref="beanId"/>
还有其他注入...
- SpEL
<property value="EL"></bean>
<!--
作用:为bean注入属性值
注意:所有属性值不区分数据类型,统一使用value赋值
常量 #{10} #{3.14} #{2e5} #{‘itcast’}
引用bean #{beanId}
引用bean属性 #{beanId.propertyName}
引用bean方法 beanId.methodName().method2()
引用静态方法 T(java.lang.Math).PI
运算符支持 #{3 lt 4 == 4 ge 3}
正则表达式支持 #{user.name matches‘[a-z]{6,}’}
集合支持 #{likes[3]}
-->
properties文件
- 准备外部properties文件
- 开启context命名空间支持
xmlns:context="http://www.springframework.org/schema/context"
- 加载指定的properties文件
<context:property-placeholder location="classpath:filename.properties">
- 使用加载的数据
<property name="propertyName" value="${propertiesName}"/>
如果需要加载所有的properties文件,可以使用*.properties表示加载所有的properties文件,读取数据使用${propertiesName}格式进行,其中propertiesName指properties文件中的属性名
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.93.129:3306/spring_db
jdbc.username=root
jdbc.password=admin
import
- 作用:在当前配置文件中导入其他配置文件中的项
<beans>
<import resource=“config.xml"/>
<!--resource:加载的配置文件名-->
</beans>
第三方资源配置
- 阿里数据源方案Druid
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>