Внедрить службу Spring Boot на основе конфигураций файла ymlJAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Внедрить службу Spring Boot на основе конфигураций файла yml

Сообщение Anonymous »

У меня есть следующий тестовый код Java в микросервисе Spring Boot:
https://github.com/rcbandit111/profile- ... main/java/ com/profile/protection/admin/service/impl/KeysServiceImpl.java#L27
Мне нужно обработать запрос API на основе конфигурации yml:
Пример:

Код: Выделить всё

profile-protection:
dataClasses:
- name: USER_INFO
activeVersionName: version-1
versions:
- versionName: version-1
algorithm: AES
encryptKeyName: user
- name: SAMPLE_ENCRYPTION
activeVersionName: version-1
versions:
- versionName: version-1
algorithmName: AES
encryptKeyName: user_aes
Я могу придумать решение — создать модель Factory, которая выбирает службы реализации на основе значения ключа:

Код: Выделить всё

@Component
public class EncryptionServiceFactory {

private final ApplicationContext applicationContext;
private final EncryptionServiceProperties encryptionServiceProperties;

@Autowired
public EncryptionServiceFactory(ApplicationContext applicationContext, EncryptionServiceProperties encryptionServiceProperties) {
this.applicationContext = applicationContext;
this.encryptionServiceProperties = encryptionServiceProperties;
}

public EncryptionService getService(String dataClassName) {
return (EncryptionService) applicationContext.getBean(dataClassName);
}
}

.....

// Implementation:

public interface EncryptionService {
String encrypt(String plaintext);
String decrypt(String ciphertext);
}

@Service("USER_INFO")
public class UserInfoEncryptionService implements EncryptionService {

// This would use configuration values for encryption
@Override
public String encrypt(String plaintext) {
// Implement AES/CBC/PKCS5Padding encryption logic here
return "encrypted-" + plaintext;
}

@Override
public String decrypt(String ciphertext) {
// Implement AES/CBC/PKCS5Padding decryption logic here
return "decrypted-" + ciphertext;
}
}

.....

@Service("SAMPLE_ENCRYPTION")
public class SampleEncryptionService implements EncryptionService {

// This would use configuration values for encryption
@Override
public String encrypt(String plaintext) {
// Implement AES/CBC/PKCS5Padding encryption logic here
return "encrypted-" + plaintext;
}

@Override
public String decrypt(String ciphertext) {
// Implement AES/CBC/PKCS5Padding decryption logic here
return "decrypted-" + ciphertext;
}
}

.....

@RestController
@RequestMapping("..........")
public class EncryptionController {

private final EncryptionServiceFactory encryptionServiceFactory;

@Autowired
public EncryptionController(EncryptionServiceFactory encryptionServiceFactory) {
this.encryptionServiceFactory = encryptionServiceFactory;
}

@PostMapping("/{dataClassName}/encrypt")
public String encrypt(@PathVariable String dataClassName, @RequestBody String plaintext) {
EncryptionService encryptionService = encryptionServiceFactory.getService(dataClassName);
return encryptionService.encrypt(plaintext);
}

}
Как видите, на основе значения dataClass выбирается служба. Но проблема в том, что у меня исправлены службы Spring Boot. ЕСЛИ мне нужно добавить больше типов классов данных в файл конфигурации yml, мне также нужно добавить службы загрузки Sprign.
Можно ли спроектировать код таким образом, чтобы я мог добавить больше Имена классов данных со значениями подконфигурации без добавления нового кода Java?

Подробнее здесь: https://stackoverflow.com/questions/790 ... le-configs
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»