Good Java programmer training required to master the notes to share SpringBoot

Good programmers Java Training Share SpringBoot required to master comment

Foreword

Spring has gone through several stages as follows:

The first stage: xml configuration

In Spring 1.x era, using Bean Spring develop eyeful xml configuration, with the expansion of the project, we need to xml configuration file into a different configuration file, then frequently in the development of classes and configuration files switching between

Phase II: Configuration notes

In Spring 2.x era, brought with JDK1.5 annotation support, Spring provides a statement Bean annotation (eg @ Component, @ Service), greatly reducing the amount of configuration. The main way of using the basic configuration of the application (e.g., database configuration) with xml, service configuration with annotations

The third stage: java configuration

Spring 3.0 introduces Java-based configuration capability, which is a type-safe reconfigurable configuration, you can replace XML. We are just at this time, Spring4.x and Spring Boot recommend using Java configuration.

All of these xml configuration represents the loss during development. Because thinking about Spring features and solutions need to be configured to switch between thinking business problems, so write configuration crowding out the time to write application logic. Spring Boot made this happen in the past. Spring Boot simplifies application development based on Spring, just "run" will be able to create a single, production-level applications Spring. Spring Boot provides out of the box set for the Spring platform and third-party libraries (provide default settings), so that we can start simple. Most Spring Boot Spring application requires very little configuration. We can use SpringBoot create java applications, java -jar start using it, or traditional war deployment. This is also one of the main SpringBoot let more and more developers use. Here we have to explain in detail SpringBoot a few important notes: @ Configuration, @ Bean, @ SpringBootApplication, @ ComponentScan


@ The Configuration and @Bean comment

Spring for Java configuration is achieved through @Configuration and @Bean these two notes, both notes is to start with the following Spring3.0:

1, @ Configuration at class, corresponding to an xml configuration file;

2, @ Bean acting on the method, which corresponds xml configuration <bean>;

Case

@Configuration

public class DemoConfigure {

    @Bean

    public User userBean(){

        User user=new User();

        user.setAge(100);

        user.setName("jack");

        user.setPwd("123");

        return user;

    }

}



Tests are as follows:

package com.example.springboot01;​import com.example.springboot01.pojo.User;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;​@RunWith(SpringRunner.class)@SpringBootTestpublic class Springboot01ApplicationTests {​  @Autowired  private User user;  @Test  public void contextLoads() {      System.out.println(user);  }​}



print:

User{name='jack', pwd='123', age=100}

@SpringBootApplication

@SpringBootApplication annotated source code as follows:


    package org.springframework.boot.autoconfigure;

    @Target(ElementType.TYPE)

    @Retention(RetentionPolicy.RUNTIME)

    @Documented

    @Inherited

    @SpringBootConfiguration

    @EnableAutoConfiguration

    @ComponentScan(excludeFilters = {

          @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),

          @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })

    public @interface SpringBootApplication {


    }



@SpringBootApplication is a complex annotations, including @ComponentScan, and @ SpringBootConfiguration, @ EnableAutoConfiguration.

- @SpringBootConfiguration inherited from @Configuration, consistent both functions are denoted by the current class is the class configuration, and will declare the current class to the one or more annotation markers @Bean example of a method of srping into the vessel, and examples name is the method name.

- Role @EnableAutoConfiguration start automatically configured, @ EnableAutoConfiguration annotation means that Springboot to configure the default configuration of your project according to the package you add a jar, for example based on spring-boot-starter-web, to determine whether you need to add a project webmvc and tomcat, will automatically help you configure the default configuration web project needs.

- @ComponentScan, scans the current sub-packet and the packet is marked annotated @ Component, @ Controller, @ Service, @ Repository class and into the spring receptacle for management. Is the previous <context: component-scan> (previously used in xml tag used for scanning the package).


Scanning certain specified packet @SpringBootApplication

@SpringBootApplication annotations are the default scan packages and sub packages where the current class. It can be set to scan other packages through the property, and then set the default value is not in helpful.

@SpringBootApplication (scanBasePackageClasses = {TestConfig.class, TestController.class}) to develop properties by setting scanBasePackageClasses, (@Configuration class annotated) configuration class which only scan.

Or the following settings:

@SpringBootApplication (scanBasePackageClasses = {Springboot01Application.class, TestConfig.class}) where these classes specify the scan sub-packet and packet.

Is not set autowiring

springboot automatic configuration can help us save a lot of time, but sometimes if we do not want to rely on the automatic configuration in the case of the introduction of the package, it can be canceled by the relevant settings

In @SpringBootApplication (exclude = {JpaRepositoriesAutoConfiguration.class, RedisAutoConfiguration.class}) annotation internal unwanted by-dependent automatic configuration parameter specifies to exclude, you can specify a plurality of classes

All in all class automatic configuration: the org.springframework.boot.autoconfigure package. Only discharge type of automatic configuration.

Picture 1

@ComponentScan

@ComponentScan Spring annotation notes also plays the important role that can be customized Spring scan package, that is, it scans by default marked @ Controller, @ Service, @ Component and @Repository annotated classes, and examples of these components to the SpringIOC container, it has a configuration attributes: basePackages, which is designated to scan the package, if not known, it is configured with default scan path type where the annotation packet (including sub-packet). We look @SpringBootConfiguration annotated source code for some of code:

@AliasFor(​ annotation = ComponentScan.class,​ attribute = "basePackages"​)​String[] scanBasePackages() default {};

scanBasePackages attribute, the attribute assigned to the basePackages @ComponentScan annotations, all SpringBoot, we also specify the package scanned by scanBasePackages property path (if not specified, the default path of the scan of the main program where the packet and a class of the sub-packets) :

@SpringBootApplication(scanBasePackages = "com.seagetech.springbootdemo")

Reproduced in: https: //www.jianshu.com/p/2d97d1fd3598

Guess you like

Origin blog.csdn.net/weixin_34218579/article/details/91276127