1. 程式人生 > >Spring 第一天:spring 概念及簡單入門

Spring 第一天:spring 概念及簡單入門

Spring 第一天的學習

spring是什麼:

  • struts是web框架(jsp/action/actionform)

  • hibemate是orm框架,處於持久層

  • spring是一種框架,是一種容器框架,該框架可以配置各個層的元件,並維護bean與bean之間的關係的框架,可以管理web層,業務層,dao層,持久層,該框架可以配置各個層的元件,並且維護各個bean之間的關係

什麼是bean
  • bean是java中的任何一種物件,javabean,service,action,資料來源,dao,ioc,di

  • ioc:控制反轉,英文全稱 inverse of control

  • di: 依賴注入,英文全稱為 dependency injection

  • spring層次圖

快速入門案例

  • 寫一個普通的類
package com.service;

/**
 * Created by hejian on 16/9/5.
 */
public class UserService {
    private String name;
    public String getName(){
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void sayHello(){
        System.out.println("hellow"
+ name); } }
  • applicationContext.xml配置如下,此檔案為核心配置檔案,主要配置bean,該檔案一般放在src目錄下,也可以放在其它目錄下,此檔案配置bean,這個bean可以是service,dao,domain,action,資料來源
<?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 = "userService" class="com.service.UserService"> <!-- bean元素的作用:當spring框架,載入時,spring會自動建立一個bean物件,並放入記憶體,並管理起來 相當於:UserService userservice = new UserService(); --> <property name="name"><!--新增屬性 此處相應的類中必須有setName方式,否則注入不進去--> <value>美團</value> </property> <!--也可以這樣寫<property name = "name" value = "美團" />--> </bean> </beans>
  • 測試這個類中的方法
package com.test;
import com.service.BybService;
import com.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * Created by hejian on 16/9/5.
 */
public class Test {
    //使用傳統方法來呼叫UserService的sayHello方法
    public  static void main(String [] args){
        UserService userService = new UserService();
        userService.setName("美團");
        userService.sayHello();
    //使用spring來呼叫,需建立applicationContext.xml檔案
    //得到spring容器物件applicationContext.xml物件
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService us = (UserService) ac.getBean("userService");//getBean返回一個物件的例項  其中ac.getBean("")括號中的名稱是bean中的id,應是一個類名
        us.sayHello();    
    }

上述過程如下
- 引入spring的相關jar包,常用的包有spring-beans-4.0.0.RELEASE.jar,commons-logging-1.2.jar,spring-context-4.0.0.RELEASE.jar包
- 建立一個applicationContext.xml檔案,是spring的核心檔案
- 建立一個bean,id = “為類名稱” class =”為類的全路徑”

bean與 bean的巢狀
- 寫一個普通的類,引用BybService類

package com.service;

/**
 * Created by hejian on 16/9/5.
 */
public class UserService {
    private String name;
    private BybService bybService;
    public String getName(){
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void sayHello(){
        System.out.println("hellow" + name);
        bybService.sayByb();  //引用BybService類的一個方法
    }
}
  • 被引用的一個普通的類BybService
package com.service;

/**
 * Created by hejian on 16/9/5.
 */
public class BybService {
    private String name;
    public String getName(){
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void sayByb(){
        System.out.println("hellow" + name);
    }
}

application.xml檔案的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 = "userService" class="com.service.UserService">
    <!-- bean元素的作用:當spring框架,載入時,spring會自動建立一個bean物件,並放入記憶體,並管理起來
    相當於:UserService userservice = new UserService();
    -->
        <property name="name"><!--新增屬性 此處相應的類中必須有setName方式,否則注入不進去-->
            <value>美團</value>
        </property>
        <!--也可以這樣寫<property name = "name" value = "美團" />-->
        <bean id = "bybService" ref = "bybservice" >
        <!--上述的id值為UserService中引用的屬性,ref值為**下一個bean的id 值**-->
    </bean>

    <bean **id = "bybService"** class ="com.service.Bybservice">
        <property name = "name"  value = "張三" />
    </bean>
</beans>

spring執行原理

spring框架什麼時候被載入
- 當ClassPathXmlApplicationContext(“applicationContext.xml”)執行的時候,spring 容器物件被建立,同時applicationContext.xml配置bean就會被建立。

bean是怎樣被載入的
- spring容器掃描applicationContext.xml檔案,同時通過反射機制在記憶體中建立 bean中元素
- 且通過getbean()獲取相應的bean
- 對應的bean應用相應的set,get 方法