前言:这是在慕课网上学习Spring Boot2.0深度实践之核心技术篇 时所做的笔记,主要供本人复习之用.
目录
3.2.1 OnSystemPropertyCondition
3.2.2 ConditioanlOnSystemProperty
4.1 通过EnableAutoConfiguration激活自动装配
第一章 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