Spring的模式注解装配、@Enable模块装配、条件装配与工厂加载机制

前言:这是在慕课网上学习Spring Boot2.0深度实践之核心技术篇 时所做的笔记,主要供本人复习之用.

目录

第一章 Spirng模式注解装配

1.1 模式注解举例

1.2 装配方式:

1.3 自定义模式注解

第二章 Spring @Enable模块装配

2.1 基于注解驱动

2.2 基于接口驱动实现

第三章 Spirng条件装配

3.1 基于配置方式实现 @Profile

3.2 基于编程方式实现

3.2.1 OnSystemPropertyCondition

3.2.2 ConditioanlOnSystemProperty

3.3.3 应用

第四章 工厂加载机制

4.1 通过EnableAutoConfiguration激活自动装配

4.2 实现自动装配:

4.3 配置到自动化装配文件里:


第一章 Spirng模式注解装配

模式注解一种用于声明在应用中扮演"组件"角色的注解,即标注了这个注解,表明一个类等在应用中扮演组件.

应用指的是Spring或SpringBoot应用,

1.1 模式注解举例

@component作为一种由Spirng容器托管的通用模式组件,任何被@Component标准的组件均为组件扫描的候选对象.类似的,凡是被@Component原标注的注解,如@Service,任何组件标注它时,也将被是做组件扫描的候选对象.

1.2 装配方式:

装配方式:<context:component-scan>(Spring 2.5)或@ComponentScan(Spring 3.1)

1.3 自定义模式注解

模式注解的派生性:

@component,@Repository,@FirstLevelRepository注解,都有value签名,保留了签名的一致性,这就是注解的派生性.

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Indexed
public @interface Component {

	String value() default "";

}
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository {
	@AliasFor(annotation = Component.class)
	String value() default "";
}

 

模式注解的层次性:

我们声明二级注解SecondLevelRepository,我们的SecondLevelRepository派生于FirstLevelRepository,这就是层次性.

@SecondLevelRepository(value = "myFirstLevelRepository") // Bean 名称
public class MyFirstLevelRepository {
}

 我们的@SpringBootAppliacation也是模式注解.

 

第二章 Spring @Enable模块装配

模块装配的举例:

2.1 基于注解驱动

Import可以导入多个类,只不过这里我们只导入了一个.

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(HelloWorldConfiguration.class)
public @interface EnableHelloWorld {
}

 

public class HelloWorldConfiguration {

    @Bean
    public String helloWorld() { // 方法名即 Bean 名称
        return "Hello,World 2018";
    }

}

 完成以上两步后,意味着只要我们EnableHelloWorld的注解标注在某个类上时,我们的Bean就已经存在了.

@E
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值