In the previous article "developing your first SpringBoot application", you have a general understanding of the basic development process of SpringBoot. This article will continue to study the official website of SpringBoot and find more details about the development of SpringBoot.
Dependency management
Maven or Gradle are recommended for dependency management. Other tools such as Ant and SpringBoot do not support them very well.
SpringBoot provides many starters to add dependencies in batches, such as spring boot starter web. The officially defined starters are spring boot starter - * named by org springframework. Boot provides:
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using.build-systems.starters
The customized third-party starter naming method often puts the name in front, like this: third party project spring boot starter.
Code directory
The official recommended code directory is as follows:
com +- example +- myapplication +- MyApplication.java | +- customer | +- Customer.java | +- CustomerController.java | +- CustomerService.java | +- CustomerRepository.java | +- order +- Order.java +- OrderController.java +- OrderService.java +- OrderRepository.java
- The package name is com example. Myapplication this way.
- main start the entry class myapplication Java is placed in the root directory to find other files.
- Other modules are similar to myapplication Java juxtaposition.
to configure
Try not to use xml for configuration, but define a configuration class with @ configuration annotation. If you have to use xml, you can use @ ImportResource to Import. Configuration classes do not need to be written as one. Multiple configuration classes can use multiple @ configuration or split into multiple @ Import.
xml has been tucking away many people, but SpringBoot make complaints about using xml.
@The SpringBootApplication annotation already contains the @ EnableAutoConfiguration annotation, which will automatically import the configuration. For example, there is an HSQLDB in the classpath. If the database connection is not manually configured, SpringBoot will automatically configure it. Generally, you only need to add @ SpringBootApplication. There is no need to add @ EnableAutoConfiguration again.
By adding -- debug when starting the application, you can view which automatic configuration classes are available or remove them, such as:
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class }) public class MyApplication { }
You can also use excludeName or spring autoconfigure. The exclude property to specify the autoconfiguration class to remove.
Dependency injection
SpringBoot can add @ ComponentScan to automatically find beans. For example, @ Component, @ Service, @ Repository, @ Controller will be automatically registered as Spring Beans@ SpringBootApplication already contains @ ComponentScan, so there is no need to add it repeatedly.
For example, @ Service uses a constructor to contain a bean:
@Service public class MyAccountService implements AccountService { private final RiskAssessor riskAssessor; public MyAccountService(RiskAssessor riskAssessor) { this.riskAssessor = riskAssessor; } // ... }
If there are multiple constructors, you can use @ Autowired to let Spring inject automatically:
@Service public class MyAccountService implements AccountService { private final RiskAssessor riskAssessor; private final PrintStream out; @Autowired public MyAccountService(RiskAssessor riskAssessor) { this.riskAssessor = riskAssessor; this.out = System.out; } public MyAccountService(RiskAssessor riskAssessor, PrintStream out) { this.riskAssessor = riskAssessor; this.out = out; } // ... }
@SpringBootApplication
Generally, there is this annotation on the main method, which actually contains three annotations:
- @EnableAutoConfiguration
- @ComponentScan
- @SpringBootConfiguration
@SpringBootApplication // same as @SpringBootConfiguration @EnableAutoConfiguration // @ComponentScan public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } }
If you only want to include some of these annotations, you can not use @ SpringBootApplication:
@SpringBootConfiguration(proxyBeanMethods = false) @EnableAutoConfiguration @Import({ SomeConfiguration.class, AnotherConfiguration.class }) public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } }
Start mode
① IDEA uses Run as Java Application.
② Start the jar package from the command line.
$ java -jar target/myapplication-0.0.1-SNAPSHOT.jar
Open remote mode
$ java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n \ -jar target/myapplication-0.0.1-SNAPSHOT.jar
③maven
$ mvn spring-boot:run
reference material: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using