javascript
Spring (1) 认识Spring、 介绍Spring特点、解答为什么学习Spring
一、Spring介紹
簡介
The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any kind of deployment platform
Spring 框架為現代基于 java 的企業應用程序提供了一個全面的編程和配置模型——適用于任何類型的部署平臺。
特點、主要特點
1、非侵入式
2、容器
3、IoC
4、AOP
5、方便程序的測試
為什么要學?
主流市場推薦、學習的人數多、Spring社區活躍、工作需要、屬于開源框架、方便web開發、集成度高。
還因為Spring具有 以下優點:
- 輕量級:相較于EJB容器,Spring采用的IoC容器非常的輕量級,基礎版本的Spring框架大約只有2MB。Spring可以讓開發者們僅僅使用POJO(Plain Old Java Object,相對于EJB)就能夠開發出企業級的應用。這樣做的好處是,你不需要使用臃腫龐大的 EJB容器(應用服務器),你只需要輕量的servlet容器(如Tomcat)。尤其在一些開發當中,很稀缺內存和CPU資源時,采用Spring比EJB無論是開發還是部署應用都更節約資源。
- 控制反轉(IOC):Spring使用控制反轉技術實現了松耦合。依賴被注入到對象,而不是創建或尋找依賴對象。
- 面向切面編程(AOP): Spring支持面向切面編程,同時把應用的業務邏輯與系統的服務分離開來。
- MVC框架:Spring MVC是一個非常好的MVC框架,可以替換其他web框架諸如Struts。
- 集成性:Spring非常容易和其他的流行框架一起集成開發,這些框架包括:ORM框架,logging框架,JEE, Quartz,以及Struts等表現層框架。
- 事務管理:Spring強大的事務管理功能,能夠處理本地事務(一個數據庫)或是全局事務(多個數據,采用JTA)。
- 模塊分離:Spring框架是由模塊構成的。雖然已經有太多的包和類了,但它們都按照模塊分好類了,你只需要考慮你會用到的模塊,而不用理其他的模塊。
- 異常處理:由于Java的JDBC,Hibernate等API中有很多方法拋出的是checked exception,而很多開發者并不能很好的處理異常。Spring提供了統一的API將這些checked exception的異常轉換成Spring的unchecked exception。
- 單元測試:Spring寫出來的代碼非常容易做單元測試,可以采用依賴注射(Dependency Injection)將測試的數據注射到程序中。
Spring框架體系結構:
二、IOC(依賴注入)
Spring 容器IOC 和 bean介紹
This chapter covers the Spring Framework implementation of the Inversion of Control (IoC) principle. IoC is also known as dependency injection (DI). It is a process whereby objects define their dependencies (that is, the other objects they work with) only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method. The container then injects those dependencies when it creates the bean. This process is fundamentally the inverse (hence the name, Inversion of Control) of the bean itself controlling the instantiation or location of its dependencies by using direct construction of classes or a mechanism such as the Service Locator pattern. --spring官網
本章介紹了控制反轉原則的 Spring 框架實現。也被稱為依賴注入。這是一個過程,對象僅通過構造函數參數、工廠方法的參數或者在對象實例被構造或者從工廠方法返回后在其上設置的屬性來定義它們的依賴關系(也就是說,它們使用的其他對象)。然后容器在創建 bean 時注入這些依賴項。這個過程從根本上來說是 bean 本身的逆過程(因此得名為控制反轉) ,通過使用類的直接構造或服務定位器模式之類的機制來控制其依賴項的實例化或位置。
上面的話,簡單的說就是 使用對象的人并不和對象有直接關聯。
控制反轉:
控制反轉(Inversion of Control)是說創建對象的控制權發生轉移,以前創建對象的主動權和創建時機由應用程序把控,而現在這種權利轉交給 IoC 容器,它就是一個專門用來創建對象的工廠,你需要什么對象,它就給你什么對象。有了 IoC 容器,依賴關系就改變了,原先的依賴關系就沒了,它們都依賴 IoC容器了,通過 IoC 容器來建立它們之間的關系。
容器概述:
The org.springframework.context.ApplicationContext interface represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the beans.
Spring IOC容器就是一個org.springframework.context.ApplicationContext的實例化對象
容器負責了實例化,配置以及裝配一個bean
ApplicationContext 接口表示 Spring IoC 容器,負責實例化、配置和裝配 bean。容器通過讀取配置元數據獲取關于實例化、配置和組裝什么對象的指令。配置元數據用 XML、 Java 注釋或 Java 代碼表示。它允許您表達組成應用程序的對象以及這些對象之間豐富的相互依賴關系。
-
ClassPathXmlApplicationContext
-
FileSystemXmlApplicationContext
Spring 如何工作?
-
Spring容器通過我們提交的pojo類以及配置元數據產生一個充分配置的可以使用的系統
-
這里說的配置元數據,實際上我們就是我們提供的XML配置文件,或者通過注解方式提供的一些配置信息
bean介紹
在 Spring 中,構成應用程序主干的對象和由 Spring IoC 容器管理的對象稱為 bean。Bean 是由 Spring IoC 容器實例化、組裝和管理的對象。否則,bean 只是應用程序中的許多對象之一。
配置元數據
-
傳統上,配置元數據是以一種簡單和直觀的 XML 格式提供的
-
現在,大部分使用注解方式。
基本結構
下面的示例顯示了基于 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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="..." class="..."> <!-- collaborators and configuration for this bean go here --></bean><bean id="..." class="..."><!-- collaborators and configuration for this bean go here --></bean><!-- more bean definitions go here --></beans>-
id 屬性是標識單個 bean 定義的字符串
-
class 屬性是全限定類名
AOP
面向切面編程,是針對面向對象編程的一種補充,同時也是spring中第二個最核心的功能,例如可以進行權限認證,日志輸出等,可以無侵入的對原來的功能進行切面加入自定義的非業務功能。
自言自語
重新學習Spring 的第一天,又開始了天天光顧Spring官網的日子。
總結
以上是生活随笔為你收集整理的Spring (1) 认识Spring、 介绍Spring特点、解答为什么学习Spring的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringCloud--Eureka服
- 下一篇: Spring(2)bean注入--Set