• Spring学习1:Spring基本特性


    http://longliqiang88.github.io/2015/08/14/Spring%E5%AD%A6%E4%B9%A01%EF%BC%9ASpring%E5%9F%BA%E6%9C%AC%E7%89%B9%E6%80%A7/

    Spring学习1:Spring基本特性

    Spring基本特征

    Spring基本特征Spring基本特征
    Spring是一个非常活跃的开源框架;它是一个基于Core来构架多层JavaEE系统的框架,它的主要目地是简化企业开发。
    Spring以一种非侵入式的方式来管理你的代码,Spring提倡”最少侵入”,这也就意味着你可以适当的时候安装或卸载Spring。

    开发spring所需要的工具

    Spring的jar包

    http://www.springsource.org/download下载spring,然后进行解压缩,在解压目录中找到下面jar文件,拷贝到类路径下
    —spring的核心类库 在spring文档的dist下
    distspring.jar
    —引入的第三方类库 都spring文档的lib下
    libjakarta-commonscommons-logging.jar
    如果使用了切面编程(AOP),还需要下列jar文件
    lib/aspectj/aspectjweaver.jar和aspectjrt.jar
    lib/cglib/cglib-nodep-2.1_3.jar
    如果使用了JSR-250中的注解,如@Resource/@PostConstruct/@PreDestroy,还需要下列jar文件
    libj2eecommon-annotations.jar
    注:JSR(Java 规范请求)是指向JCP(Java Community Process)提出新增一个标准化技术规范的正式请求。任何人都可以提交JSR(Java 规范请求),以向Java平台增添新的API和服务。JSR已成为Java界的一个重要标准

    Spring配置文件

    默认情况下是applicationContext.xml文件。可以建立很多xml文件,工程中一般都是这样配置的。

    Spring IOC

    IOC指的是控制反转,把对象的创建、初始化、销毁等工作都交给Spring容器。由spring容器来控制对象的生命周期。下图可以说明我们传统创建类的方式和使用Spring之后的区别:
    Spring IOCSpring IOC
    代码实现如下:
    首先创建Helloworld类:

    1
    2
    3
    4
    5
    public class HelloWorld {
    public void hello(){
    System.out.println("hello world");
    }
    }

    Spring配置文件:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <?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
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    <!--
    表示一个类
    id类的唯一标示
    class 代表类的全名,也就是包名+类名
    -->
    <bean id="helloWorld" class="cn.zju.spring.HelloWorld"></bean>
    </beans>

    main方法的代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public static void main(String[] args){
    /**
    * 1、启动spring容器
    * 2、从spring容器中把该对象提取出来
    * 3、对象调用方法
    */
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld");
    helloWorld.hello();
    }
    }

    Spring容易创建对象的意义:程序员可以更加专注于业务的开发,而不用管对象的创建、销毁等,把这些操作都交给Spring容器完成。

    Spring创建对象的方式

    无参构造方法

    如果在配置文件中只使用id和class属性配置bean,则默认调用类的无参构造方法进行实例化,代码如下所示:

    1
    <bean id="helloWorld" class="cn.zju.spring.HelloWorld"></bean>

    上述配置将调用HelloWorld类的无参构造方法来创建HelloWorld实例。

    静态工厂

    有些类中提供了静态的工厂方法返回实例,假设HelloWorldFactory类中有如下的静态工厂方法:

    1
    2
    3
    4
    5
    public class HelloWorldFactory {
    public static HelloWorld getInstance(){
    return new HelloWorld();
    }
    }

    在配置文件中使用factory-method属性调用改静态工厂方法,创建HelloWorld实例,代码如下:

    1
    2
    <bean id="helloWorldFactory" class="cn.zju.spring.HelloWorldFactory"></bean>
    <bean id="helloWorld" class="cn.zju.spring.HelloWorldFactory" factory-method="getInstance"></bean>

    实例工厂方法

    1. 提供工厂类
    2. 把工厂放到Spring容器中
    3. 在Spring容器中指定工厂类的工厂方法
    4. 在客户端中创建对象
      1
      2
      <bean id="helloWorldFactory" class="cn.zju.spring.HelloWorldFactory"></bean>
      <bean id="helloWorld" class="cn.zju.spring.HelloWorldFactory" factory-bean="helloWorldFactory" factory-method="getInstance"></bean>

    Spring对象初始化bean时机

    在默认情况下,只要在Spring容器中配置了一个bean,容器在启动时就会实例化该bean,单例模式。
    如果在Spring配制文件时设置懒加载模式(lazy-init=”true”),在getBean时才会实例化对象。
    如果scope=”prototype”时,无论lazy-init的值是什么都只会在使用时才会创建,当struts2的action和spring容器整合的时候,action的scope设置成prototype。

    Spring容器的生命周期

    Spring容器生命周期Spring容器生命周期

      1. 启动spring容器
      2. 创建helloWorld对象
      3. 调用helloWorld对象的init方法,init方法是由spring容器内部调用的
      4. 在客户端提取helloWorld对象,对象调用方法
      5. 当spring容器关闭的时候,执行destroy方法:注意:前提条件:必须为单例,如果多实例,不起作用。
  • 相关阅读:
    文献阅读 | On the subspecific origin of the laboratory mouse
    文献阅读 | GenomicsDB: storing genome data as sparse columnar arrays.
    文献阅读 | The human Y chromosome: an evolutionary marker comes of age
    文献阅读 | HaploGrouper: A generalized approach to haplogroup classification
    文献阅读 | Systematic evaluation of error rates and causes in short samples in next-generation sequencing
    文献阅读 | The Wheat 660K SNP array demonstrates great potential for marker‐assisted selection in polyploid wheat
    使用HMM进行分类识别(以语音识别为例)
    文献阅读 | Fine definition of the pedigree haplotypes of closely related rice cultivars by means of genome-wide discovery of single-nucleotide polymorphisms
    GWAS学习笔记(一) | 质量控制(QC)
    python实现简单决策树(信息增益)——基于周志华的西瓜书数据
  • 原文地址:https://www.cnblogs.com/zquan/p/9992525.html
Copyright © 2020-2023  润新知