Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 |
Tags
- JUnit
- spring-mvc
- 코드 리뷰
- Spring Data JPA
- JPA
- spring boot
- IntelliJ
- spring bean
- Spring Data REST
- batch
- 톰캣
- Spring Batch
- docker
- spring jpa
- 세미나
- spring-webmvc
- ORM
- Data REST
- spring camp
- tomcat
- REST API
- SuperTypeToken
- JPA mapping
- spring-webmvc #코드읽기
- static inner class
- spring boot tomcat
- ApplicationPidFileWriter
- spring batch 코드
- spring pid
- Spring
Archives
- Today
- Total
woniper
Autowired Generics (Generics Type 주입하기) 본문
- 전체 예제 : https://github.com/woniper/spring-example/tree/master/spring-boot-custom-bean-name-generator
- 좀더 훌륭한 문서 : https://spring.io/blog/2013/12/03/spring-framework-4-0-and-java-generics
스프링 4.0 부터 자바 제네릭 타입을 주입받을 수 있다. 4.0 미만 버전도 가능은 했지만 번거로운 추가 작업이 필요했다.
예를들어
Notification 인터페이스를 구현한 3개의 구현체가 있다고 예를 들자.
1 2 | @Autowird private List<Notification> notifications; | cs |
4.0 이상부터는 Notification List Type으로 주입이 가능해졌다. 매우 편리하다. 위 예제는 Notification을 예로 들었는데, 3개의 구현체를 List로 주입받아 반복문을 통해 3개의 알림을 보내면 코드를 좀 더 깔끔하게 사용이 가능하다.
1 2 3 4 | @Test public void testNotificationList() { notifications.forEach(notification -> notification.send("list", "contents")); } | cs |
이런식으로 말이다.
자바에서 제공하는 List Collection 뿐 아니라, Map, Set Type 모두 가능하다.
'Spring' 카테고리의 다른 글
Spring IoC Container를 까보자 #Bean을 어떻게 반환할까? (3) | 2017.02.08 |
---|---|
Spring IoC Container를 까보자 #Bean 등록은 어떻게 될까? (3) | 2017.02.05 |
BeanNameGenerator로 Restful API 버전 관리 (0) | 2016.10.23 |
Stopwatch Class는 무슨일을 할까? 그리고 메소드 네이밍과 책임. (4) | 2016.08.04 |
SpringBoot Actuator BeansEndPoint Class (0) | 2016.07.28 |
Comments