springIOC的使用流程及spring中使用類型轉(zhuǎn)換器的方式
一、Spring – 控制反轉(zhuǎn)IOC
Spring IOC(Inversion of Control,控制反轉(zhuǎn))是Spring框架的核心原理之一。它是一種軟件設(shè)計模式,用于管理應(yīng)用程序中的對象依賴關(guān)系。
1. IOC的基本原理
在傳統(tǒng)開發(fā)模式下:
對象的創(chuàng)建和依賴關(guān)系的管理通常由開發(fā)人員手動完成。
這種方式導(dǎo)致了高度耦合的代碼,使得應(yīng)用程序難以維護和擴展。
而Spring IOC通過控制對象的創(chuàng)建和依賴關(guān)系的注入,實現(xiàn)了對象之間的解耦。
所有的對象都交給IOC容器來管理,我們直接調(diào)用即可
基本原理:
Spring IOC的基本原理是將對象的控制權(quán)從應(yīng)用程序代碼中轉(zhuǎn)移到框架中。
它通過配置文件或注解描述對象之間的依賴關(guān)系,然后由Spring框架負責(zé)創(chuàng)建和管理這些對象。
具體而言,Spring IOC的原理如下:
- 配置:首先,開發(fā)人員需要在配置文件(如XML文件)或使用注解的方式中描述對象及其依賴關(guān)系。這些配置文件包含了對象的定義、依賴關(guān)系以及其他相關(guān)的配置信息。
- 加載:Spring框架會讀取配置文件,并根據(jù)配置信息創(chuàng)建對象的定義。這個過程通常稱為"加載"或"解析"。
- 創(chuàng)建:一旦對象定義被加載,Spring框架將根據(jù)這些定義創(chuàng)建對象的實例。對象的創(chuàng)建過程由Spring容器負責(zé)管理,可以通過反射、工廠模式等方式進行對象的實例化。
- 注入:對象創(chuàng)建完成后,Spring框架會查找對象所依賴的其他對象,并將這些對象注入到目標(biāo)對象中。這種依賴關(guān)系的注入可以通過構(gòu)造函數(shù)、屬性或方法進行。
- 管理:一旦對象被創(chuàng)建和注入依賴關(guān)系,Spring框架將負責(zé)管理這些對象的生命周期。它會跟蹤對象的創(chuàng)建、使用和銷毀,并在需要時進行適當(dāng)?shù)奶幚怼?/li>
通過Spring IOC,開發(fā)人員可以將應(yīng)用程序的業(yè)務(wù)邏輯與對象的創(chuàng)建和依賴管理解耦。這樣可以提高代碼的可維護性、可測試性和可擴展性,同時也降低了代碼之間的耦合度,使得應(yīng)用程序更加靈活和可配置。
需要注意的是,Spring框架提供了多種實現(xiàn)IOC的方式,包括XML配置、基于注解的配置和基于Java的配置等。開發(fā)人員可以根據(jù)實際需求選擇適合的方式來實現(xiàn)IOC。
2.IOC的步驟 – 以XML格式為例
導(dǎo)入相關(guān)的依賴包
spring-core.jar:提供了Spring框架的核心功能,包括Bean定義、依賴注入等。 spring-beans.jar:包含了Spring框架的Bean工廠和Bean定義的相關(guān)類。 spring-context.jar:提供了Spring框架的應(yīng)用上下文,包括IOC容器的實現(xiàn)。 spring-expression.jar:包含了Spring框架的表達式語言模塊,用于支持在配置文件中使用表達式進行配置。
創(chuàng)建JavaBean:
package com.springIOC.pojo; import java.util.Date; /** * Author : YWJ * Date : 2023/6/9 * Name : 框架 */ public class Student { private String name ; private Integer age ; private String gender ; private Date birth ; public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", gender='" + gender + '\'' + ", birth=" + birth + '}'; } }
在配置文件中編寫對應(yīng)該JavaBean的配置信息
<?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 = "testStudent" class = "com.springIOC.pojo.Student"> <property name="name" value="測試"></property> <property name="age" value="18"></property> <property name="gender" value="男"></property> <property name="birth" value="2023-01-09"></property> </bean> </beans>
測試
package com.springIOC; import com.springIOC.pojo.Student; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Author : YWJ * Date : 2023/6/9 * Name : 框架 */ public class test { @Test public void test01(){ ApplicationContext ac = new ClassPathXmlApplicationContext("application.xml"); Student student = ac.getBean("testStudent", Student.class); System.out.println(student); } }
IOC的實現(xiàn)步驟可總結(jié)為下:
- 配置Spring容器:首先,需要創(chuàng)建一個Spring容器來管理對象的創(chuàng)建和依賴關(guān)系??梢酝ㄟ^XML配置文件、基于注解的配置或者Java配置等方式進行配置。
- 定義Bean:在配置文件或者注解中定義需要由Spring容器管理的Bean對象。可以指定Bean的類名、作用域、依賴關(guān)系等信息。
- 加載配置:Spring框架會讀取配置文件或者掃描注解,將配置信息加載到Spring容器中。這個過程可以通過XML解析、注解處理器等實現(xiàn)。
- 創(chuàng)建Bean實例:Spring容器根據(jù)配置信息,通過反射或者工廠模式等方式創(chuàng)建Bean對象的實例。
- 處理依賴注入:一旦Bean實例創(chuàng)建完成,Spring容器會解析Bean對象的依賴關(guān)系,將依賴的對象注入到目標(biāo)Bean中。依賴注入可以通過構(gòu)造函數(shù)注入、屬性注入或者方法注入等方式實現(xiàn)。
- 管理Bean的生命周期:Spring容器會管理Bean對象的生命周期,包括初始化和銷毀。可以通過初始化方法和銷毀方法進行定義和配置。
- 獲取Bean實例:在應(yīng)用程序中,可以通過Spring容器來獲取已經(jīng)創(chuàng)建和管理的Bean實例??梢愿鶕?jù)Bean的名稱或者類型進行獲取。
二、Spring類型轉(zhuǎn)換器
在Spring框架中,類型轉(zhuǎn)換器(Type Converter)用于將一個數(shù)據(jù)類型轉(zhuǎn)換為另一個數(shù)據(jù)類型,以滿足應(yīng)用程序的需求。Spring提供
了多種類型轉(zhuǎn)換器,包括內(nèi)置的默認類型轉(zhuǎn)換器和自定義的類型轉(zhuǎn)換器。
1. 常見的類型轉(zhuǎn)換器
String類型轉(zhuǎn)換器:用于將String類型轉(zhuǎn)換為其他基本數(shù)據(jù)類型(如int、boolean、double等)或自定義的對象類型。Spring默認提供了基本數(shù)據(jù)類型和常見的Java類的String轉(zhuǎn)換器。
// 將String轉(zhuǎn)換為int類型 String intString = "123"; int intValue = Integer.parseInt(intString); // 將String轉(zhuǎn)換為boolean類型 String booleanString = "true"; boolean booleanValue = Boolean.parseBoolean(booleanString);
數(shù)字類型轉(zhuǎn)換器:用于在不同數(shù)字類型之間進行轉(zhuǎn)換,如int、long、float、double等。
// 將String轉(zhuǎn)換為double類型 String doubleString = "3.14"; double doubleValue = Double.parseDouble(doubleString); // 將int轉(zhuǎn)換為String類型 int intValue = 42; String intString = Integer.toString(intValue);
日期類型轉(zhuǎn)換器:用于處理日期和時間類型的轉(zhuǎn)換,包括Java.util.Date、Java.time.LocalDate等。
// 使用@DateTimeFormat注解將String轉(zhuǎn)換為LocalDate類型 public void setDateOfBirth(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate dateOfBirth) { this.dateOfBirth = dateOfBirth; } // 將LocalDate轉(zhuǎn)換為String類型 LocalDate currentDate = LocalDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); String dateString = currentDate.format(formatter);
集合類型轉(zhuǎn)換器:用于將字符串或數(shù)組轉(zhuǎn)換為集合類型,如List、Set、Map等。
// 將逗號分隔的字符串轉(zhuǎn)換為List<Integer>類型 String numbers = "1,2,3,4,5"; List<Integer> numberList = Arrays.stream(numbers.split(",")) .map(Integer::parseInt) .collect(Collectors.toList()); // 將數(shù)組轉(zhuǎn)換為Set<String>類型 String[] fruitsArray = {"apple", "banana", "orange"}; Set<String> fruitSet = new HashSet<>(Arrays.asList(fruitsArray));
自定義類型轉(zhuǎn)換器:開發(fā)人員可以實現(xiàn)自定義的類型轉(zhuǎn)換器,以處理應(yīng)用程序特定的數(shù)據(jù)類型轉(zhuǎn)換需求。
自定義類型轉(zhuǎn)換器需要實現(xiàn)org.springframework.core.convert.converter.Converter
接口
或繼承org.springframework.core.convert.converter.GenericConverter
接口,
并注冊到Spring容器中。
//自定義轉(zhuǎn)化器 public class MyCustomConverter implements Converter<String, MyCustomType> { @Override public MyCustomType convert(String source) { // 自定義轉(zhuǎn)換邏輯 // 將String轉(zhuǎn)換為MyCustomType對象 // ... } }
<!-- 注冊到配置文件 --> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <list> <bean class="com.example.MyCustomConverter" /> </list> </property> </bean>
//或者使用注解方式注冊 @Configuration public class MyConfiguration { @Bean public FormattingConversionServiceFactoryBean conversionService() { FormattingConversionServiceFactoryBean conversionService = new FormattingConversionServiceFactoryBean(); conversionService.setConverters(Collections.singleton(new MyCustomConverter())); return conversionService; } }
到此這篇關(guān)于springIOC的使用流程以及spring中如何使用類型轉(zhuǎn)換器的文章就介紹到這了,更多相關(guān)spring類型轉(zhuǎn)換器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于CyclicBarrier和CountDownLatch的使用區(qū)別說明
這篇文章主要介紹了基于CyclicBarrier和CountDownLatch的使用區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09java實現(xiàn)ssh登錄linux并執(zhí)行命令的三種實現(xiàn)方式
文章介紹了三種在Java中實現(xiàn)SSH登錄Linux并執(zhí)行命令的方法,包括使用ganymed-ssh2、jsch和sshd-core,由于ganymed-ssh2和jsch的最新版本較舊,可能無法與較新的Linux系統(tǒng)兼容,而sshd-core一直在更新,推薦使用2024-11-11