详解 | Spring Boot 核心的 3 个注解详解

Spring Boot 最大的特点是无需 XML 配置文件,能够实现自动装配,并进行全自动化的jar
首页 新闻资讯 行业资讯 详解 | Spring Boot 核心的 3 个注解详解

[[349724]]

本文转载自微信公众号「小明菜市场」,作者小明菜市场 。转载本文请联系小明菜市场公众号。

前言

Spring Boot 最大的特点是无需 XML 配置文件,能够实现自动装配,并进行全自动化的jar包配置。Spring Boot  是微服务的核心,其Spring Cloud 是基于Spring Boot  为基础的。其框架是用来简化Spring应用的初始搭建和开发过程,即,简化了框架,便捷了开发。下面开始介绍Spring Boot 最核心的三个注解

Configuration

在Spring4以后,官方推荐使用 Java Config 来代替 Application.xml 声明将Bean交给容器管理。在Spring Boot  中,Java Config 使用完全代替了application.xml 实现了xml的零配置, 开下面这个例子

实例

创建一个bean类

复制

public class SomeBean {     public void doWork() {         System.out.println("do work...");     } }
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

其中,dowork是逻辑方法 再创建一个Config类

复制

@Configuration public class Config {     @Bean     public SomeBean someBean() {         return new SomeBean();     } }
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

在这里,在Config类上添加了一个@configuration注解,可以理解为Spring中的配置类,其返回值为someBean,someBean方法上也添加了一个@bean注解,其返回对象也将会交由Spring容器进行管理。

简单测试

复制

public class Test {     public static void main(String[] args) {         ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);         SomeBean sb = context.getBean(SomeBean.class);         sb.doWork();     } }
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

这里,创建了一个AnnotationConfigApplicationContext对象,传入了Config.class  后,得到了someBean对象。

do work...

扩展

一般的,一个完整的bean需要包括,id,class,initMethod,destroyMethod,·ref,scope。所以这里使用 Java  Config 进行相关的配置这些属性。修改第一个例子代码

复制

public class SomeBean {      private void init() {         System.out.println("init...");     }      public void doWork() {         System.out.println("do work...");     }      private void destroy() {         System.out.println("destroy...");     }  }
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

增加,init,destroy方法

复制

@Configuration public class Config {      @Bean(initMethod = "init",destroyMethod = "destroy")     public SomeBean someBean() {         return new SomeBean();     } }
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

在bean注解上,属性指向对应的方法。

复制

public class Test {     public static void main(String[] args) {         AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);         SomeBean sb1 = context.getBean(SomeBean.class);         System.out.println(sb1);          SomeBean sb2 = context.getBean(SomeBean.class);         System.out.println(sb2);         context.close();     } }
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

输出结果为

复制

init... com.spring.SomeBean@16022d9d com.spring.SomeBean@16022d9d destroy...
  • 1.

  • 2.

  • 3.

  • 4.

这样就完成了一个配置的生命周期。

@ComponentScan

@ComponentScan注解,用于类或接口上主要指定的扫描路径,Spring会把指定路径下带有指定注解的类自动装配到bean容器里,会被自动装配的注解包括@Controller,@Service,@Component,@Repository等。其作用相当于,

复制

<context:component-scan base-package=”com.maple.learn” />
  • 1.


配置。

基本使用

常用的属性如下  basePackages,value,指定扫描路径,如果为空,则以@ComponentScan注解的类所在的包扫描路径。basePackageClasses:指定具体扫描的类  includeFilters:指定满足Filter条件的类 excludeFilters:指定排除Filter条件的类  includeFilters和excludeFilters 的FilterType可选:ANNOTATION=注解类型  默认、ASSIGNABLE_TYPE(指定固定类)、ASPECTJ(ASPECTJ类型)、REGEX(正则表达式)、CUSTOM(自定义类型),自定义的Filter需要实现TypeFilter接口  @ComponentScan的常见的配置如下:

复制

@ComponentScan(value="com.maple.learn",    excludeFilters = {@ComponentScan.Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class)},    includeFilters = {@ComponentScan.Filter(type=FilterType.ANNOTATION,classes={Controller.class})}         ) public class SampleClass{    …… }
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

@EnableAutoConfiguration

其注解是一个组合注解, 其源码如下

复制

@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @AutoConfigurationPackage @Import(AutoConfigurationImportSelector.class) public @interface EnableAutoConfiguration {      String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";      Class<?>[] exclude() default {};          String[] excludeName() default {};  }
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

其中最重要的是@Import(AutoConfigurationImportSelector.class)注解,借助AutoConfigurationImportSelector,@EnableAutoConfiguration  帮助Spring Boot 应用将所有符合条件的@Configuration 配置加载到IOC容器中,而最主要的还需要借助于 Spring  框架的一个工具类,SpringFactoriestLoader  将META-INF/spring.factories加载配置,spring.factories文件是一个典型的properties配置文件,配置格式为key=value形式,不过key和value都是完整的类名,例如

复制

org.springframework.data.repository.core.support.RepositoryFactorySupport=org.springframework.data.jpa.repository.support.JpaRepositoryFactory
  • 1.