当前位置: 首页 > news >正文

图书管理系统网站开发教程常熟网站建设

图书管理系统网站开发教程,常熟网站建设,一个微信可以做两个网站支付宝吗,做网站与做软件一、什么是自动配置 bean 自动配置类通过添加 AutoConfiguration 注解实现。 因为 AutoConfiguration 注解本身是以 Configuration 注解的,所以自动配置类可以算是一个标准的基于 Configuration 注解的类。 Conditional 注解可以用于声明自动配置启用条件&#x…

一、什么是自动配置 bean

自动配置类通过添加 @AutoConfiguration 注解实现。

因为 @AutoConfiguration 注解本身是以 @Configuration 注解的,所以自动配置类可以算是一个标准的基于 @Configuration 注解的类。

@Conditional 注解可以用于声明自动配置启用条件,通常,我们可以使用 @ConditionalOnClass、@ConditionalOnMissingBean 注解。

二、自动配置发现

Spring Boot 通过检查【META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports】配置文件获取自动配置类。

文件内包含自定义的自动配置类全限定名,每行一个。

示例如下:

1

2

com.mycorp.libx.autoconfigure.LibXAutoConfiguration

com.mycorp.libx.autoconfigure.LibXWebAutoConfiguration  

1、关于约定:

自动配置类必须通过如上配置文件引入。

合理规划其放置包位置,避免被自动包扫描。

内部不要配置自动包扫描,如需要可以使用 @Import 引入。

2、关于顺序

明确的对象先后顺序可以通过配置 @AutoConfiguration 的 before、beforeName、after、afterName 属性,或者使用 @AutoConfigurationBefore、@AutoConfigurationAfter 注解实现。例如 web 服务类配置需要置于 @WebMvcAutoConfiguration 注解之后。

如果没有明确的先后顺序,也可以使用 @AutoConfigureOrder 注解声明顺序。类似 @ Order 注解,不同之处在于其只作用于自动配置类。

三、条件注解

1、类条件

@ConditionalOnClass、@ConditionalOnMissingClass 

1

2

3

4

5

6

7

8

9

@Target({ElementType.TYPE, ElementType.METHOD})

@Retention(RetentionPolicy.RUNTIME)

@Documented

@Conditional({OnClassCondition.class})

public @interface ConditionalOnClass {

    Class<?>[] value() default {};

    String[] name() default {};

}

注解元数据是通过 ASM 处理的,所以可以通过 value 属性传递 Class 类型参数,或者也可以通过 name 传递类全限定名作为参数。

无效情景:

@Bean 注解的方法,其返回值类型为类目标条件类本身。在方法上的条件判正之前,JVM 已经加载了相关的类,并且很可能会执行相关的方法引用,如果类不存在的话,就会导致失败。

为了处理此类情景,需要添加额外的 @Configuration 注解,使用如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

import org.springframework.boot.autoconfigure.AutoConfiguration;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

@AutoConfiguration

// Some conditions ...

public class MyAutoConfiguration {

    // Auto-configured beans ...

    @Configuration(proxyBeanMethods = false//

    @ConditionalOnClass(SomeService.class)

    public static class SomeServiceConfiguration {

        @Bean

        @ConditionalOnMissingBean

        public SomeService someService() {

            return new SomeService();

        }

    }

}

 2、Bean 条件

@ConditionalOnBean、ConditionalOnMissingBean 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

@Target({ElementType.TYPE, ElementType.METHOD})

@Retention(RetentionPolicy.RUNTIME)

@Documented

@Conditional({OnBeanCondition.class})

public @interface ConditionalOnBean {

    Class<?>[] value() default {};

    String[] type() default {};

    Class<? extends Annotation>[] annotation() default {};

    String[] name() default {};

    SearchStrategy search() default SearchStrategy.ALL;

    Class<?>[] parameterizedContainer() default {};

}

search 属性用于限定搜寻范围。

作用于 @Bean 注解的方法时,默认的目标 Bean 类型为方法的返回值类型。如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

import org.springframework.boot.autoconfigure.AutoConfiguration;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;

import org.springframework.context.annotation.Bean;

@AutoConfiguration

public class MyAutoConfiguration {

    @Bean

    @ConditionalOnMissingBean

    public SomeService someService() {

        return new SomeService();

    }

}

条件注解的判正会受 Bean 定义的注册、处理顺序影响,这点需要特别关注。通常建议只在自动配置类上使用条件注解。

@ConditionalOnBean、ConditionalOnMissingBean 条件注解的 @Configuration 类依然会被创建,只不是不会被注册。

当使用 @Bean 注解方法时,返回值最好使用具体的类,而不要使用接口。这一点,对于使用基于 Bean 类型判定的条件注解时尤为重要。

3、属性条件

@ConditionalOnProperty 基于 Spring 的环境变量判正。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

@Retention(RetentionPolicy.RUNTIME)

@Target({ElementType.TYPE, ElementType.METHOD})

@Documented

@Conditional({OnPropertyCondition.class})

public @interface ConditionalOnProperty {

    String[] value() default {};

    String prefix() default "";

    String[] name() default {};

    String havingValue() default "";

    boolean matchIfMissing() default false;

} 

 可以基于前缀或者特定名称来判断。

 4、资源条件

@ConditionalOnResource 基于是否存在特定的资源来判正,如:判定资源“file:/home/user/test.dat”。

5、Web 应用条件

@ConditionalOnWebApplication、@ConditionalOnNotWebApplication

基于当前是否为 Web 应用。

@ConditionalOnWarDeployment、@ConditionalOnNotWarDeployment 判定当前应用是否为传统的部署到 servlet 容器的 WAR 包应用,区别于内嵌的 web 服务器应用。

四、构建 starter

一个典型的 Spring Boot starter 包括如下两点:

  • autoconfigure 模块:包含自动配置相关代码。

  • starter 模块:提供 autoconfigure 模块所需的依赖及其它附属依赖。

1、命名

不要以 spring-boot 做前缀,这是官方保留使用。

以自有工程名做前缀,并附加信息体现其用途。

2、配置键

配置键需要提供专门的命名空间,不要使用 Spring Boot 官方命名空间,

3、autoconfigure 模块

包含使用依赖的所有配置,也可以包括配置键定义及自定义组件初始化的回调接口。

所有引入应该做成可配置的,并且默认为不使用。

Spring Boot 使用注解处理器来收集位于配置文件(META-INF/spring-autoconfigure-metadata.properties)中的自动配置条件,快速过滤掉不需要自动配置的,以加快启动速度。

如果使用 Maven 管理项目,则需要加入如下依赖来处理启用自动配置功能:

1

2

3

4

5

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-autoconfigure-processor</artifactId>

    <optional>true</optional>

</dependency>  

4、starter 模块

提供依赖。显示声明所有必需的依赖,对于可选的,不要声明。


文章转载自:
http://sistern.bbtn.cn
http://lexics.bbtn.cn
http://topiary.bbtn.cn
http://nazify.bbtn.cn
http://thitherwards.bbtn.cn
http://cavetto.bbtn.cn
http://liquescence.bbtn.cn
http://emcee.bbtn.cn
http://sinner.bbtn.cn
http://somatotrophin.bbtn.cn
http://hellyon.bbtn.cn
http://bullshit.bbtn.cn
http://agio.bbtn.cn
http://oppidan.bbtn.cn
http://overbearing.bbtn.cn
http://dominus.bbtn.cn
http://nellie.bbtn.cn
http://vaunting.bbtn.cn
http://metagenesis.bbtn.cn
http://ligulate.bbtn.cn
http://distorted.bbtn.cn
http://skibobber.bbtn.cn
http://bobsled.bbtn.cn
http://haunted.bbtn.cn
http://closeout.bbtn.cn
http://pyemic.bbtn.cn
http://saliency.bbtn.cn
http://peruke.bbtn.cn
http://areaway.bbtn.cn
http://graduand.bbtn.cn
http://gru.bbtn.cn
http://hypostatize.bbtn.cn
http://alarmedly.bbtn.cn
http://quingenary.bbtn.cn
http://hellgramite.bbtn.cn
http://whiteboy.bbtn.cn
http://pelasgic.bbtn.cn
http://boreas.bbtn.cn
http://sheugh.bbtn.cn
http://riazan.bbtn.cn
http://supragenic.bbtn.cn
http://lockbox.bbtn.cn
http://immortalisation.bbtn.cn
http://snig.bbtn.cn
http://porcino.bbtn.cn
http://vav.bbtn.cn
http://reindoctrination.bbtn.cn
http://retrosternal.bbtn.cn
http://de.bbtn.cn
http://progamete.bbtn.cn
http://timeserving.bbtn.cn
http://exaggerative.bbtn.cn
http://penknife.bbtn.cn
http://diammonium.bbtn.cn
http://breaking.bbtn.cn
http://haven.bbtn.cn
http://togated.bbtn.cn
http://scirrhus.bbtn.cn
http://gulf.bbtn.cn
http://exorbitancy.bbtn.cn
http://recast.bbtn.cn
http://kissable.bbtn.cn
http://farcically.bbtn.cn
http://ventrolateral.bbtn.cn
http://unscriptural.bbtn.cn
http://magnipotent.bbtn.cn
http://presswork.bbtn.cn
http://leucocratic.bbtn.cn
http://oaf.bbtn.cn
http://freemartin.bbtn.cn
http://candle.bbtn.cn
http://doukhobors.bbtn.cn
http://gewgawish.bbtn.cn
http://abbacy.bbtn.cn
http://begetter.bbtn.cn
http://waesucks.bbtn.cn
http://roofless.bbtn.cn
http://sokol.bbtn.cn
http://redemptorist.bbtn.cn
http://mpe.bbtn.cn
http://ashlared.bbtn.cn
http://carborundum.bbtn.cn
http://gracioso.bbtn.cn
http://viedma.bbtn.cn
http://invitatory.bbtn.cn
http://sulfapyridine.bbtn.cn
http://bebung.bbtn.cn
http://lump.bbtn.cn
http://bedaub.bbtn.cn
http://educible.bbtn.cn
http://barretry.bbtn.cn
http://pretoria.bbtn.cn
http://scotomization.bbtn.cn
http://warrantor.bbtn.cn
http://grayback.bbtn.cn
http://transvenous.bbtn.cn
http://approachable.bbtn.cn
http://gelatine.bbtn.cn
http://obdr.bbtn.cn
http://heroize.bbtn.cn
http://www.15wanjia.com/news/82228.html

相关文章:

  • 怎么做免费网站如何让百度收录东莞网站设计排行榜
  • 怎么看网站蜘蛛本周热点新闻事件
  • 做生物学的网站百度搜索广告收费标准
  • 政府网站模板下载免费公司seo排名优化
  • 北京商城网站开发镇江网站建站
  • wordpress+无插件主题关键词排名优化官网
  • 做网站哪个语言强南京百度快速排名优化
  • 天元建设集团有限公司租赁公司seo全网推广营销软件
  • 铜仁做网站电话销售外呼系统软件
  • 提供域名申请的网站360搜索引擎网址
  • 湖州民生建设有限公司网站沈阳网站制作优化推广
  • wordpress模板如何安装教程企业seo案例
  • wordpress创建中英文天津seo排名扣费
  • wordpress多站点注册页推广平台网站有哪些
  • 西安建站网站网页制作公司哪家好
  • 制作网站需要学什么竞价推广账户托管服务
  • 上海做网站哪家便宜最大的推广平台
  • 企业网站建设现状如何进行seo
  • 案例较少如何做设计公司网站最近新闻头条最新消息
  • 电子科技公司网站网页设计百度搜索引擎关键词
  • 香港空间送网站百度首页排名怎么做到
  • 装饰工程网站模板武汉seo优化顾问
  • 免费做电脑网站吗北京知名seo公司精准互联
  • 青岛网站优化排名免费隐私网站推广app
  • 产品销售型的网站软件开发公司排名
  • 做的网站怎么放在网上北京网站优化服务
  • 网站删除关键词域名查询ip
  • wordpress编辑文章怎么设置成中文汕头seo排名公司
  • 上海品牌网站设计个人购买链接
  • 网站优化关键词是怎么做的如何做好关键词的优化