Share SpringBoot good programmers must grasp comment

Foreword

Spring has gone through several stages as follows :

The first stage: xml configuration

In Spring 1.x era, using Spring to develop eyeful xml configuration Bean , 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, with JDK1.5 bring annotation support, the Spring offers declarative Bean annotated (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: the Java configuration

Spring 3.0  introduces based on  Java  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 the  Spring  feature configuration and solutions need to be thinking to switch between business problems, so write configuration crowding out the time to write application logic . Spring Boot  made this happen in the past. Spring Boot  simplifies based Spring application development, just "run" will be able to create a single, production-level Spring applications. Spring Boot to Spring provides out of the box with a set of platform and third-party libraries (provide default settings), so that we can start simple. Most Spring Boot application requires very little Spring configuration. We can use SpringBoot create java applications, and the use of java -jar  start it, or using the traditional war deployment. This is also SpringBoot let one of the main reasons 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 's Java configuration by @Configuration and @Bean achieve these two notes, two notes from Spring3.0 began to have after:

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

2 , @Bean acting on the method, which corresponds xml configuration <the 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)
@SpringBootTest
public class Springboot01ApplicationTests {

  @Autowired
  private User user;
  @Test
  public void contextLoads() {
      System.out.println(user);
  }

}

 

 

print:

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

@SpringBootApplication

@SpringBootApplication注解源码如下:

 

    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是一个复合注解,包括@ComponentScan,和@SpringBootConfiguration@EnableAutoConfiguration

- @SpringBootConfiguration继承自@Configuration,二者功能也一致,标注当前类是配置类,并会将当前类内声明的一个或多个以@Bean注解标记的方法的实例纳入到srping容器中,并且实例名就是方法名。

- @EnableAutoConfiguration的作用启动自动的配置,@EnableAutoConfiguration注解的意思就是Springboot根据你添加的jar包来配置你项目的默认配置,比如根据spring-boot-starter-web ,来判断你的项目是否需要添加了webmvctomcat,就会自动的帮你配置web项目中所需要的默认配置。

- @ComponentScan,扫描当前包及其子包下被@Component@Controller@Service@Repository注解标记的类并纳入到spring容器中进行管理。是以前的<context:component-scan>(以前使用在xml中使用的标签,用来扫描包)。

  

@SpringBootApplication指定扫描某些包

@SpringBootApplication注解默认扫描的是当前类所在的包和子包。自己可以通过属性来设置扫描其他的包,设置了之后默认值就不在有用。

@SpringBootApplication(scanBasePackageClasses = {TestConfig.class,TestController.class})
通过设置scanBasePackageClasses属性来制定,只扫描哪些配置类(@Configuration注解的类)。

或者如下设置:

@SpringBootApplication(scanBasePackageClasses = {Springboot01Application.class,TestConfig.class})
指定扫描这些类所在的包和子包。

设置不自动装配

springboot 的自动配置可以帮我们节省很多时间,但是有时候如果我们不想在引入依赖包的情况自动配置,则可以通过相关设置取消

@SpringBootApplication(exclude = {JpaRepositoriesAutoConfiguration.class, RedisAutoConfiguration.class})注解内部将不需要自动配置的依赖通过exclude参数指定即可,可以指定多个类

所有自动配置的类全都在:org.springframework.boot.autoconfigure包下。只能排出自动配置的类。

@ComponentScan

@ComponentScan注解在Spring的注解中也起到到相当重要的作用,它可以自定义Spring扫描的包,也就是它默认会扫描标注了@Controller@Service@Component以及@Repository注解的类,并实例化这些组件到SpringIOC容器中,它有个配置属性:basePackages,也就是指定扫描的包,如果不知道,它会默认扫描配置了该注解的类的包所在的路径(包括子包)。我们看@SpringBootConfiguration注解的源码中有段代码:

@AliasFor(

 annotation = ComponentScan.class,

 attribute = "basePackages"

)

String[] scanBasePackages() default {};

scanBasePackages属性,指定到了@ComponentScan注解的basePackages属性,所有在SpringBoot中,我们同样可以通过scanBasePackages属性指定包扫描的路径(如不指定,会默认扫描主程序类所在的包路径以及子包下的类):

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

Guess you like

Origin www.cnblogs.com/gcghcxy/p/10974675.html