SPRINGMYBATIS01 Unit01: Spring简介 、 Spring容器 、 Spring IOC

1. Spring是什么?

开源的,用来简化企业级应用开发的应用开发框架。

(1)简化开发

Spring对常用的api(比如jdbc)   做了封装,这样,可以大大简化这些api的使用。(比如使用springjdbc访问数据库,就不用考虑如何获取连接和关闭连接了)。

(2)解耦

Spring帮我们建立对象之间的依赖关系,这样,对象之间的耦合度会大大降低,代码的维护性会大大提高。

(3)集成其它框架

Spring可以将其它的一些框架集成进来(比如用于定时任务处理的Quartz等),方便这些框架的使用。

2.Spring容器

(1)什么是Spring容器?

Spring框架中的一个核心模块,用于管理对象。

(2)启动Spring容器

step1.导包。

spring-webmvc

step2. 添加spring配置文件。

step3. 启动容器。

这里写图片描述

(3)创建对象

1)方式一  无参构造器

    step1. 为类添加无参构造器(或者缺省构造器)

    step2. 在配置文件里面,添加bean元素。

        注:bean就是容器所创建的对象。

    step3. 启动容器,调用容器的getBean方法。

这里写图片描述

2)方式二  静态工厂方法 (了解)

这里写图片描述

3) 方式三  实例工厂方法(了解)  

这里写图片描述

(4)作用域

1)默认情况下,容器对于每个bean元素,只会创建一个实例。

2) 如果将作用域设置为"prototype",则每调用一次getBean方法,就会创建一个新的实例。

这里写图片描述

(5)延迟加载 (了解)

1)默认情况下,容器启动之后,会将作用域为"singleton"的bean创建好。

2)延迟加载指的是,容器启动之后,对作用域为"singleton"的bean不再创建,直到调用了getBean方法才会创建。

(6)生命周期

1)初始化方法

 容器创建好bean的实例之后,会立即调用初始化方法。

2)销毁方法

容器关闭之前,会调用销毁方法。

这里写图片描述


代码演示

这里写图片描述

/spring01/pom.xml
<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>welkin</groupId>
  <artifactId>spring01</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>
/spring01/src/main/java/basic
MessageService.java
package basic;

public class MessageService {

    public MessageService() {
        System.out.println("MessageService()");
    }
    public void init(){
        System.out.println("init()");
    }
    public void destroy(){
        System.out.println("destory()");
    }
}
Teacher.java
package basic;

public class Teacher {

    public Teacher() {
        System.out.println("Teacher()");
    }

}
/spring01/src/main/java/first
FirstSpring.java
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) {
        String config = "applicationContext.xml";
        /*
         * ApplicationContext是接口
         * ClassPathXmlApplicationContext是一个实现类,该类会依据类路径去查找spring配置文件,然后启动容器
         */
        ApplicationContext ac = new ClassPathXmlApplicationContext(config);
        /*
        *System.out.println(ac);
        *Student.class 方法区中的class对象。就可以不用强转。
        */
        Student stu = ac.getBean("stu1" , Student.class);
        System.out.println(stu);

        Date date = ac.getBean("date1",Date.class);
        System.out.println(date);

        Calendar cal = ac.getBean("cal1",Calendar.class);
        System.out.println(cal);

        Date time = ac.getBean("time1",Date.class);
        System.out.println(time);
    }

}
Student.java
package first;

public class Student {
    public Student(){
        System.out.println("Student()");
    }
}

/spring01/src/main/resources

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">
    <!-- 使用无参构造器创建对象 -->
    <!-- 
        id:bean的名字,要求唯一。
        class: 完整的类名(包括包名)
     -->
    <bean id="stu1" class="first.Student"/>

    <bean id="date1" class="java.util.Date"/>

    <!-- 使用静态工厂方法创建对象 -->
    <!-- 
        factory-method:指定一个静态方法。
        容器会调用该类的静态方法来创建一个对象。
     -->
    <bean id="cal1" class="java.util.Calendar" factory-method="getInstance"/>

    <!-- 使用实例工厂方法创建对象 -->
    <!-- 
        factory-bean:指定一个bean的id,容器
        会调用该bean的实例方法来创建一个对象。

     -->
    <bean id="time1" factory-bean="cal1"  factory-method="getTime"/>

</beans>
basic.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">
    <!-- 指定作用域 -->
    <!-- 
        scope:用来指定作用域,缺省值是
        singleton(单例,即只会创建一个对象),如果值是prototype(每调用一次getBean方法,就会创建一个新的对象)。
        lazy-init:如果值为true,表示延迟加载。
     -->
    <bean id="t1"  class="basic.Teacher"  scope="singleton" lazy-init="true"/>
    <!-- 指定初始化方法和销毁方法 -->
    <!-- 
        init-method:指定初始化方法名。
        destroy-method:指定销毁方法名。
        注意:销毁方法只对作用域为singleton的bean有效。
     -->
    <bean id ="ms1" class="basic.MessageService" init-method="init" destroy-method="destroy"/>
</beans>

/spring01/src/test/java/test/TestCase.java

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.MessageService;

public class TestCase {

    @Test
    // 测试 作用域 和 延迟加载
    public void test1() {
        String config = "basic.xml";
        // 启动spring容器
        ApplicationContext ac = new ClassPathXmlApplicationContext(config);
        // 通过容器获得bean
        // Teacher t1 = ac.getBean("t1",Teacher.class);
        // Teacher t2 = ac.getBean("t1",Teacher.class);
        // System.out.println(t1 == t2);
    }

    @Test
    // 测试 生命周期相关的两个方法
    public void test2() {
        String config = "basic.xml";
        // 启动spring容器
        /*
         * ApplicationContext接口当中并没有 提供关闭容器的方法(close方法), 需要用其子接口
         * AbstractApplicationContext
         */
        AbstractApplicationContext ac = new ClassPathXmlApplicationContext(config);
        MessageService ms1 = ac.getBean("ms1", MessageService.class);
        // 关闭容器
        ac.close();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值