Upload
masatoshi-tada
View
7.965
Download
0
Embed Size (px)
Citation preview
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3 1
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
2
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
3
基礎を固めよ!
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
▸
▸
4
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
▸
▸
5
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
6
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
▸
▸
▸
▸7
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
8
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
▸
▸
▸
▸
9
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
10
Spring Web 4 Spring MVC
Spring Boot Developer 2 Spring Boot
Spring Cloud Services3
( )
Spring Cloud Microservices
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
▸
▸
11
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
12
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
13
Spring Framework
Spring Boot
Spring Cloud
Data, Security, Batch, …
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
▸
▸
14
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
▸
15
※CoC : Convention over Configuration ( )
Seasar2 Ruby on Rails
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
▸
16
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
17
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
18
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
▸
▸
▸
▸
19
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
20
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸ @Component
21
packagehoge.service.impl;
@ComponentpublicclassFooServiceImplimplementsFooService{// }※ ( FooService)
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸ @Configuration @ComponentScan
22
packagehoge.config;
@Configuration@ComponentScan(basePackages={“hoge.service.impl”})publicclassAppConfig{// OK}
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸ basePackages
@Component
23
hoge
config
service
impl
bar
AppConfig
FooServiceImpl
Bar
FooService
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸ AnnotationConfigApplicationContext
▸ ApplicationContext
24
// // ApplicationContextcontext=newAnnotationConfigApplicationContext(AppConfig.class);※ Java Config
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸ getBean()
25
ApplicationContextcontext=…;FooServicefs=context.getBean(FooService.class);
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
26
@Component// publicclassFooController{privatefinalFooServicefs;// @Autowired// publicFooController(FooServicefs){this.fs=fs;}}
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸ @Bean
27
@Configuration//@ComponentScan publicclassAppConfig2{@BeanpublicBarServicebarService(){//
returnnewBarServiceImpl();}}
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸ @Bean
28
@ConfigurationpublicclassAppConfig3{@BeanpublicBarServicebarService(BarRepositorybr){returnnewBarServiceImpl(br);}}
Bean
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸ @Component
29
ApplicationContextcontext=newAnnotationConfigApplicationContext(AppConfig2.class);BarServicebs=context.getBean(BarService.class);
@ComponentpublicclassBarController{privatefinalBarServicebs;@Autowired// publicBarController(BarServicebs){this.bs=bs;}}
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸ @Bean
▸ basePackages @Component
30
@Configuration@ComponentScan(basePackages=“hoge.service.impl”)publicclassAppConfig4{@BeanpublicBarServicebarService(){returnnewBarServiceImpl();}}
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
▸
▸ @Component
▸ @Bean
31
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
32
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
▸
▸
33
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
34
ViewResolver ※
Spring Spring
DispatcherServlet
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
DispatcherServlet
▸
▸WebApplicationInitialzer
▸
▸35
DispatcherServlet
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
▸ DispatcherServlet
36
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
ViewResolver▸
▸ ”employee/index””/WEB-INF/views/employee/index.jsp”
▸
▸ InternalResourceViewResolver
▸ ThymeleafViewReseolver
▸ FreeMarkerViewResolver
37
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
ViewResolver
38
@ConfigurationpublicclassMvcConfig…{@BeanpublicInternalResourceViewResolverviewResolver(){InternalResourceViewResolvervr=newInternalResourceViewResolver();vr.setPrefix("/WEB-INF/views/");vr.setSuffix(".jsp");returnvr;}}
(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
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸ ViewResolver
▸
▸
▸
▸
40
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸ ViewResolver
▸
▸
▸
▸
41
@EnableW
ebMvc
(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
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸ @Controller
▸ @Component
43
packagehoge.controller
@Controller@RequestMapping(“/employee”)publicclassEmployeeController{@GetMapping(“/index”)publicStringindex(){return“employee/index”;}
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸ @Component
▸ @Controller
▸ @RestController
▸ @Configuration👈
▸ @Service
▸ @Repository
44
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
45
@ComponentScan(basePackages=“hoge.controller”)@ConfigurationpublicclassMvcConfig{…}
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
46
@EnableWebMvc@ComponentScan(basePackages=“hoge.controller”)@ConfigurationpublicclassMvcConfig…{@BeanpublicInternalResourceViewResolverviewResolver(){…}}
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
47
DispatcherServlet
ViewResolverBean
Bean
Bean
Bean
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸ DispatcherServlet
▸
▸ @EnableWebMvc
▸ @Controller @ComponentScan
48
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
▸
▸
▸49
https://www.casareal.co.jp/recruit/jobs/ls_teacher.php
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸ ✕ ✕
▸
50
https://www.casareal.co.jp/ls/service/shinjinseminar/course01
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
51
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
52
ViewResolverBean
Bean
DataSourceBean
Bean
Bean
Bean
Bean
Bean
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
53
ViewResolverBean
Bean
DataSourceBean
Bean
Bean
Bean
Bean
Bean
Bean
Bean
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
54
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
55
@ConfigurationpublicclassThymeleafAutoConfiguration{…@ConfigurationpublicstaticclassXxxConfiguration{@BeanpublicThymeleafViewResolverthymeleafViewResolver(){…}…
※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
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
▸
▸
▸
▸56
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
▸
▸
▸
57
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
@ConfigurationProperties
▸
58
@ConfigurationProperties(prefix=“spring.thymeleaf”)publicclassThymeleafProperties{…privateCharsetencoding=DEFAULT_ENCODING;privatebooleancache=true;// }
spring.thymeleaf.encoding=Shift_JISspring.thymeleaf.cache=falsehttps://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafProperties.java
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
@EnableConfigurationProperties
▸ @ConfigurationProperties
▸ @Component/@Bean
59https://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)@ConfigurationpublicclassThymeleafAutoConfiguration{…
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
▸
▸ @ConfigurationProperties
60
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
61
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
62
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
63
@SpringBootApplicationpublicclassSampleApplication{publicstaticvoidmain(String[]args){SpringApplication.run(SampleApplication.class);}}
@EnableAutoConfiguration@Configuration@ComponentScanpublic@interfaceSpringBootApplication{…
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
64
@EnableAutoConfiguration
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\…
AutoConfiguration
https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
65
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
▸
66
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
@ConditionalOnXxx
▸ @Bean
▸
67
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
ThymeleafAutoConfiguration
68
@Configuration@ConditionalOnClass(TemplateMode.class) @AutoConfigureAfter({WebMvcAutoConfiguration.class,…})publicclassThymeleafAutoConfiguration{…@Configuration@ConditionalOnWebApplication(…)publicstaticclassXxxConfiguration{@Bean@ConditionalOnMissingBean(name=“thymeleafViewResolver”)publicThymeleafViewResolverthymeleafViewResolver(){…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
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
@ConditionalOnClass
▸
@ConditionalOnBean
▸
@ConditionalOnMissingBean
▸69
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
@ConditionalOnProperty
▸
@ConditionalOnWebApplication
▸
@AutoConfigureAfter/@AutoConfigureBefore
▸70
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸ @EnableAutoConfiguration
▸ @ConditionalOnXxx
71
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
72
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
😭
@ConditionalOnMissingBean(Bean_1.class)
@ConditionalOnBean(Bean_2.class)
73
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
Spring Bootのべからず集
一.フレームワークを動かすBeanは、なるべく自前で定義するべからず
二.@EnableXxxはなるべく付けるべからず
74
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸ @EnableXxx
▸
▸
75
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
76
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
77https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
78
@ConfigurationpublicclassSomeConfiguration{privatefinalSomeBeansb;
@Autowired// publicSomeConfiguration(SomeBeansb){this.sb=sb;}
@PostConstruct// publicvoidinit(){sb.setXxx(…);//SomeBean }}
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
79
@ConfigurationpublicclassMyThymeleafConfiguration{@BeanpublicFooDialectfooDialect(){returnnewFooDialect();}@BeanpublicBarDialectbarDialect(){returnnewBarDialect();}}
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸ ThymeleafAutoConfiguration Dialect
TemplateEngine Dialect
80
FooDialectBean
BarDialectBean
&TemplateEngine
Bean &
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
81
@ConfigurationprotectedstaticclassThymeleafDefaultConfiguration{//Dialect Bean DI publicThymeleafDefaultConfiguration(...,ObjectProvider<Collection<IDialect>>dProvider){…}@BeanpublicSpringTemplateEnginetemplateEngine(){SpringTemplateEngineengine=newSpringTemplateEngine();//Dialect this.dialects.forEach(engine::addDialect);returnengine;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
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸ XxxCustomizer
▸
▸
82
@FunctionalInterfacepublicinterfaceJackson2ObjectMapperBuilderCustomizer{
voidcustomize(Jackson2ObjectMapperBuilderbuilder);}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
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
83
@ConfigurationpublicclassMyJacksonConfiguration{@BeanpublicJackson2ObjectMapperBuilderCustomizerjackson2ObjectMapperBuilderCustomizer(){// OKreturnbuilder->{// builder.modules(…).locale(…).indentOutput(…);};}}
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
84
@ConfigurationstaticclassJacksonObjectMapperBuilderConfiguration{@BeanpublicJackson2ObjectMapperBuilder…(List<Jackson2ObjectMapperBuilderCustomizer>customizers){Jackson2ObjectMapperBuilderbuilder=newJackson2ObjectMapperBuilder();for(Jackson2ObjectMapperBuilderCustomizercustomizer:customizers){customizer.customize(builder);}returnbuilder;}}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
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸ FilterRegistrationBean
85
@BeanpublicFilterRegistrationBeanmyFilter(){MyFilterfilter=newMyFilter();FilterRegistrationBeanfrb=newFilterRegistrationBean(filter);// url-patternfrb.addUrlPatterns(“/*”);//
frb.setOrder(Ordered.HIGHEST_PRECEDENCE+10);returnfrb;}
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
▸
▸
▸
86
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
87
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
▸
▸
▸
88
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
▸
▸
89
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
▸
▸
▸
▸
90
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
▸
▸
▸
▸
▸
▸ 91
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸ 😡
▸
▸
92
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
93
(C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
94