詳解spring-boot下如何滿足多生產(chǎn)環(huán)境中個(gè)性化定制功能
在項(xiàng)目的開(kāi)發(fā)中,我們很難做到開(kāi)發(fā)一套標(biāo)準(zhǔn)的流程來(lái)解決所有客戶的需求。比如,我們當(dāng)前的計(jì)量項(xiàng)目,分別運(yùn)行于赤峰市和河北省。雖然兩個(gè)區(qū)域處理的業(yè)務(wù)相同,但是對(duì)細(xì)節(jié)的實(shí)現(xiàn)要求卻不同。前面也學(xué)習(xí)過(guò)計(jì)量檢定軟件,其為了解決各個(gè)定制者使用的功能需求,最后采取的方案是:將基礎(chǔ)項(xiàng)目復(fù)制多份,進(jìn)而滿足不同的客戶需求。優(yōu)點(diǎn)當(dāng)然是有的,但比起缺點(diǎn)來(lái),優(yōu)點(diǎn)便不值一提。缺點(diǎn)很明顯,總結(jié)為一句話就是:項(xiàng)目變得難以維護(hù)。所以,當(dāng)前讓我們看到的就是,幾個(gè)開(kāi)發(fā)人員,每天處于解決問(wèn)題當(dāng)中。本文將給出一種方案,來(lái)有效的規(guī)避上述問(wèn)題。
資源與環(huán)境
示例代碼:https://github.com/mengyunzhi/springBootSampleCode/tree/master/dynamic-autowire
開(kāi)發(fā)環(huán)境:java1.8 + spring-boot:2.1.3.RELEASE
需求假設(shè)
- 假設(shè)使用本項(xiàng)目的人員為:中國(guó)人、美國(guó)人,分別能接受的語(yǔ)言為中文和英文。
- 項(xiàng)目運(yùn)行后,可以根據(jù)當(dāng)前的訪問(wèn)人員是國(guó)籍來(lái)動(dòng)態(tài)顯示:
你好或hello - 有新的需求后,比如:增加德國(guó)人并顯示
Hallo。增加功能時(shí),不更改核心代碼。 - 不使用if else
注意:如果你看完需求假設(shè)后,毫無(wú)觸動(dòng),請(qǐng)忽略本文以下內(nèi)容
解決方案
解決方案中,我們涉及了兩種設(shè)計(jì)模塊,分別為:策略模式及工廠模式。
策略模式:一般用于將具體的算法進(jìn)行抽象及剝離。此項(xiàng)目中,我們的具體算法是說(shuō)你好。
工廠模式:一般用于根據(jù)環(huán)境來(lái)動(dòng)態(tài)的創(chuàng)建BEAN的情況下。引項(xiàng)目中,我們將根據(jù)不同國(guó)家的人,來(lái)返回不同的說(shuō)你好這個(gè)算法。
先給出UML圖:

