What is Spring Boot? What the usage of it and what’s the differences between Spring Boot and Spring? In this blog, we are going to solve those questions by introducing some basic idea.
Project Dependency Structure
First, we can see the projects hierarchy from the pom dependency:
spring-boot-dependencies
# list all dependencies of spring boot: include spring boot jar and some third party jar
spring-boot-parent
spring-boot-starters
# list all starter project, which is a set of convenient dependency descriptor
spring-boot-starter-xxx(data-elastcisearch, web, etc)
# specific tech dependencies
spring-boot-starter-parent
# can include `spring-boot-starter-xxx`
So, what is starter
and boot
?
Boot
Boot is the project to start web application more easily with the help of:
- Embedded tomcat or jetty in
spring-boot-starter-web
- Spring
ApplicationContext
etc
Starter
Starters are a set of convenient dependency descriptors, i.e. pom files or other equivalent files, that we can include in our application. we get a one-stop-shop for all the Spring and related technology that we need without having to hunt through tutorial and copy paste loads of dependency descriptors. For example, if we want to get started using Spring
and Elasticsearch
for searching, we can just include the spring-boot-starter-data-elasticsearch
dependency in our project, and we are ready to go.
But what should be noticed is that, we had better view what the specific starter includes and understand the dependency relationship, otherwise, we will stuck when we need to customize dependency.
Auto Config
Auto-configuration is a very convenient way to reduce the work of declare beans by deducing which beans we might need based on the content of our classpath.
Auto-configuration will always back away when we decided to define our own beans. And this is implemented by ConditionalOnMissingBean
:
ConditionalOnMissingBean
It can be used to define a bean if the exact class (super class not match) match or name match:
Conditional that only matches when the specified bean classes and/or names are not already contained in the BeanFactory.
EnableXXX and XXX
When using some not very common features, we need to use EnableXXX
to let Spring Boot know that we need this feature and customize what this feature needs:
@EnableScheduling
public class ScheduleApplication implements CommandLineRunner {
// only autowired when enable schedule
@Autowired
private ScheduledAnnotationBeanPostProcessor postProcessor;
Mis
After introduce the three main parts of Spring Boot (Boot
, Starter
, Auto Config
), we introduce some other convenient helper.
@Bean
This annotation can be used to annotate a method, which create beans in some type and name. The bean name is default the method name, and this method can also has input, which must be other beans.
Static or Not
As far as I could say, we had better to use static if we can.
The following is an example cited from document:
Special consideration must be taken for @Bean methods that return Spring BeanFactoryPostProcessor (BFPP) types. Because BFPP objects must be instantiated very early in the container lifecycle, they can interfere with processing of annotations such as @Autowired, @Value, and @PostConstruct within @Configuration classes. To avoid these lifecycle issues, mark BFPP-returning @Bean methods as static. For example:
@Bean
public static PropertySourcesPlaceholderConfigurer pspc() {
// instantiate, configure and return pspc ...
}
return type assignable to BeanFactoryPostProcessor.
By marking this method as static, it can be invoked without causing instantiation of its declaring @Configuration class, thus avoiding the above-mentioned lifecycle conflicts. Note however that static @Bean methods will not be enhanced for scoping and AOP semantics as mentioned above. This works out in BFPP cases, as they are not typically referenced by other @Bean methods. As a reminder, a WARN-level log message will be issued for any non-static @Bean methods having a return type assignable to BeanFactoryPostProcessor.
Properties
We can define some environment or properties of project in application.xml
and/or applicatoin.yml
(i.e property file & yaml file’s can be used at the same time.) to customize the auto configured beans or used in our project.
For example, setting properties of Elasticsearch:
spring.data.elasticsearch.cluster-name=searching
@ConfigurationProperties(prefix = "spring.data.elasticsearch")
public class ElasticsearchProperties {
private String clusterName = "elasticsearch";
}
Or setting properties of rabbitmq:
spring.rabbitmq.host = 127.0.0.1
spring.rabbitmq.port = 5672
@ConfigurationProperties(prefix = "spring.rabbitmq")
public class RabbitProperties {
private String host = "localhost";
}
AutoConfig and Bean
If a bean is config in Java by @Bean
, settings in properties about this bean will not work. Because these properties is used with auto config, when beans is created by spring. In other word, we can not use auto config and @Bean
at the same time.
Profile Specfic
java -jar myproject.jar --spring.profiles.active=dev,hsqldb
In addition to application.properties
files, profile-specific properties can also be defined using the naming convention application-{profile}.properties
.
Debug
Debug Model
The default log configuration will echo messages to the console as they are written. By default ERROR, WARN and INFO level messages are logged. we can also enable a “debug” mode by starting our application with a --debug flag.
$ java -jar myapp.jar --debug
Or, we can also specify debug=true
in our application.properties
.
The third way, used in development:
SpringApplication.run(SearchdemoApplication.class, "--debug").close();
Enabling the debug mode does not configure our application to log all messages with DEBUG level ( the list of enabled logger can be found LoggingApplicationListener
).
Log
By default, If we use the Starters
, Logback will be used for logging.
All the supported logging systems can have the logger levels set in the Spring Environment (so for example in application.properties) using logging.level.*=LEVEL
where LEVEL
is one of TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF
.
The logging system is initialized early in the application lifecycle (i.e. before the processing of annotation) and as such logging properties will not be found in property files loaded via @PropertySource
annotations.
Logback Sample Output
2017-06-06 16:56:11.360 WARN 77933 --- [ main] org.elasticsearch.client.transport : [Madame Menace] node {#transport#-1}{192.168.1.100}{192.168.1.100:9300} not part of the cluster Cluster [elasticsearch], ignoring...
Written with StackEdit.
评论
发表评论