Learn Java "Spring Boot" 03-Start Spring Boot basic configuration and project structure

Source: Internet
Author: User

                        <div class="markdown_views">                    

This section contains a brief introduction to:
1. Spring boot configuration file, using @springbootapplication annotations
2. Spring Boot modify Java version and project code
3. A standard spring boot code structure
4. View the current project automatically configured those modules
5. Disable automatic configuration
6. Customizing banner and closing banner

One, Spring boot configuration file, using @springbootapplication annotations

Spring boot defaults to using Application.properties or APPLICATION.YML, placed in the Src/main/resources directory or the Classpath/config, the general recommendation is placed in the src/ Main/resources
Here we use Application.properties to configure, here we try to modify the lower port and access path
The directory structure is as follows:

Configuration code:

server.port=8081server.context-path=/boot
 
   
   
  • 1
  • 2

Writing the test controller class

 PackageCom.likeoak.controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RestController;/** * Test controller * The type test controller. */@RestController Public  class testcontroller {    /** * Returns a string string that is successfully accessed and returns a "test OK" * Test string. * * @return The string * *@RequestMapping("/test") PublicStringTest(){return  "Test ok!"; }}

Start the main method and run the app startup class

package com.likeoak;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/** * 默认启动类 */@SpringBootApplicationpublicclass App{    publicstaticvoidmain( String[] args )    {        SpringApplication.run(App.class,args);    }}

Visit: Http://localhost:8081/boot/test
Result: Test ok!

Code Explanation:
@SpringBootApplication explanation
First look at the source code of the annotated @springbootapplication

The source of @SpringBootApplication

@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documented@Inherited@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan(excludeFilters = {        @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),        @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })public @interface SpringBootApplication {

It contains @springbootconfiguration source code.

@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documented@Configurationpublic @interface SpringBootConfiguration {

Summary: @SpringBootApplication annotations are actually @configuration, @EnableAutoConfiguration, @ComponentScan these three annotation combinations

Annotation Interpretation
@Configuration Note: Indicates that the class uses spring as a Java-based configuration
@EnableAutoConfiguration: Turn on automatic configuration annotations, with this note Spring boot will automatically configure the configuration we need based on the jar package we reference, which is the Spring boot magic.

@ComponentScan: Spring scan annotations, with this annotation spring boot will scan (by default, the root path) all packages, to load all the @bean, all the testcontroller here is scanned, we can access the.

Spring Boot modify Java version and project code

In the process of using spring bootde, to customize the Java configuration, you can use the following configuration to load into the Pom.xml

  <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <java.version>1.8</java.version>    </properties>
A city standard spring boot code structure

A typical spring boot project structure, which is also recommended by the website

com +- example     +- myproject         +- Application.java         |         +- domain         |   +- Customer.java         |   +- CustomerRepository.java         |         +- service         |   +- CustomerService.java         |         +- web             +- CustomerController.java
Iv. view current project automatic configuration of those modules

See what the current project has automatic configuration, a total of three ways
1. Run Jar-jar Xxx.jar–debug directly
2. Setting properties in Application

debug=true
    1. Increase startup parameters directly at startup

We can choose either of these, the results of the visit are as follows
The configuration is started:

Positive matches:

Dispatcherservletautoconfiguration matched:
-@ConditionalOnClass found required class ' Org.springframework.web.servlet.DispatcherServlet '; @ConditionalOnMissingClass did not find unwanted class (onclasscondition)
-@ConditionalOnWebApplication (required) found Standardservletenvironment (onwebapplicationcondition)

Dispatcherservletautoconfiguration.dispatcherservletconfiguration matched:
-@ConditionalOnClass found required class ' javax.servlet.ServletRegistration '; @ConditionalOnMissingClass did not find unwanted class (onclasscondition)
-Default Dispatcherservlet did not find dispatcher servlet beans ( Dispatcherservletautoconfiguration.defaultdispatcherservletcondition)

Dispatcherservletautoconfiguration.dispatcherservletregistrationconfiguration matched:
-@ConditionalOnClass found required class ' javax.servlet.ServletRegistration '; @ConditionalOnMissingClass did not find unwanted class (onclasscondition)
-Dispatcherservlet registration did not find servlet registration bean ( Dispatcherservletautoconfiguration.dispatcherservletregistrationcondition)

....

Not configured automatically:

Negative matches:

ActiveMQAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required classes ‘javax.jms.ConnectionFactory‘, ‘org.apache.activemq.ActiveMQConnectionFactory‘ (OnClassCondition)

AopAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required classes ‘org.aspectj.lang.annotation.Aspect‘, ‘org.aspectj.lang.reflect.Advice‘ (OnClassCondition)

ArtemisAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required classes ‘javax.jms.ConnectionFactory‘, ‘org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory‘ (OnClassCondition)

BatchAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required classes ‘org.springframework.batch.core.launch.JobLauncher‘, ‘org.springframework.jdbc.core.JdbcOperations‘ (OnClassCondition)

...

Five, disable automatic configuration

For example, do not want to automatically configure the database connection, you can use how the code to turn off the automatic configuration

/** * 测试关闭数据库自动配置 * The type Data source config. */@Configuration@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})publicclass DataSourceConfig {}
VI. Custom banner and close banner

To customize the spring boot default boot pattern step:
1. Create a banner.txt directly under the Src/main/resources
2. Visit website Http://patorjk.com/software/taag generate characters, here we use "Yiqixuejava" (Learn Java together), copy the generated characters into Banner.txt, launch the application can

Start Result:

.__         .__                              __ ___.__.|   __| _____|__| ___  _____ __   ____       |__|____ ___  _______< |  ||  | /____ /|  \  \/  /  |  \_ /__  \ | \__  \ \ \/\__  \ \___  | | |  < <_  |  |  |     <|  |  /\  ___/      | |/ __ \\   /  / __ \_ / ____||  __| \__   | __| /__/\_ \____/___ >/\__|          (____  /\_/  (____  / \/              |__|    \/           \/  \______| \/           \/

Follow-up will continue to launch this series of Spring boot articles

Original address: http://blog.csdn.net/javastudyr/article/details/73824894

Learn Java "Spring Boot" 03-Start Spring Boot basic configuration and project structure (GO)

Related Article

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.