å ±æã©ã¤ãã©ãªã管çããããã« Sonatype ã® Nexus Repository Manager OSS ã使ç¨ãã ( ãã®ï¼ï¼ )( Spring Framework ã«ä¾åããã©ã¤ãã©ãªãä½æããï¼ )
æ¦è¦
- ä»åã®æé ã§ç¢ºèªã§ããã®ã¯ä»¥ä¸ã®å
容ã§ãã
- ksbysample-library-depend-spring ãå°å ¥ãã¦ãããã©ã«ãã§ã¯åä½ãããksbysample.library.request-and-response-logger.enabled=true ãè¨å®ããæã ãåä½ããããã«å¤æ´ãã¾ãã
åç §ãããµã¤ãã»æ¸ç±
ç®æ¬¡
- RequestAndResponseLoggerAutoConfiguration ã¯ã©ã¹ã§ ksbysample.library.request-and-response-logger.enabled=true ãè¨å®ããã¦ããªããã° Bean ãçæããªãããã«ãã
- Nexus ã«ç»é²ãã
- ksbysample-webapp-demo ããã¸ã§ã¯ãã§åä½ã確èªãã
- 次åã¯ããã
æé
RequestAndResponseLoggerAutoConfiguration ã¯ã©ã¹ã§ ksbysample.library.request-and-response-logger.enabled=true ãè¨å®ããã¦ããªããã° Bean ãçæããªãããã«ãã
-
ksbysample-library-depend-spring ããã¸ã§ã¯ããéãã¾ãã
-
ãã¼ã¸ã§ã³ã 1.1.0-RELEASE ã«å¤æ´ãã¾ããbuild.gradle ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
src/main/java/ksbysample/library/dependspring/config ã®ä¸ã® RequestAndResponseLoggerAutoConfiguration.java ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
ãã¹ããã¾ããsrc/test/java/ksbysample/library/dependspring/config ã®ä¸ã® RequestAndResponseLoggerAutoConfigurationTest.java ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
ãã¹ããå®è¡ãã¾ããRequestAndResponseLoggerAutoConfigurationTest ã¯ã©ã¹ã®ã¯ã©ã¹åã®å·¦å´ã«è¡¨ç¤ºããã¦ããã¢ã¤ã³ã³ãã¯ãªãã¯ãã¦ã³ã³ããã¹ãã¡ãã¥ã¼ã表示å¾ãRun 'RequestAndResponseLoggerAutoCo...'ããé¸æãã¾ãã
ãã¹ãã¯å ¨ã¦æåããããã©ã«ãç¶æ ã§ã¯ãã°ã¯åºåãããã
ksbysample.library.request-and-response-logger.enabled=true
ãå®ç¾©ããã°ãã°ãåºåããããã¨ã確èªã§ãã¾ãã
Nexus ã«ç»é²ãã
clean ã¿ã¹ã¯å®è¡ â Rebuild Project å®è¡ãããå¾ build ã¿ã¹ã¯ãå®è¡ãã¦ãã¨ã©ã¼ãåºãªããã¨ã確èªãã¾ãã
Gradle projects View ãã uploadArchives ã¿ã¹ã¯ãå®è¡ãã¾ããBUILD SUCCESSFUL ã表示ããã¦æ£å¸¸ã« Nexus ã«ç»é²ããã¾ããã
Nexus ã®ç®¡çç»é¢ãè¦ãã¨ãç»é²ããã¦ãããã¨ã確èªã§ãã¾ãã
ksbysample-webapp-demo ããã¸ã§ã¯ãã§åä½ã確èªãã
ksbysample-library-depend-spring ããã¸ã§ã¯ããéã㦠ksbysample-webapp-demo ããã¸ã§ã¯ããéãã¾ãã
build.gradle ã ãªã³ã¯å ã®ãã®ï¼ã®å 容 ã«å¤æ´ãã¾ããå¤æ´å¾ãGradle projects View ã®å·¦ä¸ã«ãããRefresh all Gradle projectsããã¿ã³ãã¯ãªãã¯ãã¦åæ ãã¾ãã
bootRun ã¿ã¹ã¯ãå®è¡ã㦠Tomcat ãèµ·åããå¾ãhttp://localhost:8080/sample ã«ã¢ã¯ã»ã¹ãã¾ããã¾ã
ksbysample.library.request-and-response-logger.enabled=true
ãè¨å®ãã¦ããªãã®ã§ãrequest, response ã®ãã°ãåºåããã¾ãããTomcat ãåæ¢ãã¾ãã
build.gradle ã ãªã³ã¯å ã®ãã®ï¼ã®å 容 ã«å¤æ´ãã¾ãã
å度 bootRun ã¿ã¹ã¯ãå®è¡ã㦠Tomcat ãèµ·åããå¾ãhttp://localhost:8080/sample ã«ã¢ã¯ã»ã¹ãã¾ããä»åº¦ã¯ request, response ã®ãã°ãåºåããã¾ãã
Tomcat ãåæ¢ãã¾ãã
åä½ç¢ºèªãå®äºããã®ã§ãcommit ãã¾ããã¾ã feature/3-issue ãã©ã³ãã develop, master ãã©ã³ãã¸ãã¼ã¸ãã¦ãIssue #3 ãã¯ãã¼ãºãã¾ãã
次åã¯ããã
- Nexus 3 ã®æ£å¼ç ( 3.1 以é ) ã®ã¤ã³ã¹ãã¼ã«ã¯ 3.0.x ããã®ãã¼ã¸ã§ã³ã¢ããã¨ãã¦è¨è¿°ãã¦ããã®ã§ãWindowsçNexus ã®ã¤ã³ã¹ãã¼ã«æ¹æ³ãã¾ã¨ãç´ãã¾ãã
- VirtualBox+Vagrant ãã¤ã³ã¹ãã¼ã«ã㦠Linux ã®ä»®æ³ãµã¼ããæ§ç¯ããUnix çã® Nexus Repository Manager OSS ãã¤ã³ã¹ãã¼ã«ãã¦ä½¿ç¨ãã¦ã¿ã¾ãã
ã½ã¼ã¹ã³ã¼ã
ksbysample-library-depend-spring/build.gradle
group 'ksbysample.library' version '1.1.0-RELEASE'
- version ã®å¤ã
1.0.0-RELEASE
â1.1.0-RELEASE
ã«å¤æ´ãã¾ãã
RequestAndResponseLoggerAutoConfiguration.java
package ksbysample.library.dependspring.config; import ksbysample.library.dependspring.intercepter.RequestAndResponseLogger; import org.apache.commons.lang3.StringUtils; import org.springframework.aop.Advisor; import org.springframework.aop.aspectj.AspectJExpressionPointcut; import org.springframework.aop.support.DefaultPointcutAdvisor; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RequestAndResponseLoggerAutoConfiguration { private static final String REQUESTMAPPING_EXPRESSION = "@annotation(org.springframework.web.bind.annotation.RequestMapping)"; @Value("${ksbysample.library.request-and-response-logger.execution:}") private String execution; @Bean @ConditionalOnWebApplication @ConditionalOnProperty(value = {"ksbysample.library.request-and-response-logger.enabled"}, havingValue = "true") public Advisor requestAndResponseLoggerAdvisor() { AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); String expression = StringUtils.EMPTY; if (StringUtils.isNotBlank(this.execution)) { expression = this.execution + " && "; } expression += REQUESTMAPPING_EXPRESSION; pointcut.setExpression(expression); return new DefaultPointcutAdvisor(pointcut, new RequestAndResponseLogger()); } }
@ConditionalOnProperty(value = {"ksbysample.library.request-and-response-logger.enabled"}, havingValue = "true")
ã追å ãã¾ãã
RequestAndResponseLoggerAutoConfigurationTest.java
package ksbysample.library.dependspring.config; import ksbysample.library.dependspring.Application; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.experimental.runners.Enclosed; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.OutputCapture; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; import static org.assertj.core.api.Assertions.assertThat; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @RunWith(Enclosed.class) public class RequestAndResponseLoggerAutoConfigurationTest { public static abstract class TestBase { public MockMvc mvc; @Autowired private WebApplicationContext context; @Rule public OutputCapture capture = new OutputCapture(); @Before public void setUp() throws Exception { this.mvc = MockMvcBuilders.webAppContextSetup(this.context) .build(); } } @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration public static class ããã©ã«ãã®è¨å®ã§ã®ãã¹ã extends TestBase { @Test public void ãã°ãåºåãããªã() throws Exception { mvc.perform(get("/test")) .andExpect(status().isOk()); assertThat(capture.toString()).doesNotContain("[req][info ] REQUEST_URI = /test"); assertThat(capture.toString()).doesNotContain("[res][info ] RESPONSE_STATUS = 200"); } } @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration @TestPropertySource(properties = { "ksbysample.library.request-and-response-logger.enabled=true" }) public static class enabledããããã£è¨å®æã®ãã¹ã extends TestBase { @Test public void ãã°ãåºåããã() throws Exception { mvc.perform(get("/test")) .andExpect(status().isOk()); assertThat(capture.toString()).contains("[req][info ] REQUEST_URI = /test"); assertThat(capture.toString()).contains("[res][info ] RESPONSE_STATUS = 200"); } } @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration @TestPropertySource(properties = { "ksbysample.library.request-and-response-logger.enabled=true" , "ksbysample.library.request-and-response-logger.execution=execution(* ksbysample.library.dependspring..*.*(..))" }) public static class executionããããã£è¨å®æã®ãã¹ã_åºåãããè¨å® extends TestBase { @Test public void ãã°ãåºåããã() throws Exception { mvc.perform(get("/test")) .andExpect(status().isOk()); assertThat(capture.toString()).contains("[req][info ] REQUEST_URI = /test"); assertThat(capture.toString()).contains("[res][info ] RESPONSE_STATUS = 200"); } } @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration @TestPropertySource(properties = { "ksbysample.library.request-and-response-logger.enabled=true" , "ksbysample.library.request-and-response-logger.execution=execution(* ksbysample.webapp.lending.web..*.*(..))" }) public static class executionããããã£è¨å®æã®ãã¹ã_åºåãããªãè¨å® extends TestBase { @Test public void ãã°ãåºåãããªã() throws Exception { mvc.perform(get("/test")) .andExpect(status().isOk()); assertThat(capture.toString()).doesNotContain("[req][info ] REQUEST_URI = /test"); assertThat(capture.toString()).doesNotContain("[res][info ] RESPONSE_STATUS = 200"); } } }
- åã¯ã©ã¹ã§åãå 容ãå®ç¾©ããã¦ããé¨åã TestBase ã¯ã©ã¹ãä½æãã¦å ±éåãã¾ããabstract class ã«ãã¦ãã¹ã対象å¤ã«ãªãããã«ãã¾ãã
- ãããã©ã«ãã®è¨å®ã§ã®ãã¹ãããã¹ãã¯ã©ã¹ã®ã¡ã½ããåã
ãã°ãåºåããã
âãã°ãåºåãããªã
ã¸å¤æ´ããã¡ã½ããå ã§ä½¿ç¨ãã¦ãã assert ç¨ã¡ã½ãããcontains
âdoesNotContain
ã¸å¤æ´ãã¾ãã ksbysample.library.request-and-response-logger.enabled=true
ãå®ç¾©ããæã«ãã°ãåºåããããã¨ã確èªããããã®ãenabledããããã£è¨å®æã®ãã¹ãããã¹ãã¯ã©ã¹ãä½æãã¾ãã- ãexecutionããããã£è¨å®æã®ãã¹ãåºåãããè¨å®ããexecutionããããã£è¨å®æã®ãã¹ãåºåãããªãè¨å®ããã¹ãã¯ã©ã¹ã® @TestPropertySource ã¢ããã¼ã·ã§ã³ã«
ksbysample.library.request-and-response-logger.enabled=true
ã追å ãã¾ãã
ksbysample-webapp-demo/build.gradle
â ãã®ï¼
dependencies { compile('org.springframework.boot:spring-boot-starter-web') testCompile('org.springframework.boot:spring-boot-starter-test') compile('ksbysample.library:ksbysample-library-simpleutils:1.0-RELEASE') compile('ksbysample.library:ksbysample-library-depend-nospring:1.1-RELEASE') compile('ksbysample.library:ksbysample-library-depend-spring:1.1.0-RELEASE') }
ksbysample.library:ksbysample-library-depend-spring
ã®ãã¼ã¸ã§ã³çªå·ã1.0.0-RELEASE
â1.1.0-RELEASE
ã«å¤æ´ãã¾ãã
â ãã®ï¼
dependencies { .......... } bootRun { jvmArgs = ['-Dksbysample.library.request-and-response-logger.enabled=true'] }
å±¥æ´
2016/12/29
åççºè¡ã