spring application of spring boot 2.0 features

spring application of spring boot 2.0 features

spring application of spring boot 2.0 features

mmss 2017-12-29 10:20:01 Browse 42 Comments 0

spring string static class void bean

Abstract: SpringApplication The SpringApplication class provides the convenience of starting a spring application in the main function. It actually runs by calling its static method. The code is as follows: public static void main(String[] args) {SpringApplication.run(MySpringConfiguration.class, args ); }1. By default, its log level is info.

SpringApplication

SpringApplication The class provides the convenience of starting the spring application in the main function. It actually runs by calling its static method. The code is as follows:
public static void main(String[] args) {
	SpringApplication.run(MySpringConfiguration.class, args);
} 
1. By default, its log level is info. If the above startup fails, it will register FailureAnalyzers to obtain a corresponding error message and a suggestion on how to fix it. If there is no analyzable failure analysis and processing exception, you can print more logs by adding the debug attribute: the code is as follows:
java -jar myproject-0.0.1-SNAPSHOT.jar --debug 
2. Custom banner can be set by adding banner.tx at startup or by setting spring.banner.location property to specify its location. You can specify spring.banner.charset as other encoding formats by setting. You can add the spring.banner.image.location image to its designated location. The attributes that can be set in banner.txt include:
  •  ${application.version}
  • ${application.formatted-version}
  • ${spring-boot.version}
  • ${spring-boot.formatted-version}
  • ${Ansi.NAME} (or  ${AnsiColor.NAME}
  • ${AnsiBackground.NAME}${AnsiStyle.NAME})
  • ${application.title}
3. Custom springApplication: you can customize it: the code is as follows:
public static void main(String[] args) {
	SpringApplication app = new SpringApplication(MySpringConfiguration.class);
	app.setBannerMode(Banner.Mode.OFF);
	app.run(args);
} 
4. Use flunent api to create: its code is as follows:
new SpringApplicationBuilder()
		.sources(Parent.class)
		.child(Application.class)
		.bannerMode(Banner.Mode.OFF)
		.run(args); 
5. Application events and listeners: springapplication can send some listener events: some listener events are started before the bean is created, so these events cannot be registered on the bean, you can use SpringApplication.addListeners( ) or  SpringApplicationBuilder.listeners( ) srpingapplication add the configuration location in META-INF/spring.factories : The code is as follows: org.springframework.context.ApplicationListener=com.example.project.MyListener. The sequence of events is as follows:
  • ApplicationStartingEvent : At startup
  • ApplicationEnvironmentPreparedEvent: Before the context is created, when the environment information is used
  • ApplicationPreparedEvent : After the bean is initialized
  • ApplicationReadyEvent : The application is preparing a read request
  • ApplicationFailedEvent : There is an exception at startup
Application events are sent through spring's event publishing mechanism: context can ApplicationContextAware be injected through use . Bean can be used. 6. Web environment variables: can be distinguished by setting setWebEnvironment. 7. Link to application parameters: You can link to the corresponding parameter information by injecting org.springframework.boot.ApplicationArguments: the code is as follows:
import org.springframework.boot.*
import org.springframework.beans.factory.annotation.*
import org.springframework.stereotype.*

@Component
public class MyBean {

	@Autowired
	public MyBean(ApplicationArguments args) {
		boolean debug = args.containsOption("debug");
		List<String> files = args.getNonOptionArgs();
		//if run with "--debug logfile.txt" debug=true, files=["logfile.txt"]
	}

} 
8. If the application runner and commandlinerunner starts running when the springrunner some code: can be achieved  ApplicationRunner with CommandLineRunne
import org.springframework.boot.*
import org.springframework.stereotype.*

@Component
public class MyBean implements CommandLineRunner {

	public void run(String... args) {
		//Do something...
	}

} 
9. exit: hook spring callback System.exit()
@SpringBootApplication
public class ExitCodeApplication {

	@Bean
	public ExitCodeGenerator exitCodeGenerator() {
		return () -> 42;
	}

	public static void main(String[] args) {
		System.exit(SpringApplication
				.exit(SpringApplication.run(ExitCodeApplication.class, args)));
	}

} 
10. admin spring.application.admin.enabled admin




Copyright statement: The content of this article is contributed spontaneously by Internet users, and the copyright belongs to the author. This community does not own the ownership, and does not bear related legal responsibilities. If you find suspected plagiarism in this community, please send an email to: yqgroup@service.aliyun.com to report and provide relevant evidence. Once verified, the community will immediately delete the suspected infringing content.

Use Yunqi Community APP, comfortable~

[Yunqi News] The Central Office of the State Council issued the "Action Plan for Promoting the Large-scale Deployment of Internet Protocol Version 6 (IPv6)" to accelerate the deployment of IPv6-based next-generation Internet. It plans to point out that China s IPv6 scale will reach the world s largest scale by the end of 2025, Ali Cloud also announced for the first time that it will fully provide IPv6 services, so before fully deploying IPV6, you need to know that they are all here   . For details, please click on the comment article ( 0 ) (0) (0)

related articles

Reviews