• [Spring] 一篇文章了解Spring是什么


    关于Spring入门的基础知识点

    Spring简介

      Spring是由Rod Johnson创建的轻量型容器,目的在于简化企业级开发。是一种容器框架

      a.降低侵入性

      b.提供了IOC(控制反转)和AOP(面向切面)的功能,可以降低组件之间的耦合度,便于系统的维护和升级

      c. 为系统提供一个整体的解决方案,开发者除了能够使用其本身的功能以外,还可以将Spring框架和其他框架整合可以自由选择采用暗中技术来架构项目

      总结:Spring框架的实质是管理软件中的对象,可以对软件中对象的创建以及对象间的依赖关系进行管理

    优点

      ◆J2EE应该更加容易使用。
      ◆面向对象的设计比任何实现技术(比如J2EE)都重要。
      ◆面向接口编程,而不是针对类编程。Spring将使用接口的复杂度降低到零。(面向接口编程有哪些复杂度?)
      ◆代码应该易于测试。Spring框架会帮助你,使代码的测试更加简单。
      ◆JavaBean提供了应用程序配置的最好方法。
      ◆在Java中,已检查异常(Checked exception)被过度使用。框架不应该迫使你捕获不能恢复的异常。
     

    Spring中的模块

      Spring框架由七个定义明确的模块组成

      如果作为一个整体,这些模块为你提供了开发企业应用所需的一切。但你不必将应用完全基于Spring框架。你可以自由地挑选适合你的应用的模块而忽略其余的模块。

      所有的Spring模块都是在核心容器之上构建的。容器定义了Bean是如何创建、配置和管理的。当你配置你的应用时,你会潜在地使用这些类。但是作为一名开发者,你最可能对影响容器所提供的服务的其它模块感兴趣。这些模块将会为你提供用于构建应用服务的框架,例如AOP和持久性。

    核心容器(Core)
      这是Spring框架最基础的部分,它提供了依赖注入(DependencyInjection)特征来实现容器对Bean的管理。这里最基本的概念是BeanFactory,它是任何Spring应用的核心。
      BeanFactory是工厂模式的一个实现,它使用IoC将应用配置和依赖说明从实际的应用代码中分离出来。
    应用上下文(Context)模块
      核心模块的BeanFactory使Spring成为一个容器,而上下文模块使它成为一个框架。这个模块扩展了BeanFactory的概念,增加了对国际化(I18N)消息、事件传播以及验证的支持。
      另外,这个模块提供了许多企业服务,例如电子邮件、JNDI访问、EJB集成、远程以及时序调度(scheduling)服务。也包括了对模版框架例如Velocity和FreeMarker集成的支持
     
    Spring的AOP模块
      Spring在它的AOP模块中提供了对面向切面编程的丰富支持。这个模块是在Spring应用中实现切面编程的基础。为了确保Spring与其它AOP框架的互用性,Spring的AOP支持基于AOP联盟定义的API。
      Spring的AOP模块也将元数据编程引入了Spring。使用Spring的元数据支持,你可以为你的源代码增加注释,指示Spring在何处以及如何应用切面函数。
     
    JDBC抽象和DAO模块
      使用JDBC经常导致大量的重复代码,取得连接、创建语句、处理结果集,然后关闭连接。Spring的JDBC和DAO模块抽取了这些重复代码,因此你可以保持你的数据库访问代码干净简洁,并且可以防止因关闭数据库资源失败而引起的问题。
      这个模块还在几种数据库服务器给出的错误消息之上建立了一个有意义的异常层。使你不用再试图破译神秘的私有的SQL错误消息!
    另外,这个模块还使用了Spring的AOP模块为Spring应用中的对象提供了事务管理服务。
     
    对象/关系映射集成模块
      对那些更喜欢使用对象/关系映射工具而不是直接使用JDBC的人,Spring提供了ORM模块。Spring并不试图实现它自己的ORM解决方案,而是为几种流行的ORM框架提供了集成方案,包括Hibernate、JDO和iBATIS SQL映射。Spring的事务管理支持这些ORM框架中的每一个也包括JDBC。
     
    Spring的Web模块
      Web上下文模块建立于应用上下文模块之上,提供了一个适合于Web应用的上下文。另外,这个模块还提供了一些面向服务支持。例如:实现文件上传的multipart请求,它也提供了Spring和其它Web框架的集成,比如Struts、WebWork。
     
    Spring的MVC框架
      Spring为构建Web应用提供了一个功能全面的MVC框架。虽然Spring可以很容易地与其它MVC框架集成,例如Struts,但Spring的MVC框架使用IoC对控制逻辑和业务对象提供了完全的分离。
      此外,Spring的MVC框架还可以利用Spring的任何其它服务,例如国际化信息与验证。
     

    更详细的可以参考百度百科:https://baike.baidu.com/item/spring%E6%A1%86%E6%9E%B6/2853288

    A.Spring中的容器

    在Spring中,所有的Java类都当成JavaBean来处理,这些Bean通过容器进行管理和使用。

    这些类要满足下面几个规范:

      1,是公共的类,并且含有公有的无参构造法方法

      2,实例变量私有化提供 get / set 方法

    Spirng中的容器中常用的两个管理对象的类是 BeanFactory和ApplicationContext两个,区别是

      ApplicationContext继承了BeanFactory,因此含有更多的企业级的方法。并且两者都是接口。

      使用中 BeanFactory 比较高效一些

    B.Spring容器的实例化

      1,导入相关 jar 包(先放在 src / lib 下)

      2,在项目中加入Spirng配置文件 applicationContext.xml 

      3,使用BeanFactory或ApplicationContext 创建容器,只需要创建一个就足够了

      配置文件: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">
    
    </beans>

    Demo01.java

            //相对路径
            ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
            //绝对路径
            ApplicationContext ac2=new FileSystemXmlApplicationContext("src"+File.separator+"applicationContext.xml");
            System.out.println(ac2);
         // 关闭容器
            ((AbstractApplicationContext) ac).close();
  • 相关阅读:
    02-17 位图验证码(一般处理程序)+AJAX
    02-18 报表
    SQLite 函数大全
    SQLite中的时间日期函数(转)
    DES,AeS加解密,MD5,SHA加密
    suspendlayout
    AES--高级数据加密标准
    C#中Validating和Validated事件
    Net操作Excel(终极方法NPOI)
    decimal,float和double的区别
  • 原文地址:https://www.cnblogs.com/x-you/p/8400705.html
Copyright © 2020-2023  润新知