日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Spring IOC示例代码

發(fā)布時間:2025/4/16 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring IOC示例代码 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在本文中不會對概念性的東西進行講解,比如什么是IOC,因此不適合初學(xué)者。目的在于對學(xué)過Spring IOC的讀者能快速復(fù)習相關(guān)的內(nèi)容。更多細節(jié)建議參考其他資料。

使用Maven來管理項目,并在pom.xml文件中引入Spring

<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.2.RELEASE</version></dependency> </dependencies>

首先我們來創(chuàng)建我們的基礎(chǔ)應(yīng)用代碼,代碼組織結(jié)構(gòu)如下:

IAccountDao.java

/*** 模擬保存賬戶*/ public interface IAccountDao {void saveAccount(); }

AccountDaoImpl.java

/*** 賬戶的持久層實現(xiàn)類*/ public class AccountDaoImpl implements IAccountDao {public void saveAccount(){System.out.println("保存了賬戶");} }

編寫bean.xml文件

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><bean id="accountDao" class="com.chester.dao.impl.AccountDaoImpl"></bean> </beans>

編寫main函數(shù)

/*** 獲取Spring的Ioc核心容器,并生成Bean對象* @param args*/ public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");IAccountDao as = (IAccountDao)ac.getBean("accountDao");as.saveAccount(); }

運行
控制臺輸出 “保存了賬戶”


以上就是一個非常簡單的使用Spring來解耦合的過程,以下是其他一些細節(jié)知識

ApplicationContext接口的實現(xiàn)類

  • ClassPathXmlApplicationContext;
    它是從類的根路徑下加載配置文件,也是我們上面使用的方式,同時也推薦使用這種方式。
  • FileSystemXmlApplicationContext;
    它是從磁盤路徑上加載配置文件,配置文件可以在磁盤的任意位置
  • AnnotationConfigApplicationContext;
    當我們使用注釋來配置容器對象時,需要使用這個類來創(chuàng)建Spring容器,用來讀取注釋。

bean標簽

bean標簽是用于配置對象讓Spring來創(chuàng)建的,默認使用無參構(gòu)造函數(shù),如果沒有無參構(gòu)造函數(shù)將不能構(gòu)建成功。關(guān)于對象的構(gòu)建下面還會講到。

屬性:

  • id
    給對象在容器中提供一個唯一的表示,用于獲取對象
  • class
    指定類的全限定類名。用于反射創(chuàng)建對象。默認情況使用無參構(gòu)造函數(shù)
  • scope
    singleton : 單例(默認值)
    prototype : 多例
    request : WEB項目中,spring創(chuàng)建一個Bean對象,將對象存入到request域中
    session : WEB項目中,spring創(chuàng)建一個Bean對象,將對象存入到session域中
    global session : WEB項目中,應(yīng)用在Portlet環(huán)境,如果沒有Portlet環(huán)境那么相當于session
    init-method : 指定類中初始化方法的名稱
    destroy-method : 指定類中銷毀方法的名稱

實例化bean的三種方式

  • 第一種方式,默認調(diào)用無參構(gòu)造函數(shù)來創(chuàng)建對象,如果Bean中沒有無參構(gòu)造函數(shù),創(chuàng)建失敗。
<bean id="accountDao" class="com.chester.dao.impl.AccountDaoImpl"></bean>
  • 第二種方式,spring管理靜態(tài)工廠,使用靜態(tài)工廠的方法創(chuàng)建對象
public class StaticFactory{public static IAccountServer createAccountService(){return new AccountSercviceImpl();} } <bean id="accountService" class="com.chester.factory.StaticFactory" factory-method = "createAccountService"> </bean>
  • 第三種方法Spring管理實例工廠,使用實例工廠的方法創(chuàng)建對象
public class InstanceFactory{public IAccountServer createAccountService(){return new AccountSercviceImpl();} } <bean id="instancFactory" class="com.chester.factory.InstanceFactory"></bean> <bean id="accountService" factory-bean = "instancFactory"factory-method="createAccountService"></bean>

在很多情況下耦合是無法完全消除的,我們可以交由Spring來幫我們處理這些依賴情況。或者一個bean要使用另一個bean該如何處理?這就涉及到了DI(依賴注入)的知識了,可以繼續(xù)閱讀:
https://blog.csdn.net/qq_39290394/article/details/95779379

總結(jié)

以上是生活随笔為你收集整理的Spring IOC示例代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。