SpringFu Solves Slow Spring Startup

Introduction

1. Innovations in Spring 5.0

<T> void registerBean(String beanName, Class<T> beanClass, Supplier<T> supplier, BeanDefinitionCustomizer... customizers)
  • beanName: the name of Bean
  • beanClass: the registration type of Bean
  • supplier: a key parameter for generating the Bean object
  • customizers: an optional parameter for configuring the generated Bean object
context.registerBean("myService", MyService.class, 
() -> new MyService(), // The supplier parameter for defining the creation method of the Bean object.
(bean) -> bean.setLazyInit(true) // The customizers parameter for configuring Bean object.
);

2. SpringFu vs. SpringMVC

public class Application {
public static void main (String[] args) {
JafuApplication jafu = webApplication(app -> app.beans(def -> def
.bean(DemoHandler.class)
.bean(DemoService.class))
.enable(webMvc(server -> server
.port(server.profiles().contains("test") ? 8181 : 8080)
.router(router -> {
DemoHandler handler = server.ref(DemoHandler.class);
router.GET("/", handler::hello)
.GET("/api", handler::json);
}).converters(converter -> converter
.string()
.jackson()))));
jafu.run(args);
}
}
@Configuration
public class MyConfiguration {
@Bean
public Foo foo() {
return new Foo();
}
@Bean
public Bar bar(Foo foo) {
return new Bar(foo);
}
}
ConfigurationDsl config = beans(def -> def
.bean(Foo.class)
.bean(Bar.class) // Use constructors implicitly to inject other Bean.
)
@Component
public class XxComponent {
// ...
}
@Service
public class YyService {
// ...
}
public class XxComponent {
// ...
}
public class YyService {
// ...
}
beans(def -> def
.bean(XxComponent.class)
.bean(YyService.class)
)
@RestController
@RestController
@RequestMapping("/api/demo")
public class MyController {
@Autowired
private MyService myService;
@GetMapping("/")
public List<Data> findAll() {
return myService.findAll();
}
@GetMapping("/{id}")
public Data findOne(@PathVariable Long id) {
return myService.findById(id);
}
}
public class MyHandler {    private MyService myService;
public MyHandler(MyService myService) {
this.myService = myService;
}
public List<Data> findAll() {
return myService.findAll();
}
public Data findOne(ServerRequest request) {
val id = request.pathVariable("id");
return myService.findById(id);
}
}
router(r -> {
MyHandler handler = server.ref(MyHandler.class);
r.GET("/", handler::findAll)
.GET("/{id}", handler::findOne);
}
router(r -> {
MyService myService = server.ref(MyService.class);
r.GET("/", myService::findAll)
.GET("/{id}", request -> {
return myService.findById(request.pathVariable("id"));
});
}

3. Source Code Interpretation

  • autoconfigure-adapter: public ApplicationContextInitializer object
  • jafu: Java DSL implementation of SpringFu
  • kofu: Kotlin DSL implementation of SpringFu
application(         // Writing. The method can also be webApplication() or reactiveWebApplication().
ApplicationDsl // Configuring
).run() // Running
LoggingDsl(Consumer<LoggingDsl> dsl) {
dsl.accept(this);
}
  • configurationProperties(Class clazz): It is the class of registration attribution configuration equivalent to the @ConfigurationProperties annotation.
  • logging(Consumer dsl): It provides the output log configuration for functions.
  • beans(Consumer dsl): It provides the place to define Beans, which is equivalent to the @Configuration annotation.
  • enable(Consumer configuration): It provides a universal configuration entry with scaling capabilities for other DSL types, such as web listening.
conf -> conf
.beans(...)
.logging(...)
.enable(...);

4. Summary

Original Source:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com