Spring Boot notes and springboot notes

Source: Internet
Author: User

Spring Boot notes and springboot notes

1. if your configuration file is written in the root directory, @ ComponentScan does not require any parameters. All configuration components (@ Component, @ Service, @ Repository, @ Controller, etc) will be automatically registered as Spring Beans.

2. @ SpringBootApplication annotation is equivalent to the use of @ Configuration, @ EnableAutoConfiguration and @ ComponentScan default attributes (you can also customize attributes ):

package com.example.myproject;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScanpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}

 

3. If you want to register listener regardless of the method of application creation, you can add the META-INF/spring. factories file to your project and use the org. springframework. context. ApplicationListerner key.
Example:

org.springframework.context.ApplicationListener=com.example.project.MyListener

 

4. if you want to run some code after SpringApplication is started, you can implement the ApplicationRunner or CommandLineRunner interface. These two interfaces provide a run method, which will be used in SpringApplication. run (...) called before completion.

 

5. SpringApplication will read the application. properties attribute file in the following locations.

* The/config subdirectory in the current directory
* Current Directory
*/Config under classpath
* Root directory


6. default location of static content:/static,/public,/resources,/META-INF/resources


7. default location of the template engine file: src/main/resources/templates (templates under the root directory)


8. default error interface: If the interface is a static interface, the file name is the correct error code, such as 404.html, which is stored in the error directory under the static directory. If it is a dynamic page, it is stored in the error directory under the Dynamic Interface directory and the name is error status code.


9. Custom embedded servlet container Configuration:
Network setting: server. port,
Session setting: server. session. persistence
Server. session. timeout
Server. session. cookie .*
If you want to configure it through programming, You can inherit the EmbeddedServletContainerCustomizer Interface

import org.springframework.boot.context.embedded.*;import org.springframework.stereotype.Component;
@Componentpublic class CustomizationBean implements EmbeddedServletContainerCustomizer { @Override public void customize(ConfigurableEmbeddedServletContainer container) { container.setPort(9000); } }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.