The @Springbootapplication Musical Note Representative Inward Coffee + Jump Boot

Hello guys, today, we'll larn almost the @SpringBootApplication annotation, i of the most of import annotations from pop Spring Boot framework, which has changed the way Java developers usage Spring framework for writing Java applications. In this article, I'll explicate y'all the important of @SpringBootApplication in addition to it's used inwards a unproblematic Spring Boot application. We usage @SpringBootApplication annotation on our Application or Main degree to enable a host of features e.g. Java-based Spring configuration, element scanning, in addition to inwards detail for enabling Spring Boot's auto-configuration feature.

If y'all induce got been using Spring Boot for a long fourth dimension hence y'all know that before nosotros bespeak to annotate our Application degree or Main degree amongst quite a lot of annotations to get-go amongst e.g.
  1. @Configuration to enable Java-based configuration, 
  2. @ComponentScan to enable element scanning, 
  3. and @EnableAutoConfiguration to enable Spring Boot's auto-configuration feature, 
but straight off y'all tin do all that past times merely annotating your Application degree amongst @SpringBootApplication.

Btw, this annotation is available from Spring 1.2 onwards which way if y'all are running on lower Spring Boot version hence y'all even hence bespeak to usage @Configuration, @CompnentScan, in addition to @EnableAutoConfiguration if y'all bespeak those features.




1. The @SpringBootApplication Example

Here is a unproblematic instance of how to write a Spring Boot application using @SpringBootApplication annotation. This code instance is taken from my before article almost consuming a RESTful spider web service using Spring. In this example, nosotros induce got used RestTempalte degree to swallow a RESTful spider web service.


package tool;  import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.client.RestTemplate;   @SpringBootApplication public class Hello implements CommandLineRunner {    private static terminal Logger log = LoggerFactory.getLogger(Hello.class);    public static void main(String args[]) {     SpringApplication.run(Hello.class);   }    @Override   public void run(String... args) throws Exception {      RestTemplate restTemplate = new RestTemplate();      Country province = restTemplate.getForObject(         "http://www.services.groupkt.com/country/get/iso2code/US",         Country.class);      log.info(country.toString());    }  }

The Main degree serves ii purposes inwards a Spring Boot application: configuration in addition to bootstrapping. First, it's the principal Spring configuration degree in addition to second, it enables the auto-configuration characteristic of Spring Boot application.

If y'all are interested inwards learning to a greater extent than almost essential Spring Boot features e.g. auto-configuration in addition to Starter dependency hence Spring Boot Essentials is a practiced house to larn them quickly.

 i of the most of import annotations from pop Spring Boot framework The @SpringBootApplication Annotation Example inwards Java + Spring Boot





2. @SpringBootApplication = @Configuration + @ComponentScan + @EnableAutoConfiguration

The @SpringBootApplication annotation is a combination of next 3 Spring annotations in addition to provides the functionality of all 3 amongst merely i draw of piece of job of code:

@Configuration
This annotation marks a degree equally a Configuration degree inwards Java-based configuration. This is peculiarly of import if y'all favor Java-based configuration over XML configuration.  If y'all are non familiar amongst Java Based Configuration, See Spring Framework MasterClass - Beginners to Expert to larn essential Spring concepts inwards depth.

 i of the most of import annotations from pop Spring Boot framework The @SpringBootApplication Annotation Example inwards Java + Spring Boot


@ComponentScan
This annotation enables component-scanning hence that the spider web controller classes in addition to other components y'all do volition endure automatically discovered in addition to registered equally beans inwards Spring's Application Context. All the @Controller classes y'all write is discovered past times this annotation.


@EnableAutoConfiguration
This annotation enables the magical auto-configuration characteristic of Spring Boot, which tin automatically configure a lot of materials for you.

For example, if y'all are writing a Spring MVC application in addition to y'all induce got Thymeleaf JAR files on application classpath hence Spring Boot auto-configuration tin automatically configure Thymeleaf template resolver, stance resolver, in addition to other settings automatically.

So, y'all tin state that @SpringBootApplication is a 3-in-1 annotation which combines the functionality of @Configuration, @ComponentScan, in addition to @EnableAutoConfiguration.

It also marks the degree equally a BootStrap degree which way y'all tin runt it equally a normal Java degree e.g. past times running its JAR file from the command prompt equally shown here, or merely correct click in addition to runs a Java computer programme inwards Eclipse IDE.

5 Free Courses to Learn Spring in addition to Spring Boot
  • 10 Tips to locomote past times a ameliorate Java Developer inwards 2018
  • 5 Spring Security Courses to Learn Online
  • 3 Ways to Learn Spring Boot in addition to Spring Cloud
  • 5 Courses to Learn Spring Boot inwards 2018

  • That's all almost reading this article hence far. If y'all similar @SpringBootApplication annotation in addition to my explanation hence delight percentage amongst your friends in addition to colleagues. If y'all induce got whatsoever questions or feedback hence delight drib a note.

    Komentar

    Postingan populer dari blog ini

    How To Fix Invalid Target Release: 1.7, 1.8, 1.9, Or 1.10 Fault Inwards Maven Build

    Top Five Books To Larn Agile Too Scrum For Programmers - Best Of Lot, Must Read

    How To Schedule Leap Professional Person Certification Attempt Using Voucher Online - Pace Past Times Pace Guide