The @Springbootapplication Notation Event Inwards Coffee + Saltation Boot

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

If you lot cause got been using Spring Boot for a long fourth dimension in addition to hence you lot know that before nosotros involve to annotate our Application shape or Main shape amongst quite a lot of annotations to kickoff amongst e.g.
  1. @Configuration to enable Java-based configuration, 
  2. @ComponentScan to enable constituent scanning, 
  3. and @EnableAutoConfiguration to enable Spring Boot's auto-configuration feature, 
but instantly you lot tin sack exercise all that past times but annotating your Application shape amongst @SpringBootApplication.

Btw, this annotation is available from Spring 1.2 onwards which agency if you lot are running on lower Spring Boot version in addition to hence you lot even hence involve to utilisation @Configuration, @CompnentScan, in addition to @EnableAutoConfiguration if you lot involve those features.




1. The @SpringBootApplication Example

Here is a unproblematic event of how to write a Spring Boot application using @SpringBootApplication annotation. This code event is taken from my before article most consuming a RESTful spider web service using Spring. In this example, nosotros cause got used RestTempalte shape to eat 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 concluding 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 shape serves ii purposes inwards a Spring Boot application: configuration in addition to bootstrapping. First, it's the primary Spring configuration shape in addition to second, it enables the auto-configuration characteristic of Spring Boot application.

If you lot are interested inwards learning to a greater extent than most essential Spring Boot features e.g. auto-configuration in addition to Starter dependency in addition to hence Spring Boot Essentials is a skillful house to acquire 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 but i draw of piece of occupation of code:

@Configuration
This annotation marks a shape every bit a Configuration shape inwards Java-based configuration. This is peculiarly of import if you lot favor Java-based configuration over XML configuration.  If you lot are non familiar amongst Java Based Configuration, See Spring Framework MasterClass - Beginners to Expert to acquire 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 you lot exercise volition endure automatically discovered in addition to registered every bit beans inwards Spring's Application Context. All the @Controller classes you lot write is discovered past times this annotation.


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

For example, if you lot are writing a Spring MVC application in addition to you lot cause got Thymeleaf JAR files on application classpath in addition to hence Spring Boot auto-configuration tin sack automatically configure Thymeleaf template resolver, sentiment resolver, in addition to other settings automatically.

So, you lot tin sack tell that @SpringBootApplication is a 3-in-1 annotation which combines the functionality of @Configuration, @ComponentScan, in addition to @EnableAutoConfiguration.

It also marks the shape every bit a BootStrap shape which agency you lot tin sack runt it every bit a normal Java shape e.g. past times running its JAR file from the command prompt every bit shown here, or but correct click in addition to runs a Java programme inwards Eclipse IDE.

5 Free Courses to Learn Spring in addition to Spring Boot
  • 10 Tips to buy the farm a improve 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 most reading this article hence far. If you lot similar @SpringBootApplication annotation in addition to my explanation in addition to hence delight percentage amongst your friends in addition to colleagues. If you lot cause got whatever questions or feedback in addition to hence delight driblet a note.


    Sumber https://javarevisited.blogspot.com/

    0 Response to "The @Springbootapplication Notation Event Inwards Coffee + Saltation Boot"

    Post a Comment

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel