使用idea搭建spring項(xiàng)目,利用xml文件的形式進(jìn)行配置方式
寫在前面
利用Spring IOC和Spring DI的思想開發(fā)一個(gè)打印機(jī)模擬程序,使其滿足以下條件。
1、可以靈活地配置彩色墨盒或灰色墨盒。
2、可以靈活地配置打印頁面的大小。
一、項(xiàng)目搭建
新建一個(gè)maven項(xiàng)目,項(xiàng)目結(jié)構(gòu)圖如下:
二、詳細(xì)代碼
1、pom.xml
這里我們利用5.3.25版本的Spring,當(dāng)然你也可以使用別的版本的依賴,但是版本太高了可能會(huì)報(bào)錯(cuò),也許是與idea版本不兼容的原因,作者使用的IDEA版本為2020版。
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.3.25</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.3.25</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.3.25</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.25</version> </dependency> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.26</version> </dependency> </dependencies>
2、創(chuàng)建接口Ink和Paper
創(chuàng)建兩個(gè)接口類,包含得到顏色和獲得內(nèi)容的方法。
public interface Ink { public String getColor(); } public interface Paper { public String getContent(); public void putInChars(String content); }
3、創(chuàng)建不同的接口實(shí)現(xiàn)類
創(chuàng)建ColorInk、GreyInk實(shí)現(xiàn)Ink接口,此處將三原色直接賦值了,也可以不賦值,在配置文件中加入屬性的賦值。TextPaper原理相同。
public class ColorInk implements Ink { private int red = 10; private int green = 20; private int blue = 30; @Override public String getColor() { Color color = new Color(this.red,this.green,this.blue); return "#"+Integer.toHexString(color.getRGB()).substring(2); } } public class GreyInk implements Ink { private int red = 20; private int green = 30; private int blue = 40; @Override public String getColor() { int c = (this.red+this.green+this.blue)/3; Color color = new Color(c,c,c); return "#"+Integer.toHexString(color.getRGB()).substring(2); } } public class TextPaper implements Paper { private int charPerLine = 10; private int linePerLine = 8; private String content; @Override public String getContent() { String out = "每行字符數(shù)"+ this.charPerLine +"\n"; out += "每頁行數(shù)" + this.linePerLine + "\n"; out += this.content; return out; } @Override public void putInChars(String content) { this.content = content; } }
4、Printer類
加入@Data注解,提供setter()和getter()方法,也可以不加注解,直接給出其方法。
@Data @NoArgsConstructor public class Printer { private Ink ink; private Paper paper; public void print(String str) { System.out.println("使用"+ink.getColor()+"顏色打印"); paper.putInChars(str); System.out.println(paper.getContent()); } }
5、配置文件spring-config.xml
編寫配置文件,利用Spring容器實(shí)現(xiàn)控制反轉(zhuǎn)和依賴注入。
<?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--> <bean id="greyInk" class="com.hq.GreyInk" /> <bean id="colorInk" class="com.hq.ColorInk" /> <bean id="textPaper" class="com.hq.TextPaper" /> <!--在printer中注入ink和paper,ink使用greyInk,paper使用textPaper,此處利用面向接口編程的思想--> <bean id="printer" class="com.hq.Printer"> <property name="ink" ref="greyInk" /> <property name="paper" ref="textPaper" /> </bean> </beans>
6、編寫測試類
public class TestPrinter { public static void main(String[] args) { // 通過配置文件啟動(dòng)容器 ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); // printer即為定義的bean,此時(shí)注入的是greyInk Printer printer = (Printer) context.getBean("printer"); printer.print("hello world!"); } }
三、運(yùn)行結(jié)果
如果修改配置文件中printer的bean定義,如下所示:
<bean id="printer" class="com.hq.Printer"> <property name="ink" ref="colorInk" /> <property name="paper" ref="textPaper" /> </bean>
此時(shí),運(yùn)行結(jié)果為:
四、額外擴(kuò)展
如果不在類中定義屬性時(shí)即賦值,那么配置文件可以進(jìn)行如下配置:
通過< property>子元素實(shí)現(xiàn):
Spring調(diào)用Bean的默認(rèn)構(gòu)造方法來實(shí)例化Bean對象,然后通過反射的方式調(diào)用setter方法來注入屬性值,為對象賦值。要求Bean類必須提供一個(gè)默認(rèn)的無參數(shù)構(gòu)造方法, Bean類為需要注入的屬性提供對應(yīng)的setter方法。
通過構(gòu)造注入:
使用此元素傳入構(gòu)造方法的參數(shù)進(jìn)行實(shí)例化,type屬性指定構(gòu)造參數(shù)類型,參數(shù)值可通過ref屬性或value屬性直接指定,也可以通過ref或value子元素指定。
<bean id="ColorInk" class="com.hq.ColorInk"> <!--使用<property>標(biāo)簽進(jìn)行定義,name屬性為指定類中的屬性,value屬性為其賦值。--> <property name="red" value="10" /> <property name="green" value="20" /> <property name="blue" value="30" /> </bean> <bean id="greyInk" class="com.hq.GreyInk"> <property name="red" value="20" /> <property name="green" value="30" /> <property name="blue" value="40" /> </bean> <bean id="textPaper" class="com.hq.TextPaper"> <!--通過構(gòu)造函數(shù)注入--> <constructor-arg value="10" /> <constructor-arg value="8" /> </bean> <bean id="printer" class="com.hq.Printer"> <constructor-arg ref="greyInk" /> <constructor-arg ref="textPaper" /> </bean>
總結(jié)
大家可以嘗試一下使用這種方式進(jìn)行配置,熟悉各種標(biāo)簽的作用。好了,今天就先到這里。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java的Struts2框架中攔截器使用的實(shí)例教程
攔截器是Struts框架的重要特性,Struts中每一個(gè)Action請求都包裝在一系列的攔截器的內(nèi)部,這里我們就來看一下Java的Struts2框架中攔截器使用的實(shí)例教程2016-07-07SpringBoot整合阿里云視頻點(diǎn)播的過程詳解
視頻點(diǎn)播(ApsaraVideo for VoD)是集音視頻采集、編輯、上傳、自動(dòng)化轉(zhuǎn)碼處理、媒體資源管理、分發(fā)加速于一體的一站式音視頻點(diǎn)播解決方案。這篇文章主要介紹了SpringBoot整合阿里云視頻點(diǎn)播的詳細(xì)過程,需要的朋友可以參考下2021-12-12Spark隨機(jī)森林實(shí)現(xiàn)票房預(yù)測
這篇文章主要為大家詳細(xì)介紹了Spark隨機(jī)森林實(shí)現(xiàn)票房預(yù)測,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08基于SpringBoot和Dify實(shí)現(xiàn)流式響應(yīng)輸出
這篇文章主要為大家詳細(xì)介紹了如何基于SpringBoot和Dify實(shí)現(xiàn)流式響應(yīng)輸出效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下2025-03-03Spring?boot?整合RabbitMQ實(shí)現(xiàn)通過RabbitMQ進(jìn)行項(xiàng)目的連接
RabbitMQ是一個(gè)開源的AMQP實(shí)現(xiàn),服務(wù)器端用Erlang語言編寫,支持多種客戶端,這篇文章主要介紹了Spring?boot?整合RabbitMQ實(shí)現(xiàn)通過RabbitMQ進(jìn)行項(xiàng)目的連接,需要的朋友可以參考下2022-10-10Springboot指定掃描路徑的實(shí)現(xiàn)示例
本文主要介紹了Springboot指定掃描路徑的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05