Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Spring Bootの本当の理解ポイント #jjug

20,352 views

Published on

JJUG CCC 2017 Fallの発表資料です。

Published in: Technology
  • C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ ▸ @Bean ▸ basePackages @Component 30 @Configuration @ComponentScan(basePackages = “hoge.service.impl”) public class AppConfig4 { @Bean public BarService barService() { return new BarServiceImpl(); } }For more information,you can contact at email: getdegree18@hotmail.com whatsapp: +86 +86 17823792342 QQ/wechat:203288895/Getdegree18 CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ @Bean 
 27 @Configuration // @ComponentScan public class AppConfig2 { @Bean public BarService barService() { // return new BarServiceImpl(); } }
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Spring Bootの本当の理解ポイント #jjug

  1. 1. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 1
  2. 2. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ ▸ 2
  3. 3. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ 
 3 基礎を固めよ!
  4. 4. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ ▸ ▸ ▸ 4
  5. 5. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ ▸ ▸ ▸ 5
  6. 6. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 6
  7. 7. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ ▸ ▸ ▸ ▸ ▸ 7
  8. 8. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ 8
  9. 9. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ ▸ ▸ ▸ ▸ ▸ 9
  10. 10. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 10 Spring Web 4 Spring MVC Spring Boot Developer 2 Spring Boot Spring Cloud Services 3 ( ) Spring Cloud Microservices
  11. 11. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ ▸ ▸ ▸ 11
  12. 12. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 12
  13. 13. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ ▸ 13 Spring Framework Spring Boot Spring Cloud Data, Security, Batch, …
  14. 14. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ ▸ ▸ ▸ 14
  15. 15. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ ▸ ▸ 15 ※CoC : Convention over Configuration ( )
 
 
 
 Seasar2 Ruby on Rails
  16. 16. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ ▸ 
 ▸ 16
  17. 17. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ ▸ 17
  18. 18. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 18
  19. 19. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ ▸ ▸ ▸ ▸ ▸ 19
  20. 20. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ ▸ 20
  21. 21. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ 
 @Component 21 package hoge.service.impl; @Component public class FooServiceImpl implements FooService { // } ※ ( FooService)
  22. 22. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ @Configuration 
 @ComponentScan 22 package hoge.config; @Configuration @ComponentScan(basePackages = { “hoge.service.impl”}) public class AppConfig { // OK }
  23. 23. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ basePackages @Component 23 hoge config service impl bar App Config FooService Impl Bar FooService 

  24. 24. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ AnnotationConfigApplicationContext 
 ▸ ApplicationContext 24 // // ApplicationContext context = new AnnotationConfigApplicationContext( AppConfig.class); ※ Java Config
  25. 25. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ ▸ getBean() 25 ApplicationContext context = …; FooService fs = context.getBean(FooService.class);
  26. 26. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ ▸ 26 @Component // public class FooController { private final FooService fs; // @Autowired // public FooController(FooService fs) { this.fs = fs; }}
  27. 27. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ @Bean 
 27 @Configuration // @ComponentScan public class AppConfig2 { @Bean public BarService barService() { // return new BarServiceImpl(); } }
  28. 28. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ @Bean 28 @Configuration public class AppConfig3 { @Bean public BarService barService( BarRepository br) { return new BarServiceImpl(br); } } 
 Bean 

  29. 29. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ @Component 29 ApplicationContext context = new AnnotationConfigApplicationContext( AppConfig2.class); BarService bs = context.getBean(BarService.class); @Component public class BarController { private final BarService bs; @Autowired // public BarController(BarService bs) { this.bs = bs; } }
  30. 30. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ ▸ @Bean ▸ basePackages @Component 30 @Configuration @ComponentScan(basePackages = “hoge.service.impl”) public class AppConfig4 { @Bean public BarService barService() { return new BarServiceImpl(); } }
  31. 31. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ ▸ ▸ ▸ @Component ▸ @Bean 31
  32. 32. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 32
  33. 33. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ ▸ ▸ ▸ 33
  34. 34. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 34 
 View Resolver ※ 
 Spring Spring 
 Dispatcher
 Servlet
  35. 35. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 DispatcherServlet ▸ ▸ WebApplicationInitialzer ▸ ▸ 35 Dispatcher
 Servlet
  36. 36. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ 
 ▸ ▸ 
 DispatcherServlet 36
  37. 37. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ViewResolver ▸ ▸ ”employee/index”
 ”/WEB-INF/views/employee/index.jsp” ▸ ▸ InternalResourceViewResolver ▸ ThymeleafViewReseolver ▸ FreeMarkerViewResolver 37
  38. 38. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ViewResolver 38 @Configuration public class MvcConfig … { @Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver vr = new InternalResourceViewResolver(); vr.setPrefix("/WEB-INF/views/"); vr.setSuffix(".jsp"); return vr; } }
  39. 39. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ViewResolver ▸ DispatcherServlet ViewResolver 39 https://github.com/spring-projects/spring-framework/blob/master/spring-webmvc/src/main/java/org/springframework/web/servlet/ DispatcherServlet.java#L733
  40. 40. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ 
 ViewResolver ▸ ▸ ▸ ▸ 40
  41. 41. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ 
 ViewResolver ▸ ▸ ▸ ▸ 41 @EnableWebMvc
  42. 42. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 @EnableWebMvc ▸ @Import ▸ ▸ @EnableXxx 
 42 https://github.com/spring-projects/spring-framework/blob/master/spring-webmvc/src/main/java/org/ springframework/web/servlet/config/annotation/EnableWebMvc.java#L101
  43. 43. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ @Controller ▸ @Component 43 package hoge.controller @Controller @RequestMapping(“/employee”) public class EmployeeController { @GetMapping(“/index”) public String index() { return “employee/index”; }
  44. 44. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ @Component ▸ @Controller ▸ @RestController ▸ @Configuration 👈 ▸ @Service ▸ @Repository 44
  45. 45. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ 45 @ComponentScan(basePackages = “hoge.controller”) @Configuration public class MvcConfig { … }
  46. 46. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 46 @EnableWebMvc @ComponentScan(basePackages = “hoge.controller”) @Configuration public class MvcConfig … { @Bean public InternalResourceViewResolver viewResolver() { … } }
  47. 47. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 47 DispatcherServlet ViewResolver
 Bean 
 Bean 
 Bean 
 Bean
  48. 48. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ DispatcherServlet 
 ▸ 
 ▸ @EnableWebMvc ▸ @Controller @ComponentScan 
 48
  49. 49. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ ▸ ▸ 
 ▸ ▸ 49 https://www.casareal.co.jp/recruit/jobs/ ls_teacher.php
  50. 50. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ 
 ✕ 
 ✕ ▸ 
 
 
 
 50 https://www.casareal.co.jp/ls/service/shinjinseminar/course01
  51. 51. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 51
  52. 52. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 52 ViewResolver
 Bean 
 Bean DataSource
 Bean 
 Bean 
 Bean 
 Bean 
 Bean 
 Bean
  53. 53. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 53 ViewResolver
 Bean 
 Bean DataSource
 Bean 
 Bean 
 Bean 
 Bean 
 Bean 
 Bean Bean 
 Bean
  54. 54. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ ▸ 54
  55. 55. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 55 @Configuration public class ThymeleafAutoConfiguration { … @Configuration public static class XxxConfiguration { @Bean public ThymeleafViewResolver thymeleafViewResolver() { … } … ※static Java Config 
 ViewResolver 
 https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/ springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java#L183
  56. 56. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ ▸ ▸ ▸ ▸ ▸ 56
  57. 57. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ 
 ▸ ▸ ▸ ▸ 57
  58. 58. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 @ConfigurationProperties ▸ 58 @ConfigurationProperties(prefix = “spring.thymeleaf”) public class ThymeleafProperties { … private Charset encoding = DEFAULT_ENCODING; private boolean cache = true; // } spring.thymeleaf.encoding=Shift_JIS spring.thymeleaf.cache=false https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/ springframework/boot/autoconfigure/thymeleaf/ThymeleafProperties.java
  59. 59. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 @EnableConfigurationProperties ▸ @ConfigurationProperties ▸ @Component/@Bean 59 https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/ springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java#L74 @EnableConfigurationProperties( ThymeleafProperties.class) @Configuration public class ThymeleafAutoConfiguration { …
  60. 60. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ ▸ ▸ 
 ▸ @ConfigurationProperties 60
  61. 61. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 61
  62. 62. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 62
  63. 63. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 63 @SpringBootApplication public class SampleApplication { public static void main(String[] args) { SpringApplication.run( SampleApplication.class); } } @EnableAutoConfiguration @Configuration @ComponentScan public @interface SpringBootApplication { …
  64. 64. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 64 @EnableAutoConfiguration org.springframework.boot.autoconfigure.EnableAutoCon figuration= org.springframework.boot.autoconfigure.admin.SpringA pplicationAdminJmxAutoConfiguration, org.springframework.boot.autoconfigure.aop.AopAutoCo nfiguration, org.springframework.boot.autoconfigure.amqp.RabbitAu toConfiguration, … 
 AutoConfiguration 
 https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-autoconfigure/src/main/ resources/META-INF/spring.factories 

  65. 65. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ ▸ 65
  66. 66. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ 
 ▸ ▸ 
 66
  67. 67. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 @ConditionalOnXxx ▸ @Bean ▸ 67
  68. 68. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ThymeleafAutoConfiguration 68 @Configuration @ConditionalOnClass(TemplateMode.class) @AutoConfigureAfter({WebMvcAutoConfiguration.class, … }) public class ThymeleafAutoConfiguration { … @Configuration @ConditionalOnWebApplication(…) public static class XxxConfiguration { @Bean @ConditionalOnMissingBean( name = “thymeleafViewResolver”) public ThymeleafViewResolver thymeleafViewResolver() { … https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/ springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java#L183
  69. 69. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 @ConditionalOnClass ▸ @ConditionalOnBean ▸ @ConditionalOnMissingBean ▸ 69
  70. 70. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 @ConditionalOnProperty ▸ @ConditionalOnWebApplication ▸ @AutoConfigureAfter/@AutoConfigureBefore ▸ 70
  71. 71. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ @EnableAutoConfiguration ▸ @ConditionalOnXxx 71
  72. 72. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 72
  73. 73. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 😭 @ConditionalOnMissingBean(Bean_1.class) 
 @ConditionalOnBean(Bean_2.class) 
 73
  74. 74. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 Spring Bootのべからず集 一.フレームワークを動かすBean は、なるべく自前で定義する べからず 二.@EnableXxxはなるべく付ける べからず 74
  75. 75. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ ▸ @EnableXxx ▸ ▸ 75
  76. 76. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 
 76
  77. 77. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ ▸ 77 https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
  78. 78. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 78 @Configuration public class SomeConfiguration { private final SomeBean sb; @Autowired // public SomeConfiguration(SomeBean sb) { this.sb = sb; } @PostConstruct // public void init() { sb.setXxx(…); // SomeBean } }
  79. 79. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ 79 @Configuration public class MyThymeleafConfiguration { @Bean public FooDialect fooDialect() { return new FooDialect(); } @Bean public BarDialect barDialect() { return new BarDialect(); } }
  80. 80. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ ThymeleafAutoConfiguration 
 Dialect 
 TemplateEngine Dialect 80 FooDialect
 Bean BarDialect
 Bean &Template
 Engine
 Bean &
  81. 81. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 81 @Configuration protected static class ThymeleafDefaultConfiguration { // Dialect Bean DI public ThymeleafDefaultConfiguration(..., ObjectProvider<Collection<IDialect>> dProvider) { … } @Bean public SpringTemplateEngine templateEngine() { SpringTemplateEngine engine = new SpringTemplateEngine(); // Dialect this.dialects.forEach(engine::addDialect); return engine; https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/ springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java#L140
  82. 82. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ XxxCustomizer ▸ ▸ 82 @FunctionalInterface public interface Jackson2ObjectMapperBuilderCustomizer { void customize(Jackson2ObjectMapperBuilder builder); } https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/ springframework/boot/autoconfigure/jackson/Jackson2ObjectMapperBuilderCustomizer.java
  83. 83. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 83 @Configuration public class MyJacksonConfiguration { @Bean public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() { // OK return builder -> { // builder.modules(…) .locale(…) .indentOutput(…); }; } }
  84. 84. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 84 @Configuration static class JacksonObjectMapperBuilderConfiguration { @Bean public Jackson2ObjectMapperBuilder …( List<Jackson2ObjectMapperBuilderCustomizer> customizers) { Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder(); for (Jackson2ObjectMapperBuilderCustomizer customizer : customizers) { customizer.customize(builder); } return builder; } } https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/ springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java#L172
  85. 85. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ FilterRegistrationBean 85 @Bean public FilterRegistrationBean myFilter() { MyFilter filter = new MyFilter(); FilterRegistrationBean frb = new FilterRegistrationBean(filter); // url-pattern frb.addUrlPatterns(“/*”); // frb.setOrder(Ordered.HIGHEST_PRECEDENCE + 10); return frb; }
  86. 86. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ ▸ ▸ ▸ ▸ 86
  87. 87. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 87
  88. 88. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ ▸ ▸ ▸ ▸ 88
  89. 89. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ ▸ ▸ ▸ 89
  90. 90. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ ▸ ▸ ▸ ▸ ▸ 90
  91. 91. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ ▸ ▸ ▸ ▸ ▸ ▸ ▸ 
 91
  92. 92. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ 😡 ▸ ▸ 
 
 92
  93. 93. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ ▸ 93
  94. 94. (C) CASAREAL, Inc. All rights reserved. #jjug #ccc_ef3 ▸ 94

×