mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-22 15:25:49 +00:00
[스프링 IoC]
[스프링 Ioc 컨테이너]
This commit is contained in:
parent
069ae6b1b6
commit
1406333c1d
5 changed files with 79 additions and 5 deletions
|
@ -15,15 +15,13 @@
|
|||
*/
|
||||
package org.springframework.samples.petclinic.owner;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.samples.petclinic.visit.VisitRepository;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.InitBinder;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
@ -42,11 +40,13 @@ class OwnerController {
|
|||
private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm";
|
||||
|
||||
private final OwnerRepository owners;
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
private VisitRepository visits;
|
||||
|
||||
public OwnerController(OwnerRepository clinicService, VisitRepository visits) {
|
||||
public OwnerController(OwnerRepository clinicService, ApplicationContext applicationContext, VisitRepository visits) {
|
||||
this.owners = clinicService;
|
||||
this.applicationContext = applicationContext;
|
||||
this.visits = visits;
|
||||
}
|
||||
|
||||
|
@ -55,6 +55,21 @@ class OwnerController {
|
|||
dataBinder.setDisallowedFields("id");
|
||||
}
|
||||
|
||||
/*@GetMapping("/bean")
|
||||
@ResponseBody
|
||||
public String bean() {
|
||||
return "bean: " + applicationContext.getBean(OwnerController.class);
|
||||
}*/
|
||||
|
||||
@GetMapping("/bean")
|
||||
@ResponseBody
|
||||
public String bean() {
|
||||
return "bean: " + applicationContext.getBean(OwnerRepository.class) + "\n"
|
||||
+ "owners: " + this.owners;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@GetMapping("/owners/new")
|
||||
public String initCreationForm(Map<String, Object> model) {
|
||||
Owner owner = new Owner();
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
package org.springframework.samples.petclinic.sample;
|
||||
|
||||
|
||||
public class SampleController {
|
||||
|
||||
SampleRepository sampleRepository;
|
||||
|
||||
public SampleController(SampleRepository sampleRepository) {
|
||||
this.sampleRepository = sampleRepository;
|
||||
}
|
||||
|
||||
public void doSomething() {
|
||||
sampleRepository.save();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package org.springframework.samples.petclinic.sample;
|
||||
|
||||
public class SampleRepository {
|
||||
|
||||
public void save() {
|
||||
}
|
||||
}
|
|
@ -27,11 +27,14 @@ import org.junit.jupiter.api.BeforeEach;
|
|||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.context.assertj.ApplicationContextAssert;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.samples.petclinic.visit.Visit;
|
||||
import org.springframework.samples.petclinic.visit.VisitRepository;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.hamcrest.Matchers.empty;
|
||||
import static org.hamcrest.Matchers.hasProperty;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
@ -64,6 +67,23 @@ class OwnerControllerTests {
|
|||
|
||||
private Owner george;
|
||||
|
||||
@Autowired
|
||||
ApplicationContext applicationContext;
|
||||
|
||||
@Test
|
||||
public void getBean() {
|
||||
applicationContext.getBeanDefinitionNames();
|
||||
System.out.println(applicationContext.getBeanDefinitionNames().length);
|
||||
|
||||
}
|
||||
|
||||
/*@Test
|
||||
public void getBean() {
|
||||
OwnerController bean = applicationContext.getBean(OwnerController.class);
|
||||
assertThat(bean).isNotNull();
|
||||
}*/
|
||||
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
george = new Owner();
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
package org.springframework.samples.petclinic.sample;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class SampleControllerTest {
|
||||
|
||||
@Test
|
||||
public void testDoSomething() {
|
||||
SampleRepository sampleRepository = new SampleRepository();
|
||||
SampleController sampleController = new SampleController(sampleRepository);
|
||||
sampleController.doSomething();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue