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

开发 架构
Spring Boot 最大的特点是无需 XML 配置文件,能够实现自动装配,并进行全自动化的jar包配置。

[[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.

 

责任编辑:武晓燕 来源: 小明菜市场
相关推荐

2024-10-14 17:18:27

2011-04-15 09:44:45

Spring

2023-02-09 08:01:12

核心组件非阻塞

2020-10-28 09:50:33

SpringBootJava

2017-04-26 11:00:34

Spring BootHelloWorld详解

2024-12-16 08:10:00

Spring开发

2025-01-15 08:19:12

SpringBootRedis开源

2024-11-21 14:42:31

2024-08-13 08:41:18

2025-02-28 08:14:53

2024-01-02 07:04:23

2020-06-24 09:35:50

SpringSpring BooJava

2024-11-05 09:25:45

2023-11-06 07:25:51

Spring配置应用程序

2024-01-10 12:26:16

2025-01-03 16:27:35

SpringBoot代码打包

2022-05-25 09:00:00

令牌JWT安全

2024-08-05 08:45:35

SpringKafkaSCRAM

2024-04-03 15:40:14

WebSocketWeb应用Spring

2025-02-27 00:10:19

点赞
收藏

51CTO技术栈公众号