Spring注解相关

Spring注解

EnableConfigurationProperties

如果@ConfigurationProperties是在第三方包中,那么@component是不能注入到容器的。只有@EnableConfigurationProperties才可以注入到容器。

ConditionalOnClass

image.png

componentscan的作用如下:

@ComponentScan告诉Spring 哪个packages 的用注解标识的类 会被spring自动扫描并且装入bean容器。

例如,如果你有个类用@Controller注解标识了,那么,如果不加上@ComponentScan,自动扫描该controller,那么该Controller就不会被spring扫描到,更不会装入spring容器中,因此你配置的这个Controller也没有意义。

配置文件类比注解

@Configuation等价于<Beans></Beans>
@Bean 等价于<Bean></Bean>
@ComponentScan等价于<context:component-scan base-package="com.dxz.demo"/>
@Component 等价于<Bean></Bean>

@Bean VS @Component

两个注解的结果是相同的,bean都会被添加到Spring上下文中。

  • @Component 标注的是类,允许通过自动扫描发现。@Bean需要在配置类@Configuation中使用。

  • @Component类使用的方法或字段时不会使用CGLIB增强。而在@Configuration类中使用方法或字段时则使用CGLIB创造协作对象
    假设我们需要将一些第三方的库组件装配到应用中或者 我们有一个在多个应用程序中共享的模块,它包含一些服务。并非所有应用都需要它们。

  • 如果在这些服务类上使用@Component并在应用程序中使用组件扫描,我们最终可能会检测到超过必要的bean。导致应用程序无法启动
    但是我们可以使用 @Bean来加载

因此,基本上,使用@Bean将第三方类添加到上下文中。和@Component,如果它只在你的单个应用程序中

获取yml配置文件

/**
对接配置内容
 */
@Data
@ConfigurationProperties(prefix = "insurance")
@Component
public class InsuranceDockingConfig {
    /**
     * 是否打印日志
     * 0:否 1:是
     */
    public String consoleType;
    /**
     * 项目负责人
     */
    public String projectLeader;
    /**
     * 订单负责人
     */
    public String orderLeader;
}

ConditionalOnProperty 注解

1. 简介

Spring Boot源码中大量使用@ConditionalOnProperty来控制Configuration是否生效。

2. 实战

2.1 name的用法

user类

public class User {
}

Dept类

public class Dept {
}

配置类


/**
 * @Author: wgs
 * @Date 2022/2/23 16:02
 * @Classname Config1
 * @Description 当配置文件中只要出现config1.enable时(内容为空 , 同样也是有效的),则该配置生效。
 */
@Configuration
@ConditionalOnProperty(name = "config1.enable")
public class Config1 {
    @Bean
    public User user() {
        User user = new User();
        System.err.println("----------------初始化user--------------");
        return user;
    }
}

/**
 * @Author: wgs
 * @Date 2022/2/23 16:05
 * @Classname Config2
 * @Description 当配置文件中只要出现config1.enable时(内容为空,同样也是有效的),则该配置生效。
 */
@Configuration
@ConditionalOnProperty(name = "config2.enable")
public class Config2 {
    @Bean
    public Dept dept() {
        System.err.println("=============dept初始化========");
        return new Dept();
    }
}

配置文件

config1:
  enable: test

启动项目

2022-02-23 16:24:33.004  INFO 20313 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 459 ms
----------------初始化user--------------
2022-02-23 16:24:33.162  INFO 20313 --- [   

2.2 根据配置开关进行动态加载配置(havingValue)

在上述代码的基础上,我们首先的Config1和Config2中稍作一些修改。

配置类


/**
 * @Author: wgs
 * @Date 2022/2/23 16:02
 * @Classname Config1
 * @Description 当配置文件中只要出现config1.enable时(内容为空 , 同样也是有效的),则该配置生效。
 */
@Configuration
//  当配置文件中config1.enable的值为true才加载,否则就不加载
@ConditionalOnProperty(name = "config1.enable", havingValue = "true")
public class Config1 {
    @Bean
    public User user() {
        User user = new User();
        System.err.println("----------------初始化user--------------");
        return user;
    }
}

/**
 * @Author: wgs
 * @Date 2022/2/23 16:05
 * @Classname Config2
 * @Description 当配置文件中只要出现config1.enable时(内容为空,同样也是有效的),则该配置生效。
 */
@Configuration
//  当配置文件中config1.enable的值为true才加载,否则就不加载
@ConditionalOnProperty(name = "config2.enable", havingValue = "true")
public class Config2 {
    @Bean
    public Dept dept() {
        System.err.println("=============dept初始化========");
        return new Dept();
    }
}

配置文件

config1:
  enable: false
config2:
  enable: true

启动项目

2022-02-23 16:28:38.633  INFO 20439 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-02-23 16:28:38.633  INFO 20439 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 449 ms
=============dept初始化========
2022-02-23 16:28:38.830  INFO 20439 --- [   

2.3 配置缺失加载配置(havingValue)

在上述代码的基础上,我们首先的Config1和Config2中稍作一些修改。

配置类

@Configuration
//  当配置文件中config2.enable,匹配如果缺失加载
@ConditionalOnProperty(name = "config2.enable", havingValue = "true", matchIfMissing = true)
public class Config2 {
    @Bean
    public Dept dept() {
        System.err.println("=============dept初始化========");
        return new Dept();
    }
}

配置文件

config1:
  enable: false

启动项目

2022-02-23 16:28:38.633  INFO 20439 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-02-23 16:28:38.633  INFO 20439 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 449 ms
=============dept初始化========
2022-02-23 16:28:38.830  INFO 20439 --- [   

Bean的加载

在Spring Boot应用中要让一个普通类交给Spring容器管理,通常有以下方法:

1、使用 @Configuration与@Bean 注解

2、使用@Controller @Service @Repository @Component 注解标注该类并且启用@ComponentScan自动扫描

3、使用@Import 方法

其中Spring Boot实现自动配置使用的是@Import注解这种方式,AutoConfigurationImportSelector类的selectImports方法返回一组从META-INF/spring.factories文件中读取的bean的全类名,这样Spring Boot就可以加载到这些Bean并完成实例的创建工作。

2.2.3 自动配置总结
我们可以将自动配置的关键几步以及相应的注解总结如下:

1、@Configuration与@Bean:基于Java代码的bean配置

2、@Conditional:设置自动配置条件依赖

3、@EnableConfigurationProperties与@ConfigurationProperties:读取配置文件转换为bean

4、@EnableAutoConfiguration与@Import:实现bean发现与加载

2.3 自定义starter

本小节我们通过自定义两个starter来加强starter的理解和应用。

2.3.1 案例一

2.3.1.1 开发starter

第一步:创建starter工程hello-spring-boot-starter并配置pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/>
    </parent>
    <groupId>cn.itcast</groupId>
    <artifactId>hello-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
    </dependencies>
</project>

第二步:创建配置属性类HelloProperties

package cn.itcast.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

/*
 *读取配置文件转换为bean
 * */
@ConfigurationProperties(prefix = "hello")
public class HelloProperties {
    private String name;
    private String address;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "HelloProperties{" +
                "name='" + name + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

第三步:创建服务类HelloService

package cn.itcast.service;

public class HelloService {
    private String name;
    private String address;

    public HelloService(String name, String address) {
        this.name = name;
        this.address = address;
    }

    public String sayHello(){
        return "你好!我的名字叫 " + name + ",我来自 " + address;
    }
}

第四步:创建自动配置类HelloServiceAutoConfiguration

package cn.itcast.config;

import cn.itcast.service.HelloService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/*
* 配置类,基于Java代码的bean配置
* */

@Configuration
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
    private HelloProperties helloProperties;

    //通过构造方法注入配置属性对象HelloProperties
    public HelloServiceAutoConfiguration(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }

    //实例化HelloService并载入Spring IoC容器
    @Bean
    @ConditionalOnMissingBean
    public HelloService helloService(){
        return new HelloService(helloProperties.getName(),helloProperties.getAddress());
    }
}

第五步:在resources目录下创建META-INF/spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.itcast.config.HelloServiceAutoConfiguration

至此starter已经开发完成了,可以将当前starter安装到本地maven仓库供其他应用来使用。

2.3.1.2 使用starter

第一步:创建maven工程myapp并配置pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/>
    </parent>
    <groupId>cn.itcast</groupId>
    <artifactId>myapp</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--导入自定义starter-->
        <dependency>
            <groupId>cn.itcast</groupId>
            <artifactId>hello-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

第二步:创建application.yml文件

server:
  port: 8080
hello:
  name: xiaoming
  address: beijing

第三步:创建HelloController

package cn.itcast.controller;

import cn.itcast.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HelloController {
    //HelloService在我们自定义的starter中已经完成了自动配置,所以此处可以直接注入
    @Autowired
    private HelloService helloService;

    @GetMapping("/say")
    public String sayHello(){
        return helloService.sayHello();
    }
}

第四步:创建启动类HelloApplication

package cn.itcast;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloApplication.class,args);
    }
}

执行启动类main方法,访问地址http://localhost:8080/hello/say

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容