一 Sring是开源的轻量级框架(处理对象的创建,以及对象的依赖关系)
二 Spring核心主要两部分
- AOP:面向切面编程(扩展功能不是修改源代码)
-
- IOC :控制反转
- 比如有一个类,在类里面有方法(不是静态方法),调用类里面的方法,创建类的对象,使用对象调用方法,创建类对象的过程,需要new实现。
- 把对象的创建不是通过new来实现,而是交给spring配置创建类对象。
三 Spring是一站式框架
(1). Spring在javaee三层结构中,每一层都提供不同的解决技术
- web层:SpringMVC
- service层:Sring的IOC
- dao层:Spring的jdbcTemplate
四 Spring版本
Spring4.x
五 Spring的IOC操作
1 把对象的创建交给spring进行管理
2 IOC操作两部分
(1) IOC的配置文件方式
(2) IOC的注解方式
六 IOC底层原理
1 IOC底层原理使用技术
(1) xml配置文件
(2) dom4j解决xml
(3) 工厂设计模式
(4) 反射
2 画图分析IOC实现原理
![](https://img.haomeiwen.com/i6010417/5e0425ba6171d0cc.png)
![](https://img.haomeiwen.com/i6010417/fb81b093c2f2485d.png)
七 IOC入门案例
第一步 导入jar包
- Jar特点 :每个都有三个jar包
- spring-bean-4.xx.RELEASE.jar jar包
- spring-bean-4.xx.RELEASE.jar-javadoc.jar 文档
- spring-bean-4.xx.RELEASE-sources.jar 源代码
- 主要jar包:
- Beans
- core
- context
- expression
- 日志记录jar:
- logging
- log4j
maven项目加载jar
<!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
![](https://img.haomeiwen.com/i6010417/a0f6014b05c7165d.png)
第二步 创建类,在类里面创建方法
![](https://img.haomeiwen.com/i6010417/59ce70ce70380697.png)
第三步 创建spring配置文件,配置创建类
- spring核心配置文件名称和位置不是固定的
- 建议放到src下面,官方建议applicationContext.xml
![](https://img.haomeiwen.com/i6010417/f09250d010cd08f5.png)
![](https://img.haomeiwen.com/i6010417/80be08e9b6182392.png)
- 引入scheam约束
<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="user" class="entity.User"></bean>
第四步 写代码测试对象创建
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by pc on 2017/9/7.
*/
public class TextIOC {
public static void main(String [] args){
//加载Spring配置文件,并创建对象
ApplicationContext context = new ClassPathXmlApplicationContext("Spring/applicationContext.xml");
//得到配置的对象
User user= (User) context.getBean("user");
user.add();
}
}
测试结果
![](https://img.haomeiwen.com/i6010417/dcb964c45a3ee49e.png)
网友评论