SpeakService
SpeakService即為我們供其它模塊調(diào)用的說(shuō)話服務(wù),調(diào)用其中的SayHello()來(lái)完成說(shuō)你好功能。
package com.mengyunzhi.demo.dynamicautowire;
/**
* 你好
*/
public interface SpeakService {
void sayHello();
}
在其實(shí)現(xiàn)類中,我們注入SayHelloFactory,讓其來(lái)返回正確的SayHelloService,最終調(diào)用sayHello()來(lái)完成目標(biāo)。
package com.mengyunzhi.demo.dynamicautowire;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* 你好
*/
@Service
public class SpeakServiceImpl implements SpeakService {
private final
SayHelloFactory sayHelloFactory; // 說(shuō)話工廠
@Autowired
public SpeakServiceImpl(SayHelloFactory sayHelloFactory) {
this.sayHelloFactory = sayHelloFactory;
}
@Override
public void sayHello() {
this.sayHelloFactory.getSayHelloService().sayHello();
}
}
SayHelloFactory
package com.mengyunzhi.demo.dynamicautowire;
/**
* 說(shuō)話工廠
*/
public interface SayHelloFactory {
void setCountryCode(CountryCode countryCode);
SayHelloService getSayHelloService();
}
在此,我們?cè)黾右粋€(gè)CountryCode表示當(dāng)前訪問(wèn)者的國(guó)家。其實(shí)在獲取訪問(wèn)者國(guó)家時(shí),我們也可以調(diào)用其它Bean的其它來(lái)實(shí)現(xiàn)。
package com.mengyunzhi.demo.dynamicautowire;
/**
* 國(guó)家代碼
*/
public enum CountryCode {
CHINA((byte) 0, "中國(guó)"),
USA((byte) 1, "美國(guó)");
private Byte code;
private String name;
CountryCode(Byte code, String name) {
this.code = code;
this.name = name;
}
public Byte getCode() {
return code;
}
public String getName() {
return name;
}
}
使用enum來(lái)控制范圍,避免Factory在獲取Bean時(shí)發(fā)生異常。
package com.mengyunzhi.demo.dynamicautowire;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 說(shuō)話工廠
*/
@Service
public class SayHelloFactoryImpl implements SayHelloFactory {
/**
* BEAN列表
*/
private final Map<Byte, SayHelloService> servicesByCode = new HashMap<>();
/**
* 國(guó)家代碼
*/
private CountryCode countryCode = CountryCode.CHINA;
@Override
public void setCountryCode(CountryCode countryCode) {
this.countryCode = countryCode;
}
/**
* 初始化
*
* @param sayHelloServices spring獲取到的所以實(shí)現(xiàn)了SpeakService的BEAN
*/
@Autowired
public void init(List<SayHelloService> sayHelloServices) {
for (SayHelloService sayHelloService : sayHelloServices) {
this.register(sayHelloService.getCode(), sayHelloService);
}
}
/**
* 注冊(cè)Bean
*
* @param code 代碼
* @param sayHelloService BEAN
*/
private void register(Byte code, SayHelloService sayHelloService) {
this.servicesByCode.put(code, sayHelloService);
}
/**
* 獲取BEAN
*
* @return 對(duì)應(yīng)的SayHelloService BEAN
*/
@Override
public SayHelloService getSayHelloService() {
return this.servicesByCode.get(this.countryCode.getCode());
}
}
增加Map<Byte, SayHelloService> servicesByCode來(lái)存儲(chǔ)對(duì)應(yīng)國(guó)家的SayHelloServiceBEAN。增加getSayHelloService()來(lái)根據(jù)當(dāng)前國(guó)家代碼來(lái)返回相應(yīng)的Bean。
SayHelloService
package com.mengyunzhi.demo.dynamicautowire;
/**
* 說(shuō)話
*/
public interface SayHelloService {
void sayHello();
Byte getCode();
}
將sayHello()方法抽離,getCode()以獲取國(guó)家代碼。
中國(guó)人你好
package com.mengyunzhi.demo.dynamicautowire;
import org.springframework.stereotype.Component;
/**
* 中國(guó)話
*/
@Component
public class SayHelloServiceChineseImpl implements SayHelloService {
@Override
public void sayHello() {
System.out.println("您好");
}
@Override
public Byte getCode() {
return CountryCode.CHINA.getCode();
}
}
美國(guó)人Hello
package com.mengyunzhi.demo.dynamicautowire;
import org.springframework.stereotype.Component;
/**
* 美國(guó)話
*/
@Component
public class SayHelloServiceEnglishImpl implements SayHelloService {
@Override
public void sayHello() {
System.out.println("hello");
}
@Override
public Byte getCode() {
return CountryCode.USA.getCode();
}
}
測(cè)試
package com.mengyunzhi.demo.dynamicautowire;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest
@RunWith(SpringRunner.class)
public class SpeakServiceImplTest {
@Autowired
SpeakService speakService;
@Autowired
SayHelloFactory sayHelloFactory;
@Test
public void sayHello() {
// 默認(rèn)說(shuō)你好
speakService.sayHello();
// 將國(guó)家設(shè)置為美國(guó),再說(shuō)你好
sayHelloFactory.setCountryCode(CountryCode.USA);
speakService.sayHello();
// 將國(guó)家設(shè)置為中國(guó),再說(shuō)你好
sayHelloFactory.setCountryCode(CountryCode.CHINA);
speakService.sayHello();
}
}
您好
hello
您好
時(shí)序圖

