01、Spring基礎及入門

1. Spring是什麼

Spring是一種開源輕量級框架,是爲了解決企業應用程序開發複雜性而創建的,Spring致力於解決JavaEE的各層解決方案,而不僅僅於某一層的方案。

2. Spring的發展歷程

2003年2月Spring框架正式稱爲一道開源項目,Spring致力於J2EE應用的各種解決方案,而不僅僅專注於某一層解決方案。可以說Spring是企業應用開發的“一站式”選擇, Spring貫穿於表現層、業務層、持久層,然而Spring並不想取代那些已經有的框架,而是以高度的開放性,與這些已有的框架進行整合。

3. Spring的目標

  1. 讓現有的技術更容易使用,
  2. 促進良好的編程習慣。

Spring是一個全面的解決方案,它堅持一個原則:不從新造輪子。已經有較好解決方案的領域,Spring絕不重複性實現,比如:對象持久化和OR映射,Spring只對現有的JDBC,Hibernate等技術提供支持,使之更容易使用,而不做重複的實現。Spring框架有很多特性,這些特性由7個定義良好的模塊構成。

4. Spring體系結構

在這裏插入圖片描述

  1. Spring Core:即,Spring核心,它是框架最基礎的部分,提供IOC和依賴注入特性
  2. Spring Context:即,Spring上下文容器,它是BeanFactory功能加強的一個子接口
  3. Spring Web:它提供Web應用開發的支持
  4. Spring MVC:它針對Web應用中MVC思想的實現
  5. Spring DAO:提供對JDBC抽象層,簡化了JDBC編碼,同時,編碼更具有健壯性。
  6. Spring ORM:它支持用於流行的ORM框架的整合,比如:Spring + Hibernate、Spring + iBatis、Spring + JDO的整合等等。
  7. Spring AOP:AOP即,面向切面編程,它提供了與AOP聯盟兼容的編程實現

5. Spring常用組件

在這裏插入圖片描述

6. Spring初體驗

6.1 xml文件配置類

  • 1.新建一個普通的maven項目,在pom文件引入jar包
<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.0.6.RELEASE</version>
    </dependency>
  • 2 新建個Person類
public class Person {
    private  String name;
    private Integer age;
	//getter/setter方法
	//無參/全參構造方法
  }
  • 3 在resource下建立spring的配置文件,在裏面配置bean
<?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.xsd">
    <bean id="person" class="com.zz.cap1.bean.Person">
        <property name="name" value="zhangsan"/>
        <property name="age" value="27"/>
    </bean>
</beans>
  • 4 執行測試類
public class MainTest1 {
    public static void main(String[] args) {
        //把beans.xml的類加載到容器
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Person person = (Person) context.getBean("person");
        //從容器中獲取bean
        System.out.println(person);
    }
}

ClassPathXmlApplicationContext:會加載類路徑下的XML。結果如下:
在這裏插入圖片描述

6.2 註解配置類

  • 1 新建配置類MainConfig

從Spring3.0,@Configuration用於定義配置類,可替換xml配置文件,被註解的類內部包含有一個或多個被@Bean註解的方法,這些方法將會被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext類進行掃描,並用於構建bean定義,初始化Spring容器。

注意:@Configuration註解的配置類有如下要求

  1. @Configuration不可以是final類型;
  2. @Configuration不可以是匿名類;
  3. 嵌套的configuration必須是靜態類。
//配置類====配置文件
@Configuration
public class MainConfig {

    //給容器中註冊一個bean, 類型爲返回值的類型,
    //方法名默認爲bean的id
    //如何修改bean的id
    //1,直接修改方法名
    //2,@Bean("你要命名的bean名字")
    @Bean
    public Person person() {
        return new Person("李四", 18);
    }
}
  • 2 新建MainTest2測試類
public class MainTest2 {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
        Person person = (Person) context.getBean("person");
        System.out.println(person);
    }
}

AnnoatationConfigApplicationContext: 註解配置來獲取IOC容器。結果如下:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章