This commit is contained in:
Inohara 2023-02-25 16:00:34 +04:00
parent 01036e8cee
commit 10980a9096
7 changed files with 95 additions and 0 deletions

View File

@ -0,0 +1,21 @@
package com.example.demo.speaker.configuration;
import com.example.demo.speaker.domain.Speaker;
import com.example.demo.speaker.domain.SpeakerEng;
import com.example.demo.speaker.domain.SpeakerRus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SpeakerConfiguration {
@Bean(value = "ru")
public SpeakerRus createRusSpeaker(){
return new SpeakerRus();
}
@Bean(value = "en")
public Speaker createEngSpeaker(){
return new SpeakerEng();
}
}

View File

@ -0,0 +1,20 @@
package com.example.demo.speaker.controller;
import com.example.demo.speaker.service.SpeakerService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SpeakerController {
private final SpeakerService speakerService;
public SpeakerController(SpeakerService speakerService){
this.speakerService = speakerService;
}
@GetMapping("/hello")
public String hello(@RequestParam(defaultValue = "Привет") String name,
@RequestParam(defaultValue = "ru") String lang){
return speakerService.say(name, lang);
}
}

View File

@ -0,0 +1,5 @@
package com.example.demo.speaker.domain;
public interface Speaker {
String say();
}

View File

@ -0,0 +1,11 @@
package com.example.demo.speaker.domain;
import org.springframework.stereotype.Component;
@Component(value = "de")
public class SpeakerDeu implements Speaker{
@Override
public String say(){
return "Hallo";
}
}

View File

@ -0,0 +1,8 @@
package com.example.demo.speaker.domain;
public class SpeakerEng implements Speaker{
@Override
public String say() {
return "Hello";
}
}

View File

@ -0,0 +1,11 @@
package com.example.demo.speaker.domain;
import org.springframework.stereotype.Component;
@Component(value = "ru")
public class SpeakerRus implements Speaker{
@Override
public String say(){
return "Привет";
}
}

View File

@ -0,0 +1,19 @@
package com.example.demo.speaker.service;
import com.example.demo.speaker.domain.Speaker;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
@Service
public class SpeakerService {
private final ApplicationContext applicationContext;
public SpeakerService(ApplicationContext applicationContext){
this.applicationContext = applicationContext;
}
public String say(String name, String lang){
final Speaker speaker = (Speaker) applicationContext.getBean(lang);
return String.format("Hello %s %s!", speaker.say(), name);
}
}