Spring入門

Spring是一個著名的輕量級開源框架,此框架的事一種分層架構,意味着我們能夠選擇使用其任何部分。其核心是反轉控制面向切面編程

Spring優點

  • 方便解耦、低耦合:將對象間的依賴關係交由Spring進行控制,避免硬編碼造成的過度程序耦合。
    
  • 聲明式事務管理:通過聲明式的方式更加靈活地進行事務的管理。
    
  • 方便集成多種優秀框架:提供了對多種優秀框架的支持。
    

控制反轉(Inverse of Control – IoC)

控制反轉是一種設計思想,IOC把你創建對象的權利交給框架,用於消減程序間的耦合。其主要包括:依賴注入和依賴查找。
傳統Java程序設計,我們直接通過new的方式進行對象創建,是程序主動創建依賴對象的。
在這裏插入圖片描述
引入IoC的程序,IoC通過專門的容器來創建對象。
在這裏插入圖片描述

IoC實例

  1. 引入Spring的jar包。
    <!--Spring開發包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.1.RELEASE</version>
        </dependency>
  1. 創建業務層及其實現類
/**
 * 賬戶的持久層接口
 */
public interface AccountDao {

    /**
     * 保存賬戶
     */
    void saveAccount();
}
/**
 * 賬戶持久層接口的實現類
 */
public class AccountDaoImpl implements AccountDao {

    public void saveAccount() {
    	//數據庫的操作 用於保存賬戶
        System.out.println("保存賬戶");
    }
}
  1. 創建業務層及其實現類
/**
 * 賬戶業務層接口
 */
public interface AccountService {

    /**
     * 保存賬戶
     */
    void saveAccount();
}
/**
 * 賬戶的業務層實現類
 */
public class AccountServiceImpl implements AccountService {

    private AccountDao accountDao;

    public void saveAccount() {
       accountDao.saveAccount();
    }
}
  1. 創建.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/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--bean標籤:用於配置對象,讓spring創建,並且存入ioc容器中
        id屬性:對象的唯一標識
        class屬性:指定要創建對象的全限定類名
    -->
    <!--配置dao-->
  <bean id="accountDao" class="com.liang.dao.impl.AccountDaoImpl"></bean>
    <!--配置service-->
    <bean id="accountService" class="com.liang.service.impl.AccountServiceImpl"></bean>
</beans>
  1. 測試配置是否成功
public class SpringTest {
   @Test
    public void configTest()
    {
        //通過Application接口獲取Spring容器
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        //根據bean的id獲取對象
        AccountService accountService = (AccountService)applicationContext.getBean("accountService");
        System.out.println(accountService);
        
        AccountDao accountDao = (AccountDao)applicationContext.getBean("accountDao");
        System.out.println(accountDao);
    }

通過測試可以看出。沒有通過new的方式創建對象,而是在測試方法中創建Ioc容器,通過容器來獲取對象的。

IoC容器

BeanFactory 是Spring容器的頂層接口,其下有很多子接口,例如我們上面用到的ApplicationContext接口,而ApplicationContext接口的主要實現類有:

  1. ClassPathXmlApplicationContext:從類的根路徑下加載配置文件。 
    
  2. FileSystemXmlApplicationContext:通過磁盤路徑加載配置文件,配置文件可以在磁盤任意可訪問路徑下。
    
  3. AnnotationConfigApplicationContext:使用註解配置容器對象時,需要使用此類創建容器,用以讀取註解。
    

bean標籤說明

作用: 用於配置對象,讓Spring來創建對象並存入到IoC容器中。默認情況下它調用的是類中的無參構造函數,如果沒有無參構造函數則不能創建成功。

屬性 作用
id 對象的唯一標識符,用於獲取對象
class 類的全限定類名,用於反鎖創建對象
scope 指定對象作用域 singleton:單例, prototype:多例, request:web項目中的request域, session:web項目的session域
init-method 指定類中的初始化方法名稱
destroy-method 指定類中銷燬方法的名稱

bean創建的對象的作用範圍和生命週期
單例對象:scope="singleton:一個應用只有一個實例對象。作用範圍爲整個應用。單例對象當應用加載,創建容器時,對象就被創建了,只要不銷燬容器,對象則一直存在。
多例對象:scope="prototype:每次訪問相應的bean時,都會創建對象實例.此方式創建的對象只要對象在使用中,就不會被銷燬,當對象長時間不用時,則被java垃圾回收機制回收。
bean創建對象的三種方式
方式一:使用默認無參構造函數

<bean id="accountDao" class="com.liang.dao.impl.AccountDaoImpl"></bean>

如果AccountDaoImpl類沒有默認的無參構造函數,則不能用這種方式創建。
方式二:使用靜態方法創建對象
如下:當需要獲取一個靜態方法返回的對象的時候,bean需要配置factory-method屬性

public class StaticFactory {
    /**
     * 用於獲取AccountService對象
     * @return
     */
    public static AccountService createAccountService()
    {
        return new AccountServiceImpl();
    }
}
 <bean id="accountService" class="com.liang.factory.StaticFactory" factory-method="createAccountService"></bean>

方式三:使用普通話方法創建對象
如下:當需要獲取一個普通方法返回的對象的時候,可以先把普通方法所在的類獲取到,在結合factory-beanfactory-method屬性來獲取所需對象。

public class Factory {

    /**
     * 用於獲取AccountService對象
     * @return
     */
    public  AccountService createAccountService()
    {
        return new AccountServiceImpl();
    }
}
  <bean id="factory" class="com.liang.factory.Factory"></bean>
    <bean id="accountService" factory-bean="factory" factory-method="createAccountService"></bean>
發佈了71 篇原創文章 · 獲贊 6 · 訪問量 5390
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章