Unit01: Spring简介 、 Spring容器 、 Spring IOC
Unit01: Spring简介 、 Spring容器 、 Spring IOC
Spring
(1)Spring是什么?
Spring是一个开源的用来简化应用开发的框架。
(2)Spring的特点?
简化开发
Spring对很多常用的api做了简化(比如,Spring对jdbc就做了很好的 封装,我们利用Springjdbc访问数据库,就不用考虑如何获取连接和关闭 连接)。
解耦
Spring帮我们管理对象之间的依赖关系,这样,便于代码的维护。
集成
Spring可以集成其它的一些框架。集成之后,这些框架的使用会变得 更加的简单。
(3)Spring容器
Spring容器是什么?
Spring框架当中的一个核心模块,用来管理对象。
如何启动Spring容器?
step1. 导包。
step2. 添加配置文件。
step3. 编写启动Spring容器的代码。
如何创建对象?
方式一 无参构造器
step1. 类应用提供无参构造器(或者缺省构造器)。
step2. 在配置文件当中,添加一个bean元素。
step3. 启动Spring容器,调用容器提供的getBean方法获得要创建的
对象。
方式二 静态工厂方法 (了解)
方式三 实例工厂方法 (了解)
生命周期的管理
初始化方法 :
(用于获取资源,只执行一次)。
销毁方法:
(用于释放资源,只执行一次)。
注意,销毁方法只有在作用域为singleton才起作用。
作用域
默认情况下,对于一个bean元素,容器只会创建一个对应的实例。
可以设置作用域为prototype(原型,多个实例)。
延迟加载 (了解)
默认情况下,当容器启动之后,会将所有作用域为单例的bean创建好。
lazy-init属性:如果值为true,表示延迟加载,即容器启动之后,不会立即创建该实例。
(4)IOC (Inversion Of Controll 控制反转)
什么是IOC?
对象之间的依赖关系由容器来建立。
什么是DI? (Dependency Injection 依赖注入)
容器通过调用set方法或者构造器来建立对象之间的依赖关系。
注: IOC是目标,而DI是手段。
依赖注入的两种方式
方式一 set方法。
step1. 添加set方法。
step2. 配置set方法注入。
代码演示:
依赖的Jar包:
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.tarena.spring</groupId>
- <artifactId>spring-day01</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>war</packaging>
- <dependencies>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>3.2.8.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.12</version>
- </dependency>
- </dependencies>
- </project>
pom.xml
src/main/java/
basic
- package basic;
- public class MessageBean {
- public MessageBean() {
- System.out.println("MessageBean()");
- }
- public void init(){
- System.out.println("init()");
- }
- public void sendMsg(){
- System.out.println("sendMsg()");
- }
- public void destroy(){
- System.out.println("destroy()");
- }
- }
MessageBean.java
- package basic;
- public class ScopeBean {
- public ScopeBean() {
- System.out.println("ScopeBean()");
- }
- }
ScopeBean.java
first
- package first;
- import java.util.Calendar;
- import java.util.Date;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class FirstSpring {
- public static void main(String[] args) {
- //step1. 启动Spring容器
- /*
- * ClassPathXmlApplicationContext
- * 会依据类路径查找配置文件。
- */
- ApplicationContext ac =
- new ClassPathXmlApplicationContext(
- "applicationContext.xml");
- //System.out.println(ac);
- //step2. 通过容器,获得对象。
- Student stu1 =
- ac.getBean("stu1",Student.class);
- System.out.println(stu1);
- Date date1 =
- ac.getBean("date1",Date.class);
- System.out.println(date1);
- Calendar cal1 =
- ac.getBean("cal1",Calendar.class);
- System.out.println("cal1:" + cal1);
- Date time1 =
- ac.getBean("time1",Date.class);
- System.out.println("time1:" + time1);
- }
- }
FirstSpring.java
- package first;
- public class Student {
- public Student() {
- System.out.println("Student()");
- }
- }
Student.java
ioc
- package ioc;
- public class A {
- private B b;
- public void setB(B b) {
- System.out.println("A's setB()");
- this.b = b;
- }
- public A() {
- System.out.println("A()");
- }
- public void service(){
- System.out.println("A's service()");
- //调用B的f1()方法
- b.f1();
- }
- }
A.java
- package ioc;
- public class B {
- public B() {
- System.out.println("B()");
- }
- public void f1(){
- System.out.println("B's f1()");
- }
- }
B.java
src/main/resource
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:jdbc="http://www.springframework.org/schema/jdbc"
- xmlns:jee="http://www.springframework.org/schema/jee"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xmlns:util="http://www.springframework.org/schema/util"
- xmlns:jpa="http://www.springframework.org/schema/data/jpa"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
- http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
- http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
- http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
- http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
- <!--
- 利用无参构造器创建对象。
- id属性:bean的名称,要求唯一。
- class属性:类的全限定名。
- -->
- <bean id="stu1" class="first.Student">
- </bean>
- <bean id="date1" class="java.util.Date">
- </bean>
- <!-- 使用静态工厂方法创建对象。
- factory-method属性:指定一个静态方法,
- Spring容器会调用这个方法来创建对象。
- -->
- <bean id="cal1" class="java.util.Calendar"
- factory-method="getInstance">
- </bean>
- <!-- 使用实例工厂方法创建对象
- factory-bean属性:指定一个bean的id ,
- Spring容器会调用该对象对应的方法
- 来创建对象。
- -->
- <bean id="time1" factory-bean="cal1"
- factory-method="getTime">
- </bean>
- </beans>
applicationContext.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"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:jdbc="http://www.springframework.org/schema/jdbc"
- xmlns:jee="http://www.springframework.org/schema/jee"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xmlns:util="http://www.springframework.org/schema/util"
- xmlns:jpa="http://www.springframework.org/schema/data/jpa"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
- http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
- http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
- http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
- http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
- <!--
- init-method属性:指定初始化方法。
- destroy-method属性:指定销毁方法。
- lazy-init属性:如果值为true,表示延迟加载,
- 即容器启动之后,不会立即创建该实例。
- -->
- <bean id="mb1" class="basic.MessageBean"
- init-method="init"
- destroy-method="destroy"
- lazy-init="true">
- </bean>
- <!--
- scope属性:指定作用域,缺省值是
- singleton(单例,即单个实例);
- 可以设置作用域为prototype(原型,多个实例)。
- -->
- <bean id="sb1" class="basic.ScopeBean"
- scope="prototype">
- </bean>
- </beans>
basic.java
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:jdbc="http://www.springframework.org/schema/jdbc"
- xmlns:jee="http://www.springframework.org/schema/jee"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xmlns:util="http://www.springframework.org/schema/util"
- xmlns:jpa="http://www.springframework.org/schema/data/jpa"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
- http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
- http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
- http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
- http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
- <bean id="b1" class="ioc.B"/>
- <!-- set方法注入。
- 容器会调用setB方法来建立依赖关系。
- -->
- <bean id="a1" class="ioc.A">
- <property name="b" ref="b1"/>
- </bean>
- </beans>
ioc.xml
src/test/java
test
- package test;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.AbstractApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import basic.MessageBean;
- import basic.ScopeBean;
- import ioc.A;
- public class TestCase {
- @Test
- //测试 初始化方法和销毁方法
- public void test1(){
- //启动spring容器
- String config = "basic.xml";
- /*
- * ApplicationContext接口没有提供
- * 关闭容器的方法。
- */
- AbstractApplicationContext ac =
- new ClassPathXmlApplicationContext(
- config);
- //通过容器获得对象
- MessageBean mb1 =
- ac.getBean("mb1",MessageBean.class);
- mb1.sendMsg();
- //关闭容器
- ac.close();
- }
- @Test
- //测试 作用域
- public void test2(){
- //启动spring容器
- String config = "basic.xml";
- ApplicationContext ac =
- new ClassPathXmlApplicationContext(
- config);
- ScopeBean sb1 =
- ac.getBean("sb1",ScopeBean.class);
- ScopeBean sb2 =
- ac.getBean("sb1",ScopeBean.class);
- System.out.println(sb1 == sb2);
- }
- @Test
- //测试 延迟加载
- public void test3(){
- //启动spring容器
- String config = "basic.xml";
- ApplicationContext ac =
- new ClassPathXmlApplicationContext(
- config);
- }
- @Test
- //测试 依赖注入
- public void test4(){
- //启动spring容器
- String config = "ioc.xml";
- ApplicationContext ac =
- new ClassPathXmlApplicationContext(
- config);
- A a1 =
- ac.getBean("a1",A.class);
- a1.service();
- }
- }
TestCase.java
下面是上面这个案例:ioc包下的方法升级版:
增加一个IB接口,B.java , C.java实现IB.java接口,实现f1()方法,能让A.java灵活调用B和C下的f1方法。
ioc
接口:
- package ioc;
- public interface IB {
- public void f1();
- }
IB.java
- package ioc;
- public class A {
- private IB b;
- public void setB(IB b) {
- System.out.println("A's setB()");
- this.b = b;
- }
- public A() {
- System.out.println("A()");
- }
- public void service(){
- System.out.println("A's service()");
- //调用B的f1()方法
- b.f1();
- }
- }
A.java
- package ioc;
- public class B implements IB{
- public B() {
- System.out.println("B()");
- }
- public void f1(){
- System.out.println("B's f1()");
- }
- }
B.java
- package ioc;
- public class C implements IB{
- public C() {
- System.out.println("C()");
- }
- public void f1(){
- System.out.println("C's f1()");
- }
- }
C.java
Unit01: Spring简介 、 Spring容器 、 Spring IOC的更多相关文章
- spring简介、容器、IOC
对IOC的理解: 在平时的java应用开发中,我们要实现某一个功能或者说是完成某个业务逻辑时至少需要两个或以上的对象来协作完成,在没有使用Spring的时候,每个对象在需要使用他的合作对象时,自己均要 ...
- [Spring学习笔记 1 ] Spring 简介,初步知识--Ioc容器详解 基本原理。
一.Spring Ioc容器详解(1) 20131105 1.一切都是Bean Bean可是一个字符串或者是数字,一般是一些业务组件. 粒度一般比较粗. 2.Bean的名称 xml配置文件中,id属性 ...
- 【Spring 1】Spring简介
一.Spring简介 首先,Spring框架是由于软件开发的复杂性而创建的.Spring使用的是基本的JavaBean来完成以前只可能由EJB完成的事情.然而,Spring的用途不仅仅限于服务器端的开 ...
- spring框架篇(一)-------spring简介与配置文件使用控制反转事例
spring简介 Spring 是一个开源框架,中文意思就是春天,也许是作者想让自己的这个框架给Java开发人员带来春天吧.其官方网站是 https://spring.io/ ,可以在官方网站下载到完 ...
- Spring简介即Spring Ioc
Spring框架简介 Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE De ...
- 【Spring】IoC容器 - Spring Bean作用域Scope(含SpringCloud中的RefreshScope )
前言 上一章学习了[依赖来源],本章主要讨论SpringBean的作用域,我们这里讨论的Bean的作用域,很大程度都是默认只讨论依赖来源为[Spring BeanDefinition]的作用域,因为在 ...
- MyEclipse Spring 学习总结一 Spring IOC容器
一.Spring IOC容器---- Spring AllicationContext容器 程序的结构如下: 1.首先在MyEclipse 创建创建Java Project 2.创建好后,添加spin ...
- 【spring源码分析】IOC容器初始化(总结)
前言:在经过前面十二篇文章的分析,对bean的加载流程大致梳理清楚了.因为内容过多,因此需要进行一个小总结. 经过前面十二篇文章的漫长分析,终于将xml配置文件中的bean,转换成我们实际所需要的真正 ...
- 【spring源码分析】IOC容器初始化(二)
前言:在[spring源码分析]IOC容器初始化(一)文末中已经提出loadBeanDefinitions(DefaultListableBeanFactory)的重要性,本文将以此为切入点继续分析. ...
随机推荐
- 【Python】什么是闭包
文章转载自:点这里 在 Python 中很多教材都没有提及什么是闭包,但在定义一个 Decorator 时,就已经用到闭包了.如果不理解什么是闭包,则不可能清晰掌握Decorator 装饰器. 要形成 ...
- Factorialize a Number
计算一个整数的阶乘 如果用字母n来代表一个整数,阶乘代表着所有小于或等于n的整数的乘积. 阶乘通常简写成 n! 例如: 5! = 1 * 2 * 3 * 4 * 5 = 120 当你完成不了挑战的时候 ...
- 028——VUE中事件修饰符once
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Spring入门2. IoC中装配Bean
Spring入门2. IoC中装配Bean 20131125 前言: 上一节学习了Spring在JavaProject中的配置,通过配置文件利用BeanFactory和ApplicationConte ...
- 转载:【Oracle 集群】RAC知识图文详细教程(三)--RAC工作原理和相关组件
文章导航 集群概念介绍(一) ORACLE集群概念和原理(二) RAC 工作原理和相关组件(三) 缓存融合技术(四) RAC 特殊问题和实战经验(五) ORACLE 11 G版本2 RAC在LINUX ...
- 【Html 学习笔记】第四节——框架
我们经常使用的网页可能不是一个单独的网页,而是多个嵌套的,那么就需要用到下面的技术. 垂直框架:<frameset cols=""> 这里需要注意的是:四个网页需要同时 ...
- python中time()时间的相关问题
Python中time模块详解(转) 在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括:time,datetime以及calendar.这篇文章,主要讲解time ...
- 如何自定义FileZilla编辑文件的默认打开方式
看一下设置: 原来是人家默默认不给我们提示 改一下: 还是不行,还是跳到网页去了 还是不行 这样就好了...
- Systems
package com.System; public class Study01 { /* * System 包含一些游泳的类字段和方法 * 继承自java.lang包 * JDK1.0开始 * 全部 ...
- Composer 扩展包安装方法
问题说明 我们经常要往现有的项目中添加扩展包,有时候因为文档的错误引导,如下图来自 这个文档 的: composer update 这个命令在我们现在的逻辑中,可能会对项目造成巨大伤害. 因为 com ...