Spring Boot + Spring Integration ã§ãããã試ãã¦ã¿ã ( ãã®ï¼ )( ç£è¦ãã¦ãããã£ã¬ã¯ããªã«ç½®ããã Excel ãã¡ã¤ã«ã®ãã¼ã¿ã DB ã«ç»é²ãã â å¦çãçµãã£ãã Excel ãã¡ã¤ã«ãåé¤ï¼ç§»åãã )
æ¦è¦
è¨äºä¸è¦§ã¯ãã¡ãã§ãã
- Spring Boot + Spring Integration ã§ãããã試ãã¦ã¿ã ( ãã®ï¼ )( ãã£ã¬ã¯ããªãç£è¦ãã¦ãã¡ã¤ã«ãç½®ããããå¦çâåé¤ï¼ç§»åãã常é§åã¢ããªã±ã¼ã·ã§ã³ãä½æãã ) ã§ä½æãã常é§åã¢ããªã±ã¼ã·ã§ã³ã«ä»¥ä¸ã®æ©è½ã追å ãã¦ã¿ã¾ãã
- in ãã£ã¬ã¯ããªã« Excel ãã¡ã¤ã«ãã¢ãããã¼ããã¾ãã
- Service Activator ã®åã« Filter ã追å ããæ¡å¼µåã .xlsx 以å¤ã®ãã¡ã¤ã«ã¯ã¨ã©ã¼ã«ãªãããã«ãã¾ã ( ãã¡ã¤ã«ã error ãã£ã¬ã¯ããªã«ç§»åãã¾ã )ã
- Service Activator 㧠Excel ã«è¨è¼ããã¦ãããã¼ã¿ããã¼ãã«ã«ç»é²ãã¾ãã
- å¦çãæ£å¸¸çµäºããå ´åã«ã¯ Excel ãã¡ã¤ã«ãåé¤ããç°å¸¸çµäºããå ´å ( ä¾å¤ã throw ãããå ´å ) ã«ã¯ error ãã£ã¬ã¯ããªã«ç§»åãã¾ãã
- ãã¼ã¿ãç»é²ãããã¼ãã«ã¯ Spring Boot ã§æ¸ç±ã®è²¸åºç¶æ³ç¢ºèªã»è²¸åºç³è«ãã Web ã¢ããªã±ã¼ã·ã§ã³ãä½ã ( ãã®ï¼ )( DBããã¼ãã«ã®ä½æ ) ã§ä½æãã user_info, user_role ãã¼ãã«ã使ç¨ãã¾ãã
- Spring Integration ã«ã¯ Excel ãã¡ã¤ã«ãå¦çããã©ã¤ãã©ãªã¯ããã¾ããã®ã§ãJXLS ãå©ç¨ãã¾ãã
- DB ã®ç»é²å¦ç㯠Spring Integration ãæä¾ãã 18. JDBC Support ã§ã¯ãªã Doma 2 ã使ãã¾ãã
- Excel 㯠Office 365 ã®ãã®ã使ç¨ãã¾ãã
é·ãã®ã§ï¼åã«åãã¾ãã
åç §ãããµã¤ãã»æ¸ç±
Jxls Reader
http://jxls.sourceforge.net/reference/reader.htmlSpring Framework Reference Documentation - 8. Resources
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/resources.html
ç®æ¬¡
- 追å ããæ©è½ã®ä»æ§ãã¾ã¨ãã
- build.gradle ãå¤æ´ãã
- Doma 2 ã使ãããã®ãã¡ã¤ã«ã»è¨å®ã追å ã Dao, Entity ãçæãã
- Filter ãä½æãã
- åä½ç¢ºèª
- Jxls Reader ãå©ç¨ã㦠Excel ãã¡ã¤ã«ã®ãã¼ã¿ãèªã¿è¾¼ãå¦çãå®è£ ãã
- é·ããªãéããã®ã§æ¬¡åã¸ããã
æé
追å ããæ©è½ã®ä»æ§ãã¾ã¨ãã
ç¾å¨ä»¥ä¸ã®ç»åã®ããã¼ã§ããã
filter 㨠excelToDbChannel ã追å ãã¦ä»¥ä¸ã®ããã¼ã«å¤æ´ãã¾ãã
â»ç»å㯠STS ã® integration-graph ã§æãã¦ãã¾ãã
Filter ã追å ãã¦ãæ¡å¼µåã .xlsx ã§ãªããã°ä¾å¤ã throw ãã¦å¦çãç°å¸¸çµäºããã¾ãã
â»Filter ã¯æ¬æ¥æ»ãå¤ã« boolean åãè¿ã㦠Message ã次㮠Channel ã«æ¸¡ããå¦ããå¤æããããã®ãã®ã§ãããä¾å¤ã throw ããªã㨠PseudoTransactionManager ã«ãããã©ã³ã¶ã¯ã·ã§ã³å¦çã rollback ããªãã®ã§ãä»å㯠.xlsx ã§ãªããã°ä¾å¤ã throw ããããã§ãªããã° true ãæ»ãããã«å®è£ ãã¾ãã- excelToDbChannel ã DirectChannel ã¨ãã¦è¿½å ãã¾ããDirectChannel ã«ããã®ã¯ checkFile –> filter –> process ã®ä¸é£ã®æµããï¼ã¤ã®ãã©ã³ã¶ã¯ã·ã§ã³ã¨ãã¦å¦çããããã§ã ( filterãprocess ã§ä¾å¤ã throw ãããã¨ãã¡ã¤ã«ã¯ error ãã£ã¬ã¯ããªã«ç§»åããã¾ã )ã
Excel ãã¡ã¤ã«ã¯ä»¥ä¸ã®ç»åã®ãã©ã¼ãããã¨ãã¾ãã
- é ç®ã¯ååããã¹ã¯ã¼ããã¡ã¼ã«ã¢ãã¬ã¹ã権éã®ï¼åã§ãã
- 1è¡ç®ã¯ãããè¡ã2è¡ç®ä»¥éããã¼ã¿è¡ã¨ãã¾ãã
- 権éã¯ã«ã³ãåºåãã§è¤æ°è¨å ¥å¯è½ã¨ãã¾ãã
process ã§ã¯ Excel ãã¡ã¤ã«ã®ãã¼ã¿ãï¼è¡ãã¤ä»¥ä¸ã®ããã«å¦çãã¾ãã
- ååããã¹ã¯ã¼ããã¡ã¼ã«ã¢ãã¬ã¹ã®ãã¼ã¿ã user_info ãã¼ãã«ã«è¿½å ãã¾ãããã¹ã¯ã¼ãã¯è¿½å æã« Spring Security ã® BCryptPasswordEncoder#encode ã§æå·åãã¾ãã
- 権éã®ãã¼ã¿ãã«ã³ããåºåãæåã¨ãã¦å解ãã¦ã権éã®åæ°å user_role ãã¼ãã«ã«è¿½å ãã¾ãã
- Service Activator ã® process ã§ã¯ Excel ãã¡ã¤ã«ã®å ¨ãã¼ã¿ãç»é²ã§ããã commit ããã§ããªãã£ãå ´åã«ã¯ rollback ãããããã«ãã¾ãã
build.gradle ãå¤æ´ãã
feature/5-issue ãã©ã³ããä½æãã¾ãã
build.gradle ã ãªã³ã¯å ã®ãã®ï¼ã®å 容 ã«å¤æ´ãã¾ãã
Gradle projects View ã®å·¦ä¸ã«ãããRefresh all Gradle projectsããã¿ã³ãã¯ãªãã¯ã㦠build.gradle ãåæ ãã¾ãã
Doma 2 ã使ãããã®ãã¡ã¤ã«ã»è¨å®ã追å ã Dao, Entity ãçæãã
ksbysample-webapp-lending ãã以ä¸ã®ãã¡ã¤ã«ãã³ãã¼ãã¾ãã
â»â ã®ãããã¡ã¤ã«ã¯ â ã«æ¸ãããå ´æã«é ç½®ãã¾ãã
- src/main/java/ksbysample/webapp/lending/config/DomaConfig.java
â src/main/java/ksbysample/eipapp/dirchecker/config/DomaConfig.java - src/main/java/ksbysample/webapp/lending/util/doma/ComponentAndAutowiredDomaConfig.java
â src/main/java/ksbysample/eipapp/dirchecker/util/doma/ComponentAndAutowiredDomaConfig.java - src/main/java/ksbysample/webapp/lending/util/doma/SelectOptionsUtils.java
â src/main/java/ksbysample/eipapp/dirchecker/util/doma/SelectOptionsUtils.java - src/main/resources/log4jdbc.log4j2.properties
- src/main/java/ksbysample/webapp/lending/config/DomaConfig.java
src/main/resources ã®ä¸ã« application.properties ãä½æãããªã³ã¯å ã®å 容 ã®å 容ã«å¤æ´ãã¾ãã
src/main/java/ksbysample/eipapp/dirchecker ã®ä¸ã® Application.java ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
Gradle projects View ãã domaGen ã¿ã¹ã¯ãå®è¡ã㦠Dao, Entity ãçæãã¾ãã
Gradle projects View ãã bootRun ã¿ã¹ã¯ãå®è¡ãã¦æ£å¸¸ã«èµ·åããã確èªãã¾ããDoma 2 ã® SQL ãã¡ã¤ã«çæå¾ã¯ãã®ã¾ã¾èµ·åããã¨ã¨ã©ã¼ãåºãå ´åãããã®ã§ãclean ã¿ã¹ã¯å®è¡ â Rebuild Project å®è¡ãããå¾ã« bootRun ã¿ã¹ã¯ãå®è¡ãã¾ãã
Tomcat ãèµ·åãããã¨ã確èªã§ããã Ctrl+F2 ãæ¼ãã¦åæ¢ãã¾ãã
Filter ãä½æãã
src/main/java/ksbysample/eipapp/dirchecker/eip/channel ã®ä¸ã® ChannelConfig.java ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
src/main/java/ksbysample/eipapp/dirchecker/eip/endpoint ã®ä¸ã® InDirChecker.java ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
src/main/java/ksbysample/eipapp/dirchecker/eip/endpoint ã®ä¸ã® FileProcessor.java ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
åä½ç¢ºèª
Gradle projects View ãã bootRun ãå®è¡ãã¾ãã
C:\eipapp\ksbysample-eipapp-dirchecker\data\in ã«ããã¹ããã¡ã¤ã«ï¼.xlsxãã¨ãããã¡ã¤ã«ãé ç½®ããã¨ããã¡ã¤ã«ããªããªãã¾ããã
âââ
C:\eipapp\ksbysample-eipapp-dirchecker\data\in ã«ããã¹ããã¡ã¤ã«ï¼.txtãã¨ãããã¡ã¤ã«ãé ç½®ããã¨ãerror ãã£ã¬ã¯ããªã«ç§»åãã¾ããã
âââ
Ctrl+F2 ãæ¼ã㦠Tomcat ãåæ¢ãã¾ãã
Jxls Reader ãå©ç¨ã㦠Excel ãã¡ã¤ã«ã®ãã¼ã¿ãèªã¿è¾¼ãå¦çãå®è£ ãã
Jxls Reader 㧠Excel ãã¡ã¤ã«ãå¦çããããã«ã¯ã
- ãã¼ã¿æ ¼ç´ç¨ã¯ã©ã¹ãä½æãã¾ãã
- Excel ãã¡ã¤ã«ã¨ãã¼ã¿æ ¼ç´ç¨ã¯ã©ã¹ã®å¯¾å¿é¢ä¿ãè¨è¿°ãã XML ãã¡ã¤ã«ãä½æãã¾ãã
- Java ã®å®è£ ãè¡ãã¾ãã
ã§ãã®ã§ããã®é ã§å®è£ ãã¾ãã
src/main/java/ksbysample/eipapp/dirchecker ã®ä¸ã« service.userinfo ããã±ã¼ã¸ãä½æãã¾ãã
src/main/java/ksbysample/eipapp/dirchecker/service/userinfo ã®ä¸ã« UserInfoExcelRow.java ãä½æãã¾ããä½æå¾ããªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
getRoleListFromRoles ã¡ã½ããã®åä½ç¢ºèªããããã®ã§ãã¹ãã¯ã©ã¹ãä½æãã¾ããUserInfoExcelRow.java ã®ã½ã¼ã¹ä¸ã§ Ctrl+Shift+T ãæ¼ãã¦ã³ã³ããã¹ãã¡ãã¥ã¼ã表示ããå¾ãCreate New Test…ããé¸æãã¾ãããCreate Testããã¤ã¢ãã°ã表示ãããã以ä¸ã®ç»åã®ç¶æ ã«ããå¾ããOKããã¿ã³ãã¯ãªãã¯ãã¾ãã
ãChoose Destination Directoryããã¤ã¢ãã°ã表示ããããã…\src\test\groovy...ãã®æ¹ãé¸æãã¦ãOKããã¿ã³ãã¯ãªãã¯ãã¾ãã
src/test/groovy/ksbysample/eipapp/dirchecker/service/userinfo ã®ä¸ã« UserInfoExcelRowTest.groovy ãä½æããã¾ãã®ã§ããªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
ä»ã®è¨è¿°ã§ã¯ groovy ã§ã³ã³ãã¤ã«ãåºæ¥ãªãã£ãã®ã§ build.gradle ã ãªã³ã¯å ã®ãã®ï¼ã®å 容 ã«å¤æ´ãã¾ãã
ãã¹ããå®è¡ãã¦å ¨ã¦æåãããã¨ã確èªãã¾ãã
Excel ãã¡ã¤ã«ã¨ãã¼ã¿æ ¼ç´ç¨ã¯ã©ã¹ã®å¯¾å¿é¢ä¿ãè¨è¿°ãã XML ãã¡ã¤ã«ãä½æãã¾ããsrc/main/resources ã®ä¸ã« ksbysample/eipapp/dirchecker/service/userinfo ãã£ã¬ã¯ããªãä½æãã¾ãã
src/main/resources/ksbysample/eipapp/dirchecker/service/userinfo ã®ä¸ã« userinfo-excel-cfg.xml ãä½æãã¾ããä½æå¾ããªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
å¦çãè¡ã Service ã¯ã©ã¹ãå®è£ ãã¾ããsrc/main/java/ksbysample/eipapp/dirchecker/service/userinfo ã®ä¸ã« UserInfoService.java ãä½æãã¾ããä½æå¾ããªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
UserInfoService#loadFromExcelToList ã®åä½ç¢ºèªããããã®ã§ãã¹ãã¯ã©ã¹ãä½æãã¾ãããã¹ãç¨ã® Excel ãã¡ã¤ã«ã src/test/resources ã®ä¸ã«é ç½®ãã¾ãã®ã§ãã¾ã㯠src/test/resources ã®ä¸ã« ksbysample/eipapp/dirchecker/service/userinfo ãã£ã¬ã¯ããªãä½æãã¾ãã
src/test/resources/ksbysample/eipapp/dirchecker/service/userinfo ã®ä¸ã«ä»¥ä¸ã®ç»åã® Excel ãã¡ã¤ã«ã TestData01.xlsx ã¨ãããã¡ã¤ã«åã§é ç½®ãã¾ãã
ãã¹ã㧠AssertJ ã使ãããã®ã§ build.gradle ã ãªã³ã¯å ã®ãã®ï¼ã®å 容 ã«å¤æ´ãã¾ãã
Gradle projects View ã®å·¦ä¸ã«ãããRefresh all Gradle projectsããã¿ã³ãã¯ãªãã¯ã㦠build.gradle ãåæ ãã¾ãã
UserInfoService.java ã®ã½ã¼ã¹ä¸ã§ Ctrl+Shift+T ãæ¼ãã¦ã³ã³ããã¹ãã¡ãã¥ã¼ã表示ããå¾ãCreate New Test…ããé¸æãã¾ãããCreate Testããã¤ã¢ãã°ã表示ãããã以ä¸ã®ç»åã®ç¶æ ã«ããå¾ããOKããã¿ã³ãã¯ãªãã¯ãã¾ãã
ãChoose Destination Directoryããã¤ã¢ãã°ã表示ããããã…\src\test\java...ãã®æ¹ãé¸æãã¦ãOKããã¿ã³ãã¯ãªãã¯ãã¾ãã
src/test/java/ksbysample/eipapp/dirchecker/service/userinfo ã®ä¸ã« UserInfoServiceTest.java ãä½æããã¾ãã®ã§ããªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
ãã¹ããå®è¡ãã¦å ¨ã¦æåãããã¨ã確èªãã¾ãã
é·ããªãéããã®ã§æ¬¡åã¸ããã
é·ããªãéããã®ã§ä¸æ¦åºåãã¾ãããã®ï¼ã¸ã
以ä¸ã¡ã¢æ¸ãã§ãã
- Spring Integration ããããªã STS ã® integration-graph ã¯ä¾¿å©ã§ãã ( ã¨ããããå¿ é ï¼ )ãXML ãã¡ã¤ã«ãã¼ã¹ã§ãããªããGUI ã§ç°¡åã« XML ãã¡ã¤ã«ãä½æã§ãã¾ãã
ã½ã¼ã¹ã³ã¼ã
build.gradle
â ãã®ï¼
group 'ksbysample' version '1.1.0-RELEASE' buildscript { ext { springBootVersion = '1.3.7.RELEASE' } repositories { jcenter() maven { url "http://repo.spring.io/repo/" } } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") classpath("io.spring.gradle:dependency-management-plugin:0.6.0.RELEASE") // for Grgit classpath("org.ajoberstar:grgit:1.7.0") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'idea' apply plugin: 'spring-boot' apply plugin: 'io.spring.dependency-management' apply plugin: 'groovy' sourceCompatibility = 1.8 targetCompatibility = 1.8 compileJava.options.compilerArgs = ['-Xlint:all'] compileTestGroovy.options.compilerArgs = ['-Xlint:all'] compileTestJava.options.compilerArgs = ['-Xlint:all'] eclipse { classpath { containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER') containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8' } } idea { module { inheritOutputDirs = false outputDir = file("$buildDir/classes/main/") } } configurations { domaGenRuntime } repositories { jcenter() } dependencyManagement { imports { mavenBom 'io.spring.platform:platform-bom:2.0.7.RELEASE' } } dependencies { def jdbcDriver = "org.postgresql:postgresql:9.4.1209" // 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-starter-integration') compile('org.springframework.integration:spring-integration-file') compile("org.springframework.boot:spring-boot-starter-security") compile("org.springframework.boot:spring-boot-starter-data-jpa") testCompile("org.springframework.boot:spring-boot-starter-test") testCompile("org.spockframework:spock-core") { exclude module: "groovy-all" } testCompile("org.spockframework:spock-spring") { exclude module: "groovy-all" } // dependency-management-plugin ã«ãããã¼ã¸ã§ã³çªå·ãèªåã§è¨å®ãããªããã®ããããã¯ææ°ãã¼ã¸ã§ã³ãæå®ããããã® runtime("${jdbcDriver}") compile("org.seasar.doma:doma:2.12.1") compile("org.bgee.log4jdbc-log4j2:log4jdbc-log4j2-jdbc4.1:1.16") compile("org.projectlombok:lombok:1.16.10") compile("org.apache.commons:commons-lang3:3.4") compile("org.jxls:jxls-reader:2.0.2") // for Doma-Gen domaGenRuntime("org.seasar.doma:doma-gen:2.12.1") domaGenRuntime("${jdbcDriver}") } // for Doma-Gen task domaGen << { // ã¾ãå¤æ´ãå¿ è¦ãªãã® def rootPackageName = 'ksbysample.eipapp.dirchecker' def daoPackagePath = 'src/main/java/ksbysample/eipapp/dirchecker/dao' def dbUrl = 'jdbc:postgresql://localhost/ksbylending' def dbUser = 'ksbylending_user' def dbPassword = 'xxxxxxxx' def tableNamePattern = '.*' // ããããå¤æ´ä¸è¦ãªãã® def importOfComponentAndAutowiredDomaConfig = "${rootPackageName}.util.doma.ComponentAndAutowiredDomaConfig" def workDirPath = 'work' def workDaoDirPath = "${workDirPath}/dao" // ä½æ¥ç¨ãã£ã¬ã¯ããªãåé¤ãã clearDir("${workDirPath}") // ç¾å¨ã® Dao ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®ããã¯ã¢ãããåå¾ãã copy() { from "${daoPackagePath}" into "${workDaoDirPath}/org" } // Dao ã¤ã³ã¿ã¼ãã§ã¼ã¹ãEntity ã¯ã©ã¹ãçæãã ant.taskdef(resource: 'domagentask.properties', classpath: configurations.domaGenRuntime.asPath) ant.gen(url: "${dbUrl}", user: "${dbUser}", password: "${dbPassword}", tableNamePattern: "${tableNamePattern}") { entityConfig(packageName: "${rootPackageName}.entity", useListener: false) daoConfig(packageName: "${rootPackageName}.dao") sqlConfig() } // çæããã Dao ã¤ã³ã¿ã¼ãã§ã¼ã¹ãä½æ¥ç¨ãã£ã¬ã¯ããªã«ã³ãã¼ãã // @ComponentAndAutowiredDomaConfig ã¢ããã¼ã·ã§ã³ãä»å ãã copy() { from "${daoPackagePath}" into "${workDaoDirPath}/replace" filter { line -> line.replaceAll('import org.seasar.doma.Dao;', "import ${importOfComponentAndAutowiredDomaConfig};\nimport org.seasar.doma.Dao;") .replaceAll('@Dao', '@Dao\n@ComponentAndAutowiredDomaConfig') } } // @ComponentAndAutowiredDomaConfig ã¢ããã¼ã·ã§ã³ãä»å ãã Dao ã¤ã³ã¿ã¼ãã§ã¼ã¹ã // dao ããã±ã¼ã¸ã¸æ»ã copy() { from "${workDaoDirPath}/replace" into "${daoPackagePath}" } // å ã dao ããã±ã¼ã¸å ã«ãã£ããã¡ã¤ã«ãå ã«æ»ã copy() { from "${workDaoDirPath}/org" into "${daoPackagePath}" } // ä½æ¥ç¨ãã£ã¬ã¯ããªãåé¤ãã clearDir("${workDirPath}") // èªåçæãããã¡ã¤ã«ã git add ãã addGit() } /* ----------------------------------------------------------------------------- * ã¡ã½ããå®ç¾©é¨ ---------------------------------------------------------------------------- */ void clearDir(String dirPath) { delete dirPath } void addGit() { def grgit = org.ajoberstar.grgit.Grgit.open(dir: project.projectDir.parent) grgit.add(patterns: ['.']) }
- version ã
1.0.0-RELEASE
â1.1.0-RELEASE
ã¸å¤æ´ãã¾ãã - buildscript ã«
classpath("org.ajoberstar:grgit:1.7.0")
ã追å ãã¾ãã configurations { domaGenRuntime }
ã追å ãã¾ãã- dependencies ã«ä»¥ä¸ã®è¨è¿°ã追å ãã¾ãã
def jdbcDriver = "org.postgresql:postgresql:9.4.1209"
compile("org.springframework.boot:spring-boot-starter-security")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
runtime("${jdbcDriver}")
compile("org.seasar.doma:doma:2.12.1")
compile("org.bgee.log4jdbc-log4j2:log4jdbc-log4j2-jdbc4.1:1.16")
compile("org.jxls:jxls-reader:2.0.2")
domaGenRuntime("org.seasar.doma:doma-gen:2.12.1")
domaGenRuntime("${jdbcDriver}")
task domaGen << { ... }
ã追å ãã¾ãã- clearDir ã¡ã½ããã追å ãã¾ãã
- addGit ã¡ã½ããã追å ãã¾ããopen ãããã£ã¬ã¯ããªã®æå®ã¯
project.projectDir
âproject.projectDir.parent
ã¸å¤æ´ãã¾ãã
â ãã®ï¼
dependencies { def jdbcDriver = "org.postgresql:postgresql:9.4.1209" // 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-starter-integration') compile('org.springframework.integration:spring-integration-file') compile("org.springframework.boot:spring-boot-starter-security") compile("org.springframework.boot:spring-boot-starter-data-jpa") testCompile("org.springframework.boot:spring-boot-starter-test") testCompile("org.spockframework:spock-core") testCompile("org.spockframework:spock-spring") // dependency-management-plugin ã«ãããã¼ã¸ã§ã³çªå·ãèªåã§è¨å®ãããªããã®ããããã¯ææ°ãã¼ã¸ã§ã³ãæå®ããããã® runtime("${jdbcDriver}") compile("org.seasar.doma:doma:2.12.1") compile("org.bgee.log4jdbc-log4j2:log4jdbc-log4j2-jdbc4.1:1.16") compile("org.projectlombok:lombok:1.16.10") compile("org.apache.commons:commons-lang3:3.4") compile("org.jxls:jxls-reader:2.0.2") // for Doma-Gen domaGenRuntime("org.seasar.doma:doma-gen:2.12.1") domaGenRuntime("${jdbcDriver}") }
- spock-core, spock-spring ã®å¾ã®
{ exclude module: "groovy-all" }
ã®è¨è¿°ãåé¤ãã¾ãã
â ãã®ï¼
dependencies { def jdbcDriver = "org.postgresql:postgresql:9.4.1209" // 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-starter-integration') compile('org.springframework.integration:spring-integration-file') compile("org.springframework.boot:spring-boot-starter-security") compile("org.springframework.boot:spring-boot-starter-data-jpa") testCompile("org.springframework.boot:spring-boot-starter-test") testCompile("org.spockframework:spock-core") testCompile("org.spockframework:spock-spring") // dependency-management-plugin ã«ãããã¼ã¸ã§ã³çªå·ãèªåã§è¨å®ãããªããã®ããããã¯ææ°ãã¼ã¸ã§ã³ãæå®ããããã® runtime("${jdbcDriver}") compile("org.seasar.doma:doma:2.12.1") compile("org.bgee.log4jdbc-log4j2:log4jdbc-log4j2-jdbc4.1:1.16") compile("org.projectlombok:lombok:1.16.10") compile("org.apache.commons:commons-lang3:3.4") compile("org.jxls:jxls-reader:2.0.2") testCompile("org.assertj:assertj-core:3.5.2") // for Doma-Gen domaGenRuntime("org.seasar.doma:doma-gen:2.12.1") domaGenRuntime("${jdbcDriver}") }
- dependencies ã«
testCompile("org.assertj:assertj-core:3.5.2")
ã追å ãã¾ãã
application.properties
hibernate.dialect=org.hibernate.dialect.PostgreSQL9Dialect doma.dialect=org.seasar.doma.jdbc.dialect.PostgresDialect spring.datasource.url=jdbc:log4jdbc:postgresql://localhost/ksbylending spring.datasource.username=ksbylending_user spring.datasource.password=xxxxxxxx spring.datasource.driverClassName=net.sf.log4jdbc.sql.jdbcapi.DriverSpy spring.datasource.initial-size=1
Application.java
package ksbysample.eipapp.dirchecker; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; @SpringBootApplication(exclude = {JpaRepositoriesAutoConfiguration.class, HibernateJpaAutoConfiguration.class}) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
@SpringBootApplication
â@SpringBootApplication(exclude = {JpaRepositoriesAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
ã¸å¤æ´ãã¾ãã
ChannelConfig.java
package ksbysample.eipapp.dirchecker.eip.channel; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.integration.channel.DirectChannel; import org.springframework.messaging.MessageChannel; @Configuration public class ChannelConfig { @Bean public MessageChannel inChannel() { return new DirectChannel(); } @Bean public MessageChannel excelToDbChannel() { return new DirectChannel(); } }
@Bean public MessageChannel excelToDbChannel() { ... }
ã追å ãã¾ãã
InDirChecker.java
package ksbysample.eipapp.dirchecker.eip.endpoint; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.expression.ExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.integration.annotation.Filter; import org.springframework.integration.annotation.InboundChannelAdapter; import org.springframework.integration.annotation.MessageEndpoint; import org.springframework.integration.annotation.Poller; import org.springframework.integration.channel.NullChannel; import org.springframework.integration.file.FileReadingMessageSource; import org.springframework.integration.scheduling.PollerMetadata; import org.springframework.integration.transaction.DefaultTransactionSynchronizationFactory; import org.springframework.integration.transaction.ExpressionEvaluatingTransactionSynchronizationProcessor; import org.springframework.integration.transaction.PseudoTransactionManager; import org.springframework.integration.transaction.TransactionSynchronizationFactory; import org.springframework.messaging.Message; import org.springframework.scheduling.support.PeriodicTrigger; import org.springframework.transaction.interceptor.DefaultTransactionAttribute; import org.springframework.transaction.interceptor.MatchAlwaysTransactionAttributeSource; import org.springframework.transaction.interceptor.TransactionInterceptor; import java.io.File; import java.nio.file.Paths; import java.util.Collections; @MessageEndpoint public class InDirChecker { .......... @InboundChannelAdapter(value = "inChannel", poller = @Poller("checkFilePoller")) public Message<File> checkFile() { return inDirFileReadingMessageSource.receive(); } @Filter(inputChannel = "inChannel", outputChannel = "excelToDbChannel") public boolean filter(Message<File> message) { File file = message.getPayload(); if (!StringUtils.endsWith(file.getName(), ".xlsx")) { throw new RuntimeException( String.format("æ¡å¼µåã .xlsx ã®ãã¡ã¤ã«ã§ã¯ããã¾ãã ( %s )ã", file.getName())); } return true; } }
@Filter(inputChannel = "inChannel", outputChannel = "excelToDbChannel") public boolean filter(Message<File> message) { ... }
ã追å ãã¾ãã
FileProcessor.java
package ksbysample.eipapp.dirchecker.eip.endpoint; import org.springframework.integration.annotation.MessageEndpoint; import org.springframework.integration.annotation.ServiceActivator; import org.springframework.messaging.Message; import java.io.File; @MessageEndpoint public class FileProcessor { @ServiceActivator(inputChannel = "excelToDbChannel") public void process(Message<File> message) throws Exception { File file = message.getPayload(); System.out.println(file.getAbsolutePath()); } }
@ServiceActivator(inputChannel = "inChannel")
â@ServiceActivator(inputChannel = "excelToDbChannel")
ã«å¤æ´ãã¾ãã
UserInfoExcelRow.java
package ksbysample.eipapp.dirchecker.service.userinfo; import lombok.Data; import org.apache.commons.lang3.StringUtils; import java.util.Arrays; import java.util.Collections; import java.util.List; @Data public class UserInfoExcelRow { private String username; private String password; private String mailAddress; private String roles; public List<String> getRoleListFromRoles() { List<String> result = Collections.EMPTY_LIST; if (StringUtils.isNotBlank(this.roles)) { String[] roleArray = roles.split(","); result = Arrays.asList(roleArray); } return result; } }
UserInfoExcelRowTest.groovy
package ksbysample.eipapp.dirchecker.service.userinfo import spock.lang.Specification import spock.lang.Unroll class UserInfoExcelRowTest extends Specification { @Unroll def "GetRoleListFromRoles(#roles) --> #result"() { given: UserInfoExcelRow userInfoExcelRow = new UserInfoExcelRow() userInfoExcelRow.roles = roles expect: userInfoExcelRow.getRoleListFromRoles() == result where: roles || result null || [] "" || [] "ROLE_USER" || ["ROLE_USER"] "ROLE_USER,ROLE_ADMIN" || ["ROLE_USER", "ROLE_ADMIN"] } }
userinfo-excel-cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <workbook> <worksheet name="Sheet1"> <section startRow="0" endRow="0"> </section> <loop startRow="1" endRow="1" items="userInfoExcelRowList" var="userInfoExcelRow" varType="ksbysample.eipapp.dirchecker.service.userinfo.UserInfoExcelRow"> <section startRow="1" endRow="1"> <mapping row="1" col="0">userInfoExcelRow.username</mapping> <mapping row="1" col="1">userInfoExcelRow.password</mapping> <mapping row="1" col="2">userInfoExcelRow.mailAddress</mapping> <mapping row="1" col="3">userInfoExcelRow.roles</mapping> </section> <loopbreakcondition> <rowcheck offset="0"> <cellcheck offset="0"></cellcheck> </rowcheck> </loopbreakcondition> </loop> </worksheet> </workbook>
UserInfoService.java
package ksbysample.eipapp.dirchecker.service.userinfo; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.jxls.reader.ReaderBuilder; import org.jxls.reader.XLSReadStatus; import org.jxls.reader.XLSReader; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import org.springframework.stereotype.Service; import org.xml.sax.SAXException; import java.io.BufferedInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @Service public class UserInfoService { private static final String CLASSPATH_USERINFO_EXCEL_CFG_XML = "ksbysample/eipapp/dirchecker/service/userinfo/userinfo-excel-cfg.xml"; public List<UserInfoExcelRow> loadFromExcelToList(File excelFile) throws IOException, SAXException, InvalidFormatException { Resource rsExcelCfgXml = new ClassPathResource(CLASSPATH_USERINFO_EXCEL_CFG_XML); Resource rsUserInfoExcel = new FileSystemResource(excelFile.getAbsolutePath()); XLSReader reader = ReaderBuilder.buildFromXML(rsExcelCfgXml.getFile()); List<UserInfoExcelRow> userInfoExcelRowList = new ArrayList<>(); Map<String, Object> beans = new HashMap<>(); beans.put("userInfoExcelRow", new UserInfoExcelRow()); beans.put("userInfoExcelRowList", userInfoExcelRowList); try (InputStream isUserInfoExcel = new BufferedInputStream(rsUserInfoExcel.getInputStream())) { XLSReadStatus status = reader.read(isUserInfoExcel, beans); } return userInfoExcelRowList; } }
UserInfoServiceTest.java
package ksbysample.eipapp.dirchecker.service.userinfo; import ksbysample.eipapp.dirchecker.Application; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.tuple; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) public class UserInfoServiceTest { private static final String CLASSPATH_EXCEL_FOR_TEST = "ksbysample/eipapp/dirchecker/service/userinfo/Book1.xlsx"; @Autowired private UserInfoService userInfoService; @Test public void loadFromExcelToList() throws Exception { Resource resource = new ClassPathResource(CLASSPATH_EXCEL_FOR_TEST); List<UserInfoExcelRow> userInfoExcelRowList = userInfoService.loadFromExcelToList(resource.getFile()); assertThat(userInfoExcelRowList).hasSize(2); assertThat(userInfoExcelRowList).extracting("username", "password", "mailAddress", "roles") .containsOnly(tuple("yota takahashi", "12345678", "[email protected]", "ROLE_USER") , tuple("aoi inoue", "abcdefgh", "[email protected]", "ROLE_ADMIN,ROLE_USER")); } }
å±¥æ´
2016/08/29
åççºè¡ã
2016/08/29
* build.gradle ãã { exclude module: "groovy-all" }
ãåé¤ããè¨è¿°ã追å ãã¾ããã