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
- spring pid
- IntelliJ
- JPA mapping
- spring batch 코드
- REST API
- docker
- tomcat
- Spring Data REST
- SuperTypeToken
- Spring Data JPA
- spring jpa
- Spring Batch
- 세미나
- batch
- Spring
- spring-webmvc #코드읽기
- spring bean
- spring boot tomcat
- spring-mvc
- spring boot
- spring-webmvc
- Data REST
- ApplicationPidFileWriter
- spring camp
- ORM
- 코드 리뷰
- JPA
- 톰캣
- JUnit
- static inner class
Archives
- Today
- Total
woniper
Spring *Utils Classes 배끼기 본문
Spring Tips: The Spring Framework *Utils Classes 를 보고 정리하면 좋겠다고 생각했다.
전체 예제
DemoClass
@Data
@Slf4j
@AllArgsConstructor
@NoArgsConstructor
public class DemoClass {
private List<Map<String, Object>> list = new ArrayList<>();
@PostConstruct
public void begin() {
log.info("begin");
}
}
org.springframework.util.Assert
@Test
public void assertIsNull() throws Exception {
demoClass.setList(null);
Assert.isNull(demoClass.getList(), "the list can't be null");
}
@Test
public void assertIsTrue() throws Exception {
List<Map<String, Object>> list = new ArrayList<>();
Map<String, Object> map = new HashMap<>();
map.put("test", new Object());
list.add(map);
demoClass.setList(list);
Assert.isTrue(list.size() == 1, "list size is 1");
}
Assert 클래스는 간단한 validation 체크를 한다. validation 체크에 실패하면 IllegaArgumentException을 throw 한다.
org.springframework.beans.BeanUtils
@Test
public void testBeansUtils() throws Exception {
PropertyDescriptor[] propertyDescriptors = BeanUtils.getPropertyDescriptors(DemoClass.class);
// class
PropertyDescriptor classProperty = propertyDescriptors[0];
assertThat(classProperty.getName()).isEqualTo("class");
assertThat(classProperty.getReadMethod().getName()).isEqualTo("getClass");
// list
PropertyDescriptor listProperty = propertyDescriptors[1];
assertThat(listProperty.getName()).isEqualTo("list");
assertThat(listProperty.getReadMethod().getName()).isEqualTo("getList");
}
bean으로 등록된 DemoClass의 property 정보를 알 수 있다.
DemoClass에서 접근 가능한 getter method.
- bean instance
- bean 속성 검사
- bean 복사
org.springframework.util.ClassUtil
@Test
public void testClassUtils() throws Exception {
Constructor<DemoClass> demoClassConstructor = ClassUtils.getConstructorIfAvailable(DemoClass.class);
// DemoClass Constructor Info
assertThat(demoClassConstructor.toString()).isEqualTo("public net.woniper.spring.utils.DemoClass()");
// DemoClass newInstance Constructor Info
DemoClass demoClass = demoClassConstructor.newInstance();
assertThat(demoClass.toString()).isEqualTo("DemoClass(list=[])");
}
org.springframework.util.SystemPropertyUtils
@Test
public void testSystemPropertyUtils() throws Exception {
// home directory
String userHome = SystemPropertyUtils.resolvePlaceholders("${user.home}");
// resolved home directory
String userHomeText = SystemPropertyUtils.resolvePlaceholders("my home directory is ${user.home}");
assertThat(userHomeText).isEqualTo("my home directory is " + userHome);
}
현재 사용하는 system(local) 정보를 알 수 있다.
org.springframework.web.bind.ServletRequestUtils, org.springframework.web.util.WebUtils
@Test
public void testServletRequestUtils() throws Exception {
HttpServletRequest mockServletRequest = mock(HttpServletRequest.class);
// ServletRequestUtils
int age = ServletRequestUtils.getIntParameter(mockServletRequest, "age", -1);
assertThat(age).isEqualTo(-1);
// WebUtils
ServletContext mockServletContext = mock(ServletContext.class);
when(mockServletContext.getAttribute(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE))
.thenReturn(new File("/mock/directory"));
when(mockServletRequest.getServletContext())
.thenReturn(mockServletContext);
File tempDir = WebUtils.getTempDir(mockServletRequest.getServletContext());
assertThat(tempDir.getAbsolutePath()).isEqualTo("/mock/directory");
}
javax.servlet.http.HttpServletRequest를 이용해 Web 환경에서 사용할 수 있는 Utils.
org.springframework.aop.support.AopUtils
@Test
public void testAopUtils() throws Exception {
assertThat(AopUtils.getTargetClass(demoClass).toString()).isEqualTo("class net.woniper.spring.utils.DemoClass");
assertThat(AopUtils.isAopProxy(demoClass)).isTrue();
assertThat(AopUtils.isCglibProxy(demoClass)).isTrue();
assertThat(AopUtils.isJdkDynamicProxy(demoClass)).isFalse();
}
Aop 정보를 알 수 있다.
org.springframework.util.ReflectionUtils
@Test
public void testReflectionUtils() throws Exception {
ReflectionUtils.doWithFields(DemoClass.class, System.out::print);
ReflectionUtils.doWithMethods(DemoClass.class, System.out::print);
Field list = ReflectionUtils.findField(DemoClass.class, "list");
assertThat(list.toString())
.isEqualTo("private java.util.List net.woniper.spring.utils.DemoClass.list");
assertThat(ResolvableType.forField(list).toString())
.isEqualTo("java.util.List<java.util.Map<java.lang.String, java.lang.Object>>");
}
ReflectionUtils를 이용해 java 기능 중 하나인 Reflection을 쉽게 사용할 수 있다.
'Spring' 카테고리의 다른 글
Spring Data REST #2 동작 원리 (0) | 2017.05.09 |
---|---|
Spring Data REST #1 Introduction (0) | 2017.05.09 |
prototype bean은 정말 항상 새로운 객체를 반환할까? (0) | 2017.02.12 |
Spring IoC Container를 까보자 #BeanFactory와 ApplicationContext (1) | 2017.02.09 |
Spring IoC Container를 까보자 #Bean을 어떻게 반환할까? (3) | 2017.02.08 |
Comments