스프링 빈

This commit is contained in:
zaien24 2020-08-26 15:54:55 +09:00
parent 1406333c1d
commit 345fe33c6c
6 changed files with 45 additions and 48 deletions

View file

@ -131,6 +131,11 @@
<artifactId>spring-boot-devtools</artifactId> <artifactId>spring-boot-devtools</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>

View file

@ -15,8 +15,7 @@
*/ */
package org.springframework.samples.petclinic.owner; package org.springframework.samples.petclinic.owner;
import org.springframework.context.ApplicationContext; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.visit.VisitRepository;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
@ -39,37 +38,14 @@ class OwnerController {
private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm"; private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm";
private final OwnerRepository owners; @Autowired
private final ApplicationContext applicationContext; private OwnerRepository owners;
private VisitRepository visits;
public OwnerController(OwnerRepository clinicService, ApplicationContext applicationContext, VisitRepository visits) {
this.owners = clinicService;
this.applicationContext = applicationContext;
this.visits = visits;
}
@InitBinder @InitBinder
public void setAllowedFields(WebDataBinder dataBinder) { public void setAllowedFields(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields("id"); 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") @GetMapping("/owners/new")
public String initCreationForm(Map<String, Object> model) { public String initCreationForm(Map<String, Object> model) {
Owner owner = new Owner(); Owner owner = new Owner();
@ -146,7 +122,7 @@ class OwnerController {
* @param ownerId the ID of the owner to display * @param ownerId the ID of the owner to display
* @return a ModelMap with the model attributes for the view * @return a ModelMap with the model attributes for the view
*/ */
@GetMapping("/owners/{ownerId}") /*@GetMapping("/owners/{ownerId}")
public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) { public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) {
ModelAndView mav = new ModelAndView("owners/ownerDetails"); ModelAndView mav = new ModelAndView("owners/ownerDetails");
Owner owner = this.owners.findById(ownerId); Owner owner = this.owners.findById(ownerId);
@ -155,6 +131,6 @@ class OwnerController {
} }
mav.addObject(owner); mav.addObject(owner);
return mav; return mav;
} }*/
} }

View file

@ -0,0 +1,13 @@
package org.springframework.samples.petclinic.sample;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SampleConfig {
@Bean
public SampleController sampleController() {
return new SampleController();
}
}

View file

@ -1,16 +1,9 @@
package org.springframework.samples.petclinic.sample; package org.springframework.samples.petclinic.sample;
import org.springframework.stereotype.Controller;
//@Controller
public class SampleController { public class SampleController {
SampleRepository sampleRepository;
public SampleController(SampleRepository sampleRepository) {
this.sampleRepository = sampleRepository;
}
public void doSomething() {
sampleRepository.save();
}
} }

View file

@ -70,12 +70,12 @@ class OwnerControllerTests {
@Autowired @Autowired
ApplicationContext applicationContext; ApplicationContext applicationContext;
@Test /*@Test
public void getBean() { public void getBean() {
applicationContext.getBeanDefinitionNames(); applicationContext.getBeanDefinitionNames();
System.out.println(applicationContext.getBeanDefinitionNames().length); System.out.println(applicationContext.getBeanDefinitionNames().length);
} }*/
/*@Test /*@Test
public void getBean() { public void getBean() {

View file

@ -1,16 +1,26 @@
package org.springframework.samples.petclinic.sample; package org.springframework.samples.petclinic.sample;
import org.junit.jupiter.api.Test; import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.Assert.*;
class SampleControllerTest { @RunWith(SpringRunner.class)
@SpringBootTest
public class SampleControllerTest {
@Autowired
ApplicationContext applicationContext;
@Test @Test
public void testDoSomething() { public void testDI() {
SampleRepository sampleRepository = new SampleRepository(); SampleController bean = applicationContext.getBean(SampleController.class);
SampleController sampleController = new SampleController(sampleRepository); Assertions.assertThat(bean).isNotNull();
sampleController.doSomething();
} }
} }