å ±æã©ã¤ãã©ãªã管çããããã« Sonatype ã® Nexus Repository Manager OSS ã使ç¨ãã ( ãã®ï¼ï¼ )( Spring Framework ã«ä¾åããã©ã¤ãã©ãªãä½æããï¼ )
æ¦è¦
- ä»åã®æé ã§ç¢ºèªã§ããã®ã¯ä»¥ä¸ã®å
容ã§ãã
- ksbysample-webapp-lending ãã RequestAndResponseLogger.java ãæã£ã¦ã㦠auto-configuration é¢é£ã®å®è£ ã¾ã§é²ãã¾ãã
åç §ãããµã¤ãã»æ¸ç±
-
Pro Spring Boot
- ä½è : Felipe Gutierrez
- åºç社/ã¡ã¼ã«ã¼: Apress
- çºå£²æ¥: 2016/05/21
- ã¡ãã£ã¢: ãã¼ãã¼ããã¯
- ãã®ååãå«ãããã°ãè¦ã
- auto-configuration ããã¸ã§ã¯ãã®ãµã³ãã«ã¨ã㦠Chapter 14 ã®ãThe journal-spring-boot-autoconfigure Projectããåèã«ãã¾ããã
Spring Framework Reference Documentation - 12. Spring AOP APIs - 12.3.2 Advice types in Spring
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop-api.html#aop-api-advice-types- MethodInterceptor ã¤ã³ã¿ã¼ãã§ã¼ã¹ãå®è£
ãããã¨ãã㨠import åè£ã«
org.aopalliance.intercept.MethodInterceptor
ã¨org.springframework.cglib.proxy.MethodInterceptor
ã®ï¼ã¤ãåºã¦æ¥ãã®ã§ãããã©ã¡ãã使ç¨ããã°ãããåãããªãã£ãã®ã§ããã®ãã¼ã¸ãåç §ãã¾ãããinvoke ã¡ã½ãããæ¸ããã¦ããã®ã§org.aopalliance.intercept.MethodInterceptor
ã使ç¨ãã¾ãã
- MethodInterceptor ã¤ã³ã¿ã¼ãã§ã¼ã¹ãå®è£
ãããã¨ãã㨠import åè£ã«
Spring Boot Reference Guide - 43. Creating your own auto-configuration
http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-auto-configuration.htmlSpring Boot Reference Guide - 64. Spring Boot Gradle plugin - 64.6 Repackage configuration
http://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-gradle-plugin.html#build-tool-plugins-gradle-repackage-configuration
ç®æ¬¡
- ksbysample-webapp-lending ãã RequestAndResponseLogger.java ãæã£ã¦ãã
- AutoConfiguration ã¯ã©ã¹ãä½æãã
- META-INF/spring.factories ãä½æãã
- build ãã¦ã¿ã
- ksbysample-library-depend-spring-1.0.0-RELEASE.jar ã®ä¸ãè¦ã¦ã¿ã
æé
ksbysample-webapp-lending ãã RequestAndResponseLogger.java ãæã£ã¦ãã
src/main/java ã®ä¸ã« ksbysample.library.dependspring.intercepter ããã±ã¼ã¸ãä½æãã¾ãã
ksbysample-webapp-lending ã® RequestAndResponseLogger.java ãã³ãã¼ã㦠src/main/java/ksbysample/library/dependspring/intercepter ã®ä¸ã«ãã¼ã¹ããã¾ãã
使ç¨ããã©ã¤ãã©ãªããªãã®ã§ build.gradle ã ãªã³ã¯å ã®ãã®ï¼ã®å 容 ã«å¤æ´ãã¾ããå¤æ´å¾ãGradle projects View ã®å·¦ä¸ã«ãããRefresh all Gradle projectsããã¿ã³ãã¯ãªãã¯ãã¦åæ ãã¾ãã
RequestAndResponseLogger.java ã®ä»¥ä¸ã®ç¹ãå¤æ´ãã¾ãããªã³ã¯å ã®å 容 ã«ãªãã¾ãã
- ã¯ã©ã¹ã«ä»ä¸ããã¦ãã @Aspect, @Component ã¢ããã¼ã·ã§ã³ã¯åé¤ãã¾ãã
- MethodInterceptor ã¤ã³ã¿ã¼ãã§ã¼ã¹ãå®è£ ããinvoke ã¡ã½ããããªã¼ãã¼ã©ã¤ããã¾ãã
- logginRequestAndResponse ã¡ã½ããã®ä¸èº«ã¯ MethodInterceptor ã¤ã³ã¿ã¼ãã§ã¼ã¹ã® invoke ã¡ã½ããã¸ç§»åãã¾ãã
AutoConfiguration ã¯ã©ã¹ãä½æãã
Spring Boot ã® spring-boot-autoconfigure ããã¸ã§ã¯ã ãè¦ã㨠AutoConfiguration ç¨ã® Bean ãä½æããã¯ã©ã¹ã¯ã¯ã©ã¹åã®æ«å°¾ã« AutoConfiguration
ãä»ãããã¦ãããããªã®ã§ãRequestAndResponseLoggerAutoConfiguration.java ãä½æãã¾ãã
src/main/java/ksbysample/library/dependspring ã®ä¸ã« config ããã±ã¼ã¸ãä½æãã¾ãã
src/main/java/ksbysample/library/dependspring/config ã®ä¸ã« RequestAndResponseLoggerAutoConfiguration.java ãä½æãããªã³ã¯å ã®å 容 ãè¨è¿°ãã¾ãã
META-INF/spring.factories ãä½æãã
src/main/resources ã®ä¸ã« META-INF ãã£ã¬ã¯ããªãä½æãã¾ãã
src/main/resources/META-INF ã®ä¸ã« spring.factories ãä½æãããªã³ã¯å ã®å 容 ãè¨è¿°ãã¾ãã
build ãã¦ã¿ã
clean ã¿ã¹ã¯å®è¡ â Rebuild Project å®è¡ãããå¾ãbuild ã¿ã¹ã¯ãå®è¡ãã¾ãã
"FAILURE" ã®æåãåºåãã失æãã¾ããã
IntelliJ IDEA ã®ã³ã³ã½ã¼ã«ã«åºåãããå 容ã§ã¯ã¨ã©ã¼ã®åå ãåãããªãã®ã§ãã³ãã³ãããã³ãããã
--stacktrace
ãªãã·ã§ã³ãä»ã㦠build ã¿ã¹ã¯ãå®è¡ãã¦ã¿ã¾ããåºåããããã°ãè¦ãæãã§ã¯ã以ä¸ã®åå ã®ããã§ãã
- bootRepackage ã¿ã¹ã¯ã§å¤±æãã¦ããã
- main ã¯ã©ã¹ããªããã失æãã¦ããã
main ã¯ã©ã¹ã¯ä½æããªãã®ã§ã©ãããã°ããã調ã¹ã¦ã¿ãã¨ããã64.6 Repackage configuration ã«
enabled
ã¨ããè¨å®é ç®ãè¦ã¤ãã¾ãããããã false ã«è¨å®ããã° bootRepackage ã¿ã¹ã¯ã¯å®è¡ãããªããªãããã§ããbuild.gradle ã ãªã³ã¯å ã®ãã®ï¼ã®å 容 ã«å¤æ´ãã¾ããå¤æ´å¾ãGradle projects View ã®å·¦ä¸ã«ãããRefresh all Gradle projectsããã¿ã³ãã¯ãªãã¯ãã¦åæ ãã¾ãã
å度 clean ã¿ã¹ã¯å®è¡ â Rebuild Project å®è¡ãããå¾ãbuild ã¿ã¹ã¯ãå®è¡ãã¾ããä»åº¦ã¯ "BUILD SUCCESSFUL" ãåºåããã¦æåãã¾ããã
jar ãã¡ã¤ã«ãä½æããã¦ãããã¨ã確èªã§ãã¾ããã
ksbysample-library-depend-spring-1.0.0-RELEASE.jar ã®ä¸ãè¦ã¦ã¿ã
ksbysample-library-depend-spring-1.0.0-RELEASE.jar ã解åãã¦ã¿ãã¨ä»¥ä¸ã®æ§æã§ããã
ksbysample-library-depend-spring-1.0.0-RELEASE.jar â generated â ksbysample â â library â â dependspring â â config â â â RequestAndResponseLoggerAutoConfiguration.class â â intercepter â â RequestAndResponseLogger.class â META-INF â â MANIFEST.MF â â spring.factories â .gitkeep
ããã¾ã§ã®å 容ã commit ãã¾ãã
ã½ã¼ã¹ã³ã¼ã
ksbysample-library-depend-spring/build.gradle
â ãã®ï¼
dependencies { // dependency-management-plugin ã«ãããã¼ã¸ã§ã³çªå·ãèªåã§è¨å®ããããã® // Appendix A. Dependency versions ( http://docs.spring.io/platform/docs/current/reference/htmlsingle/#appendix-dependency-versions ) åç § compile("org.springframework.boot:spring-boot-autoconfigure") compile("org.springframework.boot:spring-boot-starter-web") compile("org.aspectj:aspectjweaver") testCompile("org.springframework.boot:spring-boot-starter-test") // dependency-management-plugin ã«ãããã¼ã¸ã§ã³çªå·ãèªåã§è¨å®ãããªããã®ããããã¯ææ°ãã¼ã¸ã§ã³ãæå®ããããã® compile("org.apache.commons:commons-lang3:3.5") compile("com.google.guava:guava:20.0") testCompile("org.assertj:assertj-core:3.6.1") }
- 以ä¸ã®è¨è¿°ã追å ãã¾ãã
- ãã¨ã§ãã¹ããæ¸ãã®ã§ AssertJ ã追å ãã¦ããã¾ãã
â ãã®ï¼
dependencyManagement { imports { mavenBom 'io.spring.platform:platform-bom:2.0.5.RELEASE' } } bootRepackage { enabled = false } dependencies { .......... }
bootRepackage { enabled = false }
ã追å ãã¾ãã
RequestAndResponseLogger.java
package ksbysample.library.dependspring.intercepter; import com.google.common.collect.Iterators; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Arrays; import java.util.Map; import java.util.stream.StreamSupport; public class RequestAndResponseLogger implements MethodInterceptor { private final Logger logger = LoggerFactory.getLogger(this.getClass()); private static final String LOG_REQUEST_INFO = "[req][info ] "; private static final String LOG_REQUEST_HEADER = "[req][header] "; private static final String LOG_REQUEST_COOKIE = "[req][cookie] "; private static final String LOG_REQUEST_PARAMETER = "[req][param ] "; private static final String LOG_RESPONSE_INFO = "[res][info ] "; private static final String LOG_RESPONSE_HEADER = "[res][header] "; @Override public Object invoke(MethodInvocation invocation) throws Throwable { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest(); loggingRequest(request); Object ret = invocation.proceed(); HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse(); loggingResponse(response); return ret; } private void loggingRequest(HttpServletRequest request) { loggingRequestInfo(request); loggingRequestHeaders(request); loggingRequestCookies(request); loggingRequestParameters(request); } ..........
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.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 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()); } }
- ããã©ã«ãã§ã¯ @RequestMapping ã¢ããã¼ã·ã§ã³ãä»ä¸ãããå ¨ã¦ã®ã¡ã½ããã« RequestAndResponseLogger ã® invoke ã¡ã½ããã®å¦çã追å ãã¾ãã
- ãã ãé©ç¨ããããã±ã¼ã¸ãå¶éã§ãããã application.properties ã«
ksbysample.library.request-and-response-logger.execution=execution(* ksbysample.webapp.lending.web..*.*(..))
ã®ããã«è¨å®ã追å ããã°åæ ãããããã«ãã¾ãã
META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ ksbysample.library.dependspring.config.RequestAndResponseLoggerAutoConfiguration
å±¥æ´
2016/12/25
åççºè¡ã