IoC
This commit is contained in:
parent
01036e8cee
commit
10980a9096
@ -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();
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package com.example.demo.speaker.domain;
|
||||
|
||||
public interface Speaker {
|
||||
String say();
|
||||
}
|
@ -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";
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.example.demo.speaker.domain;
|
||||
|
||||
public class SpeakerEng implements Speaker{
|
||||
@Override
|
||||
public String say() {
|
||||
return "Hello";
|
||||
}
|
||||
}
|
@ -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 "Привет";
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user