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
21. (C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
@Component
21
package hoge.service.impl;
@Component
public class FooServiceImpl
implements FooService {
//
}
※ ( FooService)
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. (C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸ basePackages
@Component
23
hoge
config
service
impl
bar
App
Config
FooService
Impl
Bar
FooService
24. (C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸ AnnotationConfigApplicationContext
▸ ApplicationContext
24
//
//
ApplicationContext context =
new AnnotationConfigApplicationContext(
AppConfig.class);
※ Java Config
25. (C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸ getBean()
25
ApplicationContext context = …;
FooService fs =
context.getBean(FooService.class);
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. (C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸ @Bean
27
@Configuration // @ComponentScan
public class AppConfig2 {
@Bean
public BarService barService() {
//
return new BarServiceImpl();
}
}
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. (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. (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. (C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
▸
▸ @Component
▸ @Bean
31
34. (C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
34
View
Resolver ※
Spring Spring
Dispatcher
Servlet
35. (C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
DispatcherServlet
▸
▸
WebApplicationInitialzer
▸
▸
35
Dispatcher
Servlet
36. (C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
▸
DispatcherServlet
36
37. (C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
ViewResolver
▸
▸ ”employee/index”
”/WEB-INF/views/employee/index.jsp”
▸
▸ InternalResourceViewResolver
▸ ThymeleafViewReseolver
▸ FreeMarkerViewResolver
37
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. (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. (C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
ViewResolver
▸
▸
▸
▸
40
41. (C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
ViewResolver
▸
▸
▸
▸
41
@EnableWebMvc
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. (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. (C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸ @Component
▸ @Controller
▸ @RestController
▸ @Configuration 👈
▸ @Service
▸ @Repository
44
45. (C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
45
@ComponentScan(basePackages =
“hoge.controller”)
@Configuration
public class MvcConfig {
…
}
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. (C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
47
DispatcherServlet
ViewResolver
Bean
Bean
Bean
Bean
48. (C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸ DispatcherServlet
▸
▸ @EnableWebMvc
▸ @Controller @ComponentScan
48
49. (C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
▸
▸
▸
49
https://www.casareal.co.jp/recruit/jobs/
ls_teacher.php
50. (C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
✕
✕
▸
50
https://www.casareal.co.jp/ls/service/shinjinseminar/course01
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
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. (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. (C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸
▸
▸
▸ @ConfigurationProperties
60
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. (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
67. (C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
@ConditionalOnXxx
▸ @Bean
▸
67
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. (C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
@ConditionalOnClass
▸
@ConditionalOnBean
▸
@ConditionalOnMissingBean
▸
69
70. (C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
@ConditionalOnProperty
▸
@ConditionalOnWebApplication
▸
@AutoConfigureAfter/@AutoConfigureBefore
▸
70
71. (C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸ @EnableAutoConfiguration
▸ @ConditionalOnXxx
71
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. (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. (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. (C) CASAREAL, Inc. All rights reserved.
#jjug #ccc_ef3
▸ ThymeleafAutoConfiguration
Dialect
TemplateEngine Dialect
80
FooDialect
Bean
BarDialect
Bean
&Template
Engine
Bean &
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. (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. (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. (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. (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;
}