增加德國(guó)人
增加德國(guó)人SayHelloServiceGermanyImpl.
在CountryCode中,增加德國(guó).
package com.mengyunzhi.demo.dynamicautowire;
import org.springframework.stereotype.Component;
@Component
public class SayHelloServiceGermanyImpl implements SayHelloService {
@Override
public void sayHello() {
System.out.println("Hallo");
}
@Override
public Byte getCode() {
return CountryCode.GERMANY.getCode();
}
}
package com.mengyunzhi.demo.dynamicautowire;
/**
* 國(guó)家代碼
*/
public enum CountryCode {
CHINA((byte) 0, "中國(guó)"),
USA((byte) 1, "美國(guó)"),
GERMANY((byte) 2, "德國(guó)");
private Byte code;
private String name;
CountryCode(Byte code, String name) {
this.code = code;
this.name = name;
}
public Byte getCode() {
return code;
}
public String getName() {
return name;
}
}
單元測(cè)試
@Test
public void sayHello1() {
// 默認(rèn)說(shuō)你好
speakService.sayHello();
// 將國(guó)家設(shè)置為美國(guó),再說(shuō)你好
sayHelloFactory.setCountryCode(CountryCode.USA);
speakService.sayHello();
// 將國(guó)家設(shè)置為德國(guó),再說(shuō)你好
sayHelloFactory.setCountryCode(CountryCode.GERMANY);
speakService.sayHello();
// 將國(guó)家設(shè)置為中國(guó),再說(shuō)你好
sayHelloFactory.setCountryCode(CountryCode.CHINA);
speakService.sayHello();
}
測(cè)試結(jié)果如下:
您好
hello
Hallo
您好
總結(jié)
在解決問(wèn)題時(shí),只所有我們看的不夠遠(yuǎn),可能是由于自己站的不夠高。同樣的問(wèn)題,困惑我了多日,直到近期系統(tǒng)的學(xué)習(xí)設(shè)計(jì)模式 、angular官方教程、Spring 實(shí)戰(zhàn)后,結(jié)合近期項(xiàng)目變更帶來(lái)的新需求,才在使用設(shè)計(jì)模式解決此問(wèn)題上有所啟發(fā)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- spring中通過(guò)ApplicationContext getBean獲取注入對(duì)象的方法實(shí)例
- Springboot-dubbo-fescar 阿里分布式事務(wù)的實(shí)現(xiàn)方法
- Spring 中優(yōu)雅的獲取泛型信息的方法
- spring @Transactional 無(wú)效的解決方案
- spring boot定時(shí)任務(wù)接收郵件并且存儲(chǔ)附件的方法講解
- activemq整合springboot使用方法(個(gè)人微信小程序用)
- Springboot項(xiàng)目打war包docker包找不到resource下靜態(tài)資源的解決方案
- Spring自帶的校驗(yàn)框架Validation的使用實(shí)例
- 詳解Spring框架下向異步線程傳遞HttpServletRequest參數(shù)的坑
- Spring通過(guò)ApplicationContext主動(dòng)獲取bean的方法講解
相關(guān)文章
Java?json轉(zhuǎn)換實(shí)體類(JavaBean)及實(shí)體類(JavaBean)轉(zhuǎn)換json代碼示例
這篇文章主要介紹了兩種常見(jiàn)的JSON與Java實(shí)體類相互轉(zhuǎn)換的方法,分別是使用庫(kù)Jackson、Gson、Fastjson和在線工具,無(wú)論是將JSON轉(zhuǎn)換為Java實(shí)體類還是將Java實(shí)體類轉(zhuǎn)換為JSON,這些方法都能顯著簡(jiǎn)化開(kāi)發(fā)過(guò)程,需要的朋友可以參考下2024-12-12
dubbo如何實(shí)現(xiàn)consumer從多個(gè)group中調(diào)用指定group的provider
這篇文章主要介紹了dubbo如何實(shí)現(xiàn)consumer從多個(gè)group中調(diào)用指定group的provider問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
使用注解@Recover優(yōu)化丑陋的循環(huán)詳解
我們知道在實(shí)現(xiàn)一個(gè)功能的時(shí)候是可以使用不同的代碼來(lái)實(shí)現(xiàn)的,那么相應(yīng)的不同實(shí)現(xiàn)方法的性能肯定也是有差別的,下面這篇文章主要給大家介紹了關(guān)于使用注解@Recover優(yōu)化丑陋的循環(huán)的相關(guān)資料,需要的朋友可以參考下2022-04-04
分布式調(diào)度XXL-Job整合Springboot2.X實(shí)戰(zhàn)操作過(guò)程(推薦)
這篇文章主要介紹了分布式調(diào)度XXL-Job整合Springboot2.X實(shí)戰(zhàn)操作,包括定時(shí)任務(wù)的使用場(chǎng)景和常見(jiàn)的定時(shí)任務(wù),通過(guò)本文學(xué)習(xí)幫助大家該選擇哪個(gè)分布式任務(wù)調(diào)度平臺(tái),對(duì)此文感興趣的朋友一起看看吧2022-04-04
ssm整合之Spring整合MyBatis框架配置事務(wù)的詳細(xì)教程
這篇文章主要介紹了ssm整合之Spring整合MyBatis框架配置事務(wù),本文通過(guò)圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
Java數(shù)據(jù)庫(kù)連接_jdbc-odbc橋連接方式(詳解)
下面小編就為大家?guī)?lái)一篇Java數(shù)據(jù)庫(kù)連接_jdbc-odbc橋連接方式(詳解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08

