Spring Boot Features - Walking Techie

Blog about Java programming, Design Pattern, and Data Structure.

Thursday, June 21, 2018

Spring Boot Features

SpringApplication

This class provides a convenient way to bootstrap a Spring application that is started from a main() method. In many situations, you can delegate to the static SpringApplication.run method, as shown in the following example:

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

When you start your application with spring-boot-starter dependency of spring boot, you should see something similar to the following output:

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.2.RELEASE)

2018-06-14 14:59:54.103  INFO 2638 --- [           main] c.w.t.s.SpringHelloApplication           : Starting SpringHelloApplication on administrator-ThinkPad-L450 with PID 2638 (/home/santosh/Documents/Documents/spring-boot/blogger/spring-hello/target/classes started by santosh in /home/santosh/Documents/Documents/spring-boot/blogger/spring-hello)
2018-06-14 14:59:54.117  INFO 2638 --- [           main] c.w.t.s.SpringHelloApplication           : No active profile set, falling back to default profiles: default
2018-06-14 14:59:54.252  INFO 2638 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3e3047e6: startup date [Thu Jun 14 14:59:54 IST 2018]; root of context hierarchy
2018-06-14 14:59:55.247  INFO 2638 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-06-14 14:59:55.261  INFO 2638 --- [           main] c.w.t.s.SpringHelloApplication           : Started SpringHelloApplication in 2.049 seconds (JVM running for 3.286)
2018-06-14 14:59:55.264  INFO 2638 --- [       Thread-6] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@3e3047e6: startup date [Thu Jun 14 14:59:54 IST 2018]; root of context hierarchy
2018-06-14 14:59:55.266  INFO 2638 --- [       Thread-6] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

By default, INFO logging messages are shown.

External Configuration

Spring Boot lets you externalize your configuration so that you can work with the same application code in different environments. You can use properties files, YAML files, environment variables, and command-line arguments to externalize configuration.

Profiles

Spring profiles provides a way to segregate a part of your application configuration and make it available to only certain enviornments. Any @Component or @Configuration can be marked with @Profile to limit when it is loaded, as shown in the following example:

package com.walking.techie.springhello.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
@Profile("prod")
public class ProductionConfiguration {
    //............some code.........
}

You can use a spring.profiles.active Environment property to specify which profiles are active.
You can make available above configuration code only into "prod" active profile. You can specify the active profiles in application.properties file, as shown below:

spring.profiles.active=prod

Logging

Spring Boot uses Commons Logging for all internal logging but leaves the underlying log implementation open. Default configurations are provided for Java Util Logging, Log4J2, and Logback. In each case, loggers are pre-configured to use console output with optional file output also available.
By default, if you use the “Starters”, Logback is used for logging.
Main log levels are ERROR, WARN, INFO, DEBUG, or TRACE.

The Spring Web MVC Framework

The Spring Web MVC framework often referred as “Spring MVC” is a rich “model view controller” web framework. Spring MVC lets you create special @Controller or @RestController beans to handle incoming HTTP requests.

Embedded Servlet Container Support

Spring Boot includes support for embedded Tomcat, Jetty, and Undertow servers. Most developers use the appropriate “Starter” to obtain a fully configured instance. By default, the embedded server listens for HTTP requests on port 8080.

Working with SQL databases

The Spring Framework provides extensive support for working with SQL databases, from direct JDBC access using JdbcTemplate to complete “object relational mapping” technologies such as Hibernate. Spring Data provides an additional level of functionality: creating Repository implementations directly from interfaces and using conventions to generate queries from your method names.

Working with NO-SQL technologies

Spring Data provides additional projects that help you access a variety of NoSQL technologies, including: MongoDB, Neo4J, Elasticsearch, Solr, Redis, Gemfire, Cassandra, Couchbase and LDAP. Spring Boot provides auto-configuration for Redis, MongoDB, Neo4j, Elasticsearch, Solr Cassandra, Couchbase, and LDAP. You can make use of the other projects, but you must configure them yourself.

Messaging Overview

The Spring Framework provides extensive support for integrating with messaging systems like JMS, ActiveMQ, RabbitMQ etc.

JMS

The javax.jms.ConnectionFactory interface provides a standard method of creating a javax.jms.Connection for interacting with a JMS broker.

Testing Overview

Spring Boot provides a number of utilities and annotations to help when testing your application. spring-boot-starter-test “Starter”, which imports both Spring Boot test modules as well as JUnit, AssertJ, Hamcrest, and a number of other useful libraries.

Testing Spring Boot Applications

Spring Boot provides a @SpringBootTest annotation that can be specified on a test class that runs Spring Boot based tests. Add @RunWith(SpringRunner.class) to your test class so that annotations will not be ignored.

Auto configuration

Spring Boot provides feature to create your own auto-configuration. Auto-configuration classes can be bundled in external jars and still be picked-up by Spring Boot.

Condition Annotations

@Conditional annotations use to override the auto-configuration if you are not happy with your default. Spring Boot includes a number of @Conditional annotations that you can reuse in your own code by annotating @Configuration classes or individual @Bean methods.

1 comment :