您現在的位置是:首頁 > 綜合

Spring概述:Spring中lOC和DI介紹,Spring框架用啥方式配置資料

  • 由 java領路人 發表于 綜合
  • 2021-12-06
簡介下面改造基於XML配置元資料的例子,將其改成基於Java註解的方式來注入Bean,具體程式碼如下:註解的方式宣告UserService@Servicepublic class UserService {private Integer id

控制反轉是什麼意思

IoC和DI簡介

IoC(Inversion of Control)是“控制反轉”的意思。如何理解“控制反轉”這個詞呢?

首先我們需要知道反轉的是什麼,是由誰來控制。在Spring框架沒有出現之前,在Java面向物件的開發中,開發者透過new關鍵字完成對Object的建立。Spring框架誕生後,是透過Spring容器來管理物件的,因此Object的建立是透過Spring來完成的。最終得出結論:控制反轉指的是由開發者來控制建立物件變成了由Spring容器來控制建立物件,建立物件和銷燬物件的過程都由Spring來控制。以Spring框架為開發基礎的應用盡量不要自己建立物件,應全部交由Spring容器管理。

Spring概述:Spring中lOC和DI介紹,Spring框架用啥方式配置資料

DI(Dependency Injection)稱為依賴注入。在Java程式中,類與類之間的耦合非常頻繁,如Class A需要依賴Class B的物件b。而基於Spring框架的開發,在Class A中不需要顯式地使用new關鍵字新建一個物件b,只需在物件b的宣告之上加一行註解@Autowired,這樣在Class A用到b時,Spring容器會主動完成物件b的建立和注入。這就是Class A依賴Spring容器的注入。透過上面的解釋,我們可以發現IoC和DI其實是同一概念從不同角度的解釋。

在Spring框架中,org。springframework。context。ApplicationContext介面代表Spring IoC容器,它負責例項化、配置和組裝Beans。容器透過讀取元資料的配置來獲取物件的例項化,以及配置和組裝的描述資訊。元資料可以用XML、Java註解或Java配置程式碼表示應用的物件及這些物件之間的內部依賴關係。

Spring概述:Spring中lOC和DI介紹,Spring框架用啥方式配置資料

Spring框架提供了幾個開箱即用的ApplicationContext介面的實現類,如Class-PathXmlApplicationContext、FileSystemXmlApplicationContext和AnnotationConfig ApplicationContext等。在獨立應用程式中,通常建立一個ClassPathXmlApplication-Context或FileSystemXmlApplicationContext例項物件來獲取XML的配置資訊。

開發者也可以指示容器使用Java註解或Java配置作為元資料格式,透過Annotation-ConfigApplicationContext來獲取Java配置的Bean。

元資料配置

1。 基於XML的配置

Spring框架最早是透過XML配置檔案的方式來配置元資料的,示例程式碼如下:

<?xml version=“1。0” encoding=“UTF-8”?>

xmlns:xsi=“http://www。w3。org/2001/XMLSchema-instance”

xsi:schemaLocation=“http://www。springframework。org/schema/bea

ns

https://www。springframework。org/schema/beans/spring

beans。xsd”>

<!——定義UserService類 ——>

class=“com。spring。boot。UserService”>

<!——id屬性 ——>

<!——name屬性 ——>

在src/main/resources目錄下新建spring。xml檔案,內容如上面的程式碼所示,標籤用來描述Bean的元資料資訊。在上面的程式碼中聲明瞭一個UserService類,該類有兩個屬性,即id和name,透過標籤直接進行賦值。

UserService實體類的宣告程式碼如下:

//宣告UserService類

public class UserService {

private Integer id;

//使用者ID

private String name;

//使用者名稱稱

//getter和setter方法

public Integer getId() {

return id;

}

public void setId(Integer id) {

this。id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this。name = name;

}

//列印屬性值

public void getUser() {

System。out。println(“id:”+this。id);

System。out。println(“name:”+this。name);

}

}

以上程式碼聲明瞭一個UserService類,並實現了屬性id和屬性name的setter和getter方法,透過getUser()方法列印屬性值。編寫測試程式碼,展示透過Spring上下文獲取UserService物件,具體程式碼如下:

//測試類

public class SpringXmlTest {

public static void main(String[] args) {

//透過spring。xml獲取Spring應用上下文

ApplicationContext context = new

ClassPathXmlApplication

Context(“spring。xml”);

UserService userService =

context。getBean(“userService”,

UserService。class);

userService。getUser();

//列印結果

}

}

列印結果:

id:1

name:zhangsan

在上面的示例程式碼中,ClassPathXmlApplicationContext可以透過spring。xml檔案獲取UserService類的配置元資料,透過Spring容器的組裝和例項化UserService類,最終正確呼叫getUser()方法打印出定義的屬性值。

2。 基於Java註解的配置

從Spring 2。5開始,支援以Java註解的方式來配置Bean,如@Scope、@Service、@Component、@Controller、@Repository、@Autowired和@Resource等註解。

@Scope註解可以設定Bean的作用域。Spring容器例項化的物件預設是單例的,如果想要修改作用域,可以透過@Scope註解進行修改。表1。1中列出了@Scope註解使用的一些作用域。

Spring概述:Spring中lOC和DI介紹,Spring框架用啥方式配置資料

表1。1 @Scope註釋的作用域

request、session、application和websocket作用域只在Web應用環境中使用。在普通的Spring IoC容器裡只有singleton和prototype兩種作用域,其他的設定會丟擲異常。

下面改造基於XML配置元資料的例子,將其改成基於Java註解的方式來注入Bean,具體程式碼如下:

//註解的方式宣告UserService

@Service

public class UserService {

private Integer id;

//使用者ID

private String name;

//使用者名稱稱

//getter和setter方法

public Integer getId() {

return id;

}

public void setId(Integer id) {

this。id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this。name = name;

}

//屬性值列印

public void getUser() {

System。out。println(“id:”+this。id);

System。out。println(“name:”+this。name);

}

}

上面的程式碼在UserService類中加了一個@Service註解,spring。xml配置檔案不再使用。下面增加一個註解類,新增@ComponentScan註解,程式碼如下:

//@ComponentScan註解用來掃描UserService類

@ComponentScan(“com。spring。boot”)

public class SpringAnnotationTest {

}

@ComponentScan註解的值是com。spring。boot,說明Spring容器可以自動掃描這個包路徑下可管理的類,並對該類進行例項化。新增測試類程式碼如下:

@ComponentScan(“com。spring。boot”)

public class SpringAnnotationTest {

public static void main(String[] args) {

//透過註解類獲取應用上下文

ApplicationContext context = new

AnnotationConfigApplication

Context(SpringAnnotationTest。class);

//獲取UserService物件

UserService userService =

context。getBean(UserService。class);

userService。setId(1);

userService。setName(“zhangsan”);

userService。getUser();

//呼叫方法,列印屬性值

}

}

列印結果:

id:1

name:zhangsan

透過AnnotationConfigApplicationContext類可以獲取被@Service註解的User-Service例項化物件,並正確列印屬性值。透過Java註解的方式同樣完成了例項的初始化,說明XML配置方式可以完全被替換。

3。 基於Java配置的示例

從Spring 3。0開始,Spring框架開始支援基於Java的方式來配置元資料,如@Configuration、@Bean、@Import和@Profile等註解。

@Configuration註解一般用來配置類,配置類中可以使用@Bean註解來宣告某個類的初始化操作;@Import註解可以匯入由@Configuration註解的配置類;@Profile註解可以根據不同環境生成不同的例項。

Spring概述:Spring中lOC和DI介紹,Spring框架用啥方式配置資料

下面改造基於Java註解的案例,給出一個基於Java配置的示例。UserService類去掉@Service註解後,將變成普通的Bean。

UserService類的宣告程式碼如下:

//宣告UserService類

public class UserService {

private Integer id;

//使用者ID

private String name;

//使用者名稱稱

public Integer getId() {

return id;

}

public void setId(Integer id) {

this。id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this。name = name;

}

//屬性值列印

public void getUser() {

System。out。println(“id:”+this。id);

System。out。println(“name:”+this。name);

}

}

新增配置類,程式碼如下:

//基於@Configuration註解生成UserService物件

@Configuration

public class SpringConfigTest {

@Bean

public UserService userService() {

return new UserService();

}

}

SpringConfigTest類由@Configuration註解,表明這個類是個配置類。由@Bean註解的userService()方法返回了UserService類的例項。新增測試類程式碼如下:

@Configuration

public class SpringConfigTest {

@Bean

public UserService userService() {

return new UserService();

}

public static void main(String[] args) {

//透過配置類獲取Spring應用上下文

ApplicationContext context = new

AnnotationConfigApplication

Context(SpringConfigTest。class);

UserService userService =

context。getBean(UserService。class);

userService。setId(1);

userService。setName(“zhangsan”);

userService。getUser();

//列印屬性值

}

}

列印結果:

id:1

name:zhangsan

從上面的例子看,基於Java配置例項化物件的方式不再需要對spring。xml的依賴。基於Java註解或Java配置來管理Bean的方式已經是當今程式設計的流行方式。後文介紹Spring Boot時,還會介紹一些新的註解或配置方式。

本文給大家講解的內容是控制反轉——Spring IoC容器的理念;

下篇文章給大家講解的是

Bean管理

覺得文章不錯的朋友可以轉發此文關注小編;

感謝大家的支援!

Top