Spring Boot 7 DI蜃ヲ逅

DI蜃ヲ逅

繧ス繝シ繧ケ繧ウ繝シ繝峨↓繧ッ繝ゥ繧ケ蜷阪r逶エ謗・險倩ソー縺帙★縺ォ繧、繝ウ繧ケ繧ソ繝ウ繧ケ蛹悶r陦後≧繝励Ο繧ー繝ゥ繝溘Φ繧ー譁ケ豕
Spring DI繧ウ繝ウ繝繝縲榊盾辣ァ

@Service

窶サMyService.java
@Service
public class MyService {
縲public String getTest(){
縲縲return "TEST";
縲}
}

窶サMyRestContoller.java
@RestController
public class MyRestContoller {
@Autowired
縲private MyService service;
MyService繧呈爾縺励※閾ェ蜍輔う繝ウ繧ケ繧ソ繝ウ繧ケ蛹

縲@RequestMapping(value = "/getTest")
縲public String getAll() {
縲縲return service.getTest();
縲}
}

@Component

窶サComponentTestApplication.java
@SpringBootApplication
public class ComponentTestApplication {
縲public static void main(String[] args) {
縲縲SpringApplication.run(ComponentTestApplication.class, "TEST");
縲}
}

窶サMyComponent.java
@Component
public class MyComponent {
縲private List<String> list;

縲public List<String> getList() {
縲縲return this.list;
縲}

@Autowired
縲public MyComponent (ApplicationArguments args) {
縲縲this.list = args.getNonOptionArgs();
縲縲this.listシ啜EST
縲}
}

窶サMyRestContoller.java
@RestController
public class MyRestContoller {
@Autowired
縲private MyComponent component;
MyComponent繧呈爾縺励※閾ェ蜍輔う繝ウ繧ケ繧ソ繝ウ繧ケ蛹
@Autowired縺御サ伜刈縺輔l縺溘さ繝ウ繧ケ繝医Λ繧ッ繧ソ繧貞ョ溯。

縲@RequestMapping(value = "/rest")
縲public String restAll() {
縲縲return component.getList().get(0);
縲}
}

@Configurationシ機Bean

窶サMyBean.java
public class MyBean {
縲public String getTest1(){
縲縲return "TEST1";
縲}
縲public String getTest2(){
縲縲return "TEST2";
縲}
}

窶サMyAppConfig.java
@Configuration
public class MyAppConfig {
@Bean
縲public MyBean getMyBean(){
縲縲return new MyBean();
縲}
}

窶サMyRestContoller.java
@RestController
public class MyRestContoller {
@Autowired
縲private MyBean bean;
MyBean繧呈爾縺励※閾ェ蜍輔う繝ウ繧ケ繧ソ繝ウ繧ケ蛹
縲@RequestMapping(value = "/getTest")
縲public String restAll() {
縲縲return bean.getTest1() + bean.getTest2();
縲}
}

@Repository

Spring Boot 6 繝繝シ繧ソ繝吶シ繧ケ謫堺ス縲榊盾辣ァ

Follow me!