mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-24 00:05:48 +00:00
스프링 빈
This commit is contained in:
parent
1406333c1d
commit
345fe33c6c
6 changed files with 45 additions and 48 deletions
5
pom.xml
5
pom.xml
|
@ -131,6 +131,11 @@
|
|||
<artifactId>spring-boot-devtools</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -15,8 +15,7 @@
|
|||
*/
|
||||
package org.springframework.samples.petclinic.owner;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.samples.petclinic.visit.VisitRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.validation.BindingResult;
|
||||
|
@ -39,37 +38,14 @@ 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, ApplicationContext applicationContext, VisitRepository visits) {
|
||||
this.owners = clinicService;
|
||||
this.applicationContext = applicationContext;
|
||||
this.visits = visits;
|
||||
}
|
||||
@Autowired
|
||||
private OwnerRepository owners;
|
||||
|
||||
@InitBinder
|
||||
public void setAllowedFields(WebDataBinder dataBinder) {
|
||||
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();
|
||||
|
@ -146,7 +122,7 @@ class OwnerController {
|
|||
* @param ownerId the ID of the owner to display
|
||||
* @return a ModelMap with the model attributes for the view
|
||||
*/
|
||||
@GetMapping("/owners/{ownerId}")
|
||||
/*@GetMapping("/owners/{ownerId}")
|
||||
public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) {
|
||||
ModelAndView mav = new ModelAndView("owners/ownerDetails");
|
||||
Owner owner = this.owners.findById(ownerId);
|
||||
|
@ -155,6 +131,6 @@ class OwnerController {
|
|||
}
|
||||
mav.addObject(owner);
|
||||
return mav;
|
||||
}
|
||||
}*/
|
||||
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -1,16 +1,9 @@
|
|||
package org.springframework.samples.petclinic.sample;
|
||||
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
||||
//@Controller
|
||||
public class SampleController {
|
||||
|
||||
SampleRepository sampleRepository;
|
||||
|
||||
public SampleController(SampleRepository sampleRepository) {
|
||||
this.sampleRepository = sampleRepository;
|
||||
}
|
||||
|
||||
public void doSomething() {
|
||||
sampleRepository.save();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -70,12 +70,12 @@ class OwnerControllerTests {
|
|||
@Autowired
|
||||
ApplicationContext applicationContext;
|
||||
|
||||
@Test
|
||||
/*@Test
|
||||
public void getBean() {
|
||||
applicationContext.getBeanDefinitionNames();
|
||||
System.out.println(applicationContext.getBeanDefinitionNames().length);
|
||||
|
||||
}
|
||||
}*/
|
||||
|
||||
/*@Test
|
||||
public void getBean() {
|
||||
|
|
|
@ -1,16 +1,26 @@
|
|||
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
|
||||
public void testDoSomething() {
|
||||
SampleRepository sampleRepository = new SampleRepository();
|
||||
SampleController sampleController = new SampleController(sampleRepository);
|
||||
sampleController.doSomething();
|
||||
public void testDI() {
|
||||
SampleController bean = applicationContext.getBean(SampleController.class);
|
||||
Assertions.assertThat(bean).isNotNull();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue