一、Spring概述

1.1 Spring概述:

    1)  Spring是一個開源框架 
    2)  Spring爲簡化企業級開發而生,使用Spring,JavaBean就可以實現很多以前要靠EJB才能實現的功能。同樣的功能,在EJB中要通過繁瑣的配置和複雜的代碼才能夠實現,而在Spring中卻非常的優雅和簡潔。 
    3)  Spring是一個IOC(DI)和AOP容器框架。
    4)  Spring的優良特性
     ①   非侵入式:基於Spring開發的應用中的對象可以不依賴於Spring的API
     註解:假設大家都想要把用戶代碼塞到一個框架裏。侵入式的做法就是要求用戶代碼“知道”框架的代碼,表現爲用戶代碼需要繼承框架提供的類。非侵入式則不需要用戶代碼引入框架代碼的信息,從類的編寫者角度來看,察覺不到框架的存在。
     參考:https://blog.csdn.net/xujiangdong1992/article/details/73467922
     ②  依賴注入:DI——Dependency Injection,反轉控制(IOC)最經典的實現。
     ③  面向切面編程:Aspect Oriented Programming——AOP
     ④  容器:Spring是一個容器,因爲它包含並且管理應用對象的生命週期
     ⑤  組件化:Spring實現了使用簡單的組件配置組合成一個複雜的應用。在 Spring 中可以使用XML和Java註解組合這些對象。
    5)  一站式:在IOC和AOP的基礎上可以整合各種企業應用的開源框架和優秀的第三方類庫(實際上Spring 自身也提供了表述層的SpringMVC和持久層的Spring JDBC)。
    6)  Spring模塊

一、Spring概述

1.2 安裝Spring插件

1)  插件包:springsource-tool-suite-3.4.0.RELEASE-e4.3.1-updatesite.zip

STS=eclipse+Spring插件  更方便的使用Spring

1.3 搭建Spring運行時環境

1)  加入JAR包
① Spring自身JAR包:spring-framework-4.0.0.RELEASE\libs目錄下
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
② commons-logging-1.1.1.jar (日誌被core依賴了)
2)  在Spring Tool Suite工具中通過如下步驟創建Spring的配置文件
① File->New->Spring Bean Configuration File (創建時候帶有spring頭信息並且使用的默認版本,在xml文件中 左下角NameSpaces默認勾選beans  點擊右邊會出現版本) 
② 爲文件取名字 例如:applicationContext.xml

1.4 HelloWorld

1)  目標:使用Spring創建對象,爲屬性賦值
2)  創建Student類
        ① 定義一些屬性並給set  get方法  toString()方法等
3)  創建Spring配置文件
  <!-- 使用bean元素定義一個由IOC容器創建的對象 -->
    <!-- class屬性指定用於創建bean的全類名 -->
    <!-- id屬性指定用於引用bean實例的標識 -->
    <bean id="student" class="com.atguigu.helloworld.bean.Student">
        <!-- 使用property子元素爲bean的屬性賦值 -->
        <property name="studentId" value="1001"/>
        <property name="stuName" value="Tom2015"/>
        <property name="age" value="20"/>
    </bean>
4)  測試:通過Spring的IOC容器創建Student類實例
    //1.創建IOC容器對象
    ApplicationContext iocContainer = 
            new ClassPathXmlApplicationContext("helloworld.xml");
    //2.根據id值獲取bean實例對象
    Student student = (Student) iocContainer.getBean("student");
    //3.打印bean
    System.out.println(student);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章