Spring学习
- Spring入门
- spring简介
- Spring简单示例程序HelloWorld
- 程序的搭建:
- IOC容器 & DI概述
- bean的基本配置
- Spring容器
- 获取bean
- 依赖注入方法
- 字面值
- 引用其他bean (ref)
Spring入门
spring简介
Spring是一个开源框架,Spring为简化企业级应用开发而生。 使用Spring可以使简单的 JavaBean实现以前只有EJB才能实现的功能。 Spring是一个IOC(DlI)和 AOP容器框架;
特点:
- 轻量级: Spring是非侵入性的–基于Spring开发的应用中的对象可以 不依赖于Spring 的API.
- 依赖注入(DI — dependency injection、Ioc)
- 面向切面编程(AOP — aspect oriented programming)
- 容器: Spring是一个容器,因为它包含并且管理应用对象的生命周期.
- 框架: Spring 实现了使用简单的组件配置组合成一个复杂的应用.在Spring中可以使用XML和Java 注解组合这些对象
- 一站式: 在IOC和 AOP的基础上可以整合各种企业应用的开源框架和优秀的第三方类库(实际上 Spring自身也提供了展现层的SpringMvc和持久层的Spring JDBC) .
Spring简单示例程序HelloWorld
程序的搭建:
- 引入Spring依赖包;
Maven库
选择5.3,18版本
配置文件:
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.18</version>
</dependency>
- 配置文件
配置依赖包:
在Maven项目的下的pom.xml中引入Spring的依赖
- 编写程序使用spring框架
构建一个HelloSpring实体类 包含Name.Addr
平常写法,不适用Spring的情况:
创建springconfig文件:
在resource下配置一个spring-1.xml配置文件,之后通过该配置文件创建我们需要的容器
package edu.etime.springdemo.demo1;
public class Test {
public static void main( String[] args) {
/*一般方法使用HeLLoSpring对象
1、new创建对象实例
2、调用get , set方法获取值或者赋值*/
HelloSpring helloSpring = new HelloSpring();
helloSpring.setName("张三");
helloSpring.setAddr("温江");
system.out.println(helloSpring.getName());
System.out.print1n(helloSpring.getAddr());
}
/*
使用spring容器:
1、创建spring容器
2、从容器中获取指定的bean
*/
//创建容器
ApplicationContext ctx = new ClassPathXm1ApplicationContext("spring-1.xm1");
//从容器中获取bean
HelloSpring hs = (HelloSpring) ctx.getBean("hellospring");
system.out.println(hs.getName()+"\t"+hs.getAddr());
}
IOC容器 & DI概述
IOC(Inversion of Control):其思想是 反转资源获取的方向 .传统的资源查找方式要求组件向容器发起请求查找资源. 作为回应,容器适时的返回资源.而应用了IOC之后,则是 容器主动地将资源推送给它所管理的组件,组件所要做的仅是选择一种合适的方式来接受资源. 这种行为也被称为查找的被动形式
DI(Dependency Injection)—IOC的另一种表述方式:即组件以一些预先定义好的方式(例如: setter方法)接受来自如容器的资源注入.相对于IOC而言,这种表述更直接
IOC 与 DI 两者区别
IOC控制反转
DI依赖注入
bean的基本配置
<bean id="hellospring" class="edu.etime.spring.HelloSpringTest">
<property name="name" value="Spring配置名"/>
<property name="addr" value="四川成都!"/>
</bean>
-
id 属性 :bean 的名称 ,在IOC容器中,bena的名称是唯一的.
hellospring
-
如果没有指定id属性,那么将会用class属性作为bean的名称
edu.etime.spring.HelloSpringTest
-
Class属性,类的全名称(包名 + 类名)
edu.etime.spring.HelloSpringTest
Spring容器
在Spring lOC容器读取 Bean配置创建Bean实例之前,必须对它进行实例化.只有在容器实例化后,才可以从lOC 容器里获取 Bean实例并使用.( 要获取 bean就必须要先实例化IOC容器,然后从容器中获取bean ) .
Spring 提供两种类型的IOC容器实现:
-
BeanFactory:IOC容器的基本实现;
-
ApplicationContext: 提供了更多的高级特性,是BeanFactory的子接口; (主要使用的实现容器方式)
ApplicationContext提供的实现类:
ClassPathXmlApplicationContext: 从类路径下加载配置文件。FileSystemXmIApplicationContext: 从文件系统中加载配置文件。
ConfigurableApplicationContext扩展于ApplicationContext,新增加两个主要方法: refresh() 和 close(), 让 ApplicationContext具有启动、刷新和关闭上下文的能力.
ApplicationContext在初始化上下文时就实例化所有单例的 Bean。(默认spirng._容器中的bean都是单例对象)。
WebApplicationContext是专门为wEB应用而准备的,它允许从相对于wEB根目录的路径中完成初始化工作.
ApplicationContext 与 ConfigurableApplicationContext的区别:
ConfigurableApplicationContext是ApplicationContext的子接口,增加了刷新和关闭方法,让IOC容器具备刷新和关闭功能.
获取bean
两种方式获取Bean
- 通过Bean的id获取
- 通过Bean的Class类型获取(相同类型bean在ioc容器中不能存在多个)
//根据bean的id获取bean
HelloSpring hs = (HelloSpring) ctx.getBean("edu.etime.springdemo.demo1.HelloSpring")
//根据类型获取bean
//注意:相同类型的bean在IOC容器中不能存在多个
Hellospring hs1 = ctx.getBean(HelloSpring.class);
依赖注入方法
三种注入方式:
-
通过属性注入(通过setter方法注入)
-
通过构造方法注入
顺序不同的情况:
①通过index指定
②通过类型
③通过参数名字
- 通过工厂方法注入(静态工厂方法注入,实例工厂方法注入)
工厂方法注入一般来来说并不会去使用,大家可以自行搜索了解
工厂方法注入讲解示例
字面值
如果我们的值带有特殊字符:
那我们可以使用<![CDATA[<成都>]]>
这样的方式注入
引用其他bean (ref)
-
引入其他bean ref引用
-
内部bean