本系列文章主要为了记录学习SpringBoot的知识点,基本来自慕课网小马哥视频Spring Boot2.0深度实践之核心技术篇
1 Spring模式注解自动装配
任何被 @Component 标准的组件均为组件扫描的候选对象
1.1 装配方式
<context:component-scan>方式,即传统配置xml方式@CompoentScan方式
@ComponentScan(basePackages = "com.imooc.dive.in.spring.boot")
public class SpringConfiguration {
...
}
1.2 自定义模式注解
可以通过自定义注解继承Component注解,继承关系为
ComponentRepositoryFirstLevelRepository
/**
* 一级 {@link Repository @Repository}
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
* @since 1.0.0
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repository
public @interface FirstLevelRepository {
String value() default "";
}
2 Spring @Enable模块装配
@Version Spring Framework 3.1
为了方便组件管理,以模块的形式合并方便统一装配
2.1 注解驱动方式
即将需要装配的类统一加到模块开关注解中
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(DelegatingWebMvcConfiguration.class, other.class)
public @interface EnableWebMvc {
}
在需要装配的类中可以打上@Component("name")用于给类命名,也可以不打,这样只能用类的类型来获取了
2.2 接口编程方式
本质上和2.1类似,只是多引入了一个类用以实现接口方法
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(HelloWorldImportSelector.class)
public @interface EnableHelloWorld {
}
public class HelloWorldImportSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
String[] s = new String[]{DelegatingWebMvcConfiguration.class.getName(),
other.class.getName()};
return s;
}
}
3 Spring条件装配
@Profile 3.1
@Conditional 4.0
3.1 Profile
条件装配可以使用在针对同一接口有多种实现方法,然后在实现方法上打上@Profile("java8"),然后在启动SpringBoot时,调用.profiles("java8")即可启用该装配,注意该方法可以重复调用,这样可以选择多种profile条件。
3.2 Conditional
基于某种更加自定义形式的条件可以用Conditional.首先自定义条件注解类和条件类,然后在条件类中自定义函数,用以判断类是否可以被装配。然后在需要装配的bean中打上自定义注解
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
@Documented
@Conditional(OnSystemPropertyCondition.class)
public @interface ConditionalOnSystemProperty {
String name();
String value();
}
public class OnSystemPropertyCondition implements Condition {
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
Map<String, Object> map = annotatedTypeMetadata
.getAnnotationAttributes(ConditionalOnSystemProperty.class.getName());
String propertyName = String.valueOf(map.get("name"));
String propertyValue= String.valueOf(map.get("value"));
String javaPropertyValue = System.getProperty(propertyName);
return propertyValue.equals(javaPropertyValue);
}
}
@Bean
@ConditionalOnSystemProperty(name = "user.name", value = "Administrator")
String helloWorld() {
return "Hello World";
}
4 SpringBoot自动装配
自动装配集成了上述所有注解方式,可以选择多种也可以不选
- 要想实现自动装配首先需要在启动类中打开自动装配
@EnableAutoConfiguration,注意在一般启动类中@SpringBootApplication已经继承了@EnableAutoConfiguration - 然后在
resources/META-INF/spring.factories中添加所有需要自动装配的类名 - 在类中可以添加想要的bean,也可以在该类上集成其他的注解方式
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.imooc.diveinsprintboot.configuration.HelloWorldAutoConfiguration,\
other name
@Configuration("zzz") //用于给该bean定义名字,可选
@EnableHelloWorld //模块注解 可选
@ConditionalOnSystemProperty(name = "user.name", value = "Administrator") //条件注解 可选
public class HelloWorldAutoConfiguration {
@Bean
public String helloWorld(){
return "Hello World!";
}
}
本文深入探讨SpringBoot的自动装配机制,包括组件扫描、模块装配、条件装配及自动装配的集成,通过实例解析如何利用注解实现灵活的依赖注入。
1015

被折叠的 条评论
为什么被折叠?



