Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Startup time optimization - eliminate expensive bean creations #92

Closed
cslee00 opened this issue Jan 10, 2018 · 2 comments
Closed

Startup time optimization - eliminate expensive bean creations #92

cslee00 opened this issue Jan 10, 2018 · 2 comments

Comments

@cslee00
Copy link

cslee00 commented Jan 10, 2018

Continued analysis of startup time (Spring container, not Boot) shows several unnecessary (default) beans created. These can be avoided by providing a minimal set (instead of relying on defaults).

Unclear if these could/should be rolled into the container (that would be nice as an optional flag) or a documentation issue.

@Configuration
@ComponentScan("mypackage.controller")
public class AppConfig {

    /*
     * Create required HandlerMapping, to avoid several default HandlerMapping instances being created
     */
    @Bean
    public HandlerMapping handlerMapping() {
        return new RequestMappingHandlerMapping();
    }

    /*
     * Create required HandlerAdapter, to avoid several default HandlerAdapter instances being created
     */
    @Bean
    public HandlerAdapter handlerAdapter() {
        return new RequestMappingHandlerAdapter();
    }

    /*
     * optimization - avoids creating default exception resolvers; not required as the serverless container handles
     * all exceptions
     *
     * By default, an ExceptionHandlerExceptionResolver is created which creates many dependent object, including
     * an expensive ObjectMapper instance.
     */
    @Bean
    public HandlerExceptionResolver handlerExceptionResolver() {
        return new HandlerExceptionResolver() {
            @Nullable
            @Override
            public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) {
                return null;
            }
        };
    }
}```
@cslee00
Copy link
Author

cslee00 commented Jan 10, 2018

In addition to reducing startup time, the above config improves security by removing unnecessary components such as BeanNameUrlHandlerMapping.

@sapessi
Copy link
Collaborator

sapessi commented Jan 10, 2018

This are great tips. I'll make sure to include them in the documentation. I'll close this issue for now and refer to the original issue #33.

@sapessi sapessi closed this as completed Jan 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants