Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to meet the function of personalized customization in multi-production environment under spring boot

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "how to meet the personalized customization function in the multi-production environment under spring boot". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to meet the personalized customization function in the multi-production environment under spring boot" can help you solve your doubts.

Resources and environment

Development environment: java1.8 + spring-boot:2.1.3.RELEASE

Demand hypothesis

Suppose the people who use this project are Chinese and Americans, and the acceptable languages are Chinese and English, respectively.

After the project is running, it can be dynamically displayed according to the nationality of the current visitor: Hello or hello

When there is new demand, for example: increase the number of Germans and display Hallo. When adding functionality, the core code is not changed.

Do not use if else

Note: if you are untouched after reading the requirement hypothesis, please ignore the following content of this article

Solution

In the solution, we involve two kinds of design modules, namely: strategy mode and factory mode.

Strategy pattern: it is generally used to abstract and peel off specific algorithms. In this project, our specific algorithm is to say hello.

Factory mode: generally used in situations where BEAN is created dynamically according to the environment. In the introduction project, we will return different algorithms to say hello according to people from different countries.

First give the UML diagram:

SpeakService

SpeakService is the talking service that we call for other modules. Call SayHello () to complete the function of saying hello.

Package com.mengyunzhi.demo.dynamicautowire

/ * *

* Hello

* /

Public interface SpeakService {

Void sayHello ()

}

In its implementation class, we inject SayHelloFactory to return the correct SayHelloService, and finally call sayHello () to achieve the goal.

Package com.mengyunzhi.demo.dynamicautowire

Import org.springframework.beans.factory.annotation.Autowired

Import org.springframework.stereotype.Service

/ * *

* Hello

* /

@ Service

Public class SpeakServiceImpl implements SpeakService {

Private final

SayHelloFactory sayHelloFactory; / / talking factory

@ Autowired

Public SpeakServiceImpl (SayHelloFactory sayHelloFactory) {

This.sayHelloFactory = sayHelloFactory

}

@ Override

Public void sayHello () {

This.sayHelloFactory.getSayHelloService (). SayHello ()

}

}

SayHelloFactory

Package com.mengyunzhi.demo.dynamicautowire

/ * *

* speaking factory

* /

Public interface SayHelloFactory {

Void setCountryCode (CountryCode countryCode)

SayHelloService getSayHelloService ()

}

Here, we add a CountryCode to represent the country of the current visitor. In fact, when we get the visitor's country, we can also call other Bean to achieve it.

Package com.mengyunzhi.demo.dynamicautowire

/ * *

* country code

* /

Public enum CountryCode {

CHINA ((byte) 0, "China")

USA ((byte) 1, "USA")

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

}

}

Use enum to control the scope to avoid exceptions when Factory gets the Bean.

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

/ * *

* speaking factory

* /

@ Service

Public class SayHelloFactoryImpl implements SayHelloFactory {

/ * *

* BEAN list

* /

Private final Map servicesByCode = new HashMap ()

/ * *

* country code

* /

Private CountryCode countryCode = CountryCode.CHINA

@ Override

Public void setCountryCode (CountryCode countryCode) {

This.countryCode = countryCode

}

/ * *

* initialization

*

* @ param sayHelloServices spring gets it, so it implements the BEAN of SpeakService

* /

@ Autowired

Public void init (List sayHelloServices) {

For (SayHelloService sayHelloService: sayHelloServices) {

This.register (sayHelloService.getCode (), sayHelloService)

}

}

/ * *

* sign up for Bean

*

* @ param code code

* @ param sayHelloService BEAN

* /

Private void register (Byte code, SayHelloService sayHelloService) {

This.servicesByCode.put (code, sayHelloService)

}

/ * *

* obtain BEAN

*

* @ return corresponding SayHelloService BEAN

* /

@ Override

Public SayHelloService getSayHelloService () {

Return this.servicesByCode.get (this.countryCode.getCode ())

}

}

Add Map servicesByCode to store the SayHelloService BEAN of the corresponding country. Add getSayHelloService () to return the corresponding Bean based on the current country code.

SayHelloService

Package com.mengyunzhi.demo.dynamicautowire

/ * *

* speak

* /

Public interface SayHelloService {

Void sayHello ()

Byte getCode ()

}

Extract the sayHello () method and getCode () to get the country code.

Hello, Chinese.

Package com.mengyunzhi.demo.dynamicautowire

Import org.springframework.stereotype.Component

/ * *

* Chinese

* /

@ Component

Public class SayHelloServiceChineseImpl implements SayHelloService {

@ Override

Public void sayHello () {

System.out.println ("Hello")

}

@ Override

Public Byte getCode () {

Return CountryCode.CHINA.getCode ()

}

}

American Hello

Package com.mengyunzhi.demo.dynamicautowire

Import org.springframework.stereotype.Component

/ * *

* American dialect

* /

@ Component

Public class SayHelloServiceEnglishImpl implements SayHelloService {

@ Override

Public void sayHello () {

System.out.println ("hello")

}

@ Override

Public Byte getCode () {

Return CountryCode.USA.getCode ()

}

}

test

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 () {

/ / say hello by default

SpeakService.sayHello ()

/ / set the country to the United States, and say hello

SayHelloFactory.setCountryCode (CountryCode.USA)

SpeakService.sayHello ()

/ / set the country to China, and say hello

SayHelloFactory.setCountryCode (CountryCode.CHINA)

SpeakService.sayHello ()

}

}

Hello

Hello

Hello

Time sequence diagram

Increase the number of Germans

SayHelloServiceGermanyImpl

CountryCode

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

/ * *

* country code

* /

Public enum CountryCode {

CHINA ((byte) 0, "China")

USA ((byte) 1, "USA")

GERMANY ((byte) 2, Germany)

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

}

}

Unit testing

@ Test

Public void sayHello1 () {

/ / say hello by default

SpeakService.sayHello ()

/ / set the country to the United States, and say hello

SayHelloFactory.setCountryCode (CountryCode.USA)

SpeakService.sayHello ()

/ / set the country to Germany, and say hello

SayHelloFactory.setCountryCode (CountryCode.GERMANY)

SpeakService.sayHello ()

/ / set the country to China, and say hello

SayHelloFactory.setCountryCode (CountryCode.CHINA)

SpeakService.sayHello ()

}

After reading this, the article "how to meet the personalized customization function in multi-production environment under spring boot" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 251

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report