1How to set up the environment?
Define the configuration:
Add dependency in pom.xml:
(1) Add parent project coordinates
(2) Add web launcher
(3) Manage jdk version
(4) Database driver
(5) General mapper
application.yma placement:
Port, database, entity class (mybatis)
(Whether it is Mybatis, Hibernate is a kind of implementation framework of ORM, both are a kind of encapsulation of JDBC!)
config configuration:
The java configuration mainly relies on java classes and some annotations. The more commonly used annotations are:
@Configuration
: Declare a class as a configuration class instead of the xml file@Bean
: Declare on the method, add the return value of the method to the Bean container instead of the<bean>
label@value
: Property injection@PropertySource
: Specify an external property file,
However, attribute injection uses the @Value annotation. Although this method is feasible, it is not powerful enough because it can only inject basic type values.
In SpringBoot, a new attribute injection method is provided to support the injection of various java basic data types and complex types.
In fact, if only one Bean needs to use a property, we don't need to inject it into a class (JdbcProperties). Instead, you can declare it directly where you need it:
JdbcConfig
JdbcProperties
MvcConfig
Handwritten entity class, service layer, controller
Handwriting start class, define interceptor
2 interceptors?
Definition: not a common attribute, but a class
Role: logging, permission checking, performance monitoring, general behavior
The difference between interceptor and filter:
https://www.cnblogs.com/panxuejun/p/7715917.html
The first difference: The interceptor is based on java's reflection mechanism, while the filter is based on function callbacks.
3 The principle of SpringBoot automatic configuration?
The beginning of all magic comes from our main function. We found two special places:
Annotation: @SpringBootApplication
run method: SpringApplication.run()
@SpringBootConfiguration
Declare that the current class is the configuration class of the SpringBoot application, and there can only be one in the project. Generally do not need to add
@EnableAutoConfiguration
Tell SpringBoot to "guess" how I want to configure Spring based on the dependencies I added
Eg: For example, when we introduce spring-boot-starter-web, this starter will help us add the dependencies of'tomcat' and'SpringMVC'
@ComponentScan
(1) Instructions for configuring component scanning
(2) Specify the package to be scanned through the basePackageClasses or basePackages attribute