Springboot装配(配置)方式
一、手动装配
1、 模式注解装配:@Controller、@Service、@Repository等Component注解的拓展,Configruation
2、 Enable模块装配,常用如下:
- @EnableWebMvc WebMvc模式
- @EnableTransaction Management事务管理模块
- @EnableCaching Caching模块
- @EnableAutoConfiguration 自动装配模式
- @EnableManagementContext Actuator管理模块
- @EnableEurekaServer Eureka服务器模块
- @EnableConfigServer 配置服务器模块
- 自定义@Enable模块:
①Import的派生注解
②基于注解驱动实现,可参考@EnableWebMvc注解
③基于接口驱动实现,ImportSelector接口(条件判断注入);参考@EnableCaching注解
3、条件装配
定义:Bean配置的前提条件,如@Profile(底层实际用@Confitional),@Confitional(编程条件配置)
二、手动装配实例
创建Springboot项目,项目结构如下所示:
1、Enable模式装配
工作原理:启动类注入注解,注解中导入配置类Config,而配置类中注入对应的类(实现类或接口),启动该项目时,将配置类内任何内容都能成功注入到项目中(可以是任何位置)
(1)EnableConfiguration形式实例
1> 配置类
@Configuration
public class HelloConfig {
/*
将World类注入进来
*/
@Bean
World world(){
System.out.println("加载HelloConfig!");
return new World();
}
}
2> World类
public class World {
public String say(){
System.out.println("Hello World!");
return "Hello World";
}
}
3> 注解类
//注解相关的配置,以下3行
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
//导入配置类
@Import({HelloConfig.class})
public @interface EnableHello {
}
4> 启动类
@EnableHello //注入EnableHello
//@EnableHelloSelector(isLinux = false)
@RestController
@SpringBootApplication
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class, args);
System.out.println("hello,独爱空城梦!");
}
@GetMapping("/test")
public Object test(){
return "你是认真的吗?";
}
}
5> 项目启动后,结果如下所示:
(2)Selector形式实例
1> 配置类
public class HelloConfigSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
//获取接口原数据
Map<String, Object> metaData = annotationMetadata.getAnnotationAttributes(EnableHelloSelector.class.getName());
Boolean isLinux = (Boolean)metaData.get("isLinux");
//加载多个,并把所有配置初始化
return new String[]{isLinux?HelloConfig.class.getName():HelloCOnfig2.class.getName()};
}
}
2> Config2配置类(因Selector需要两个配置类,另一个)
@Configuration
public class HelloCOnfig2 {
@Bean
World world(){
System.out.println("加载HelloCOnfig2222222!");
return new World();
}
}
3> 注解类
Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import({HelloConfigSelector.class})
public @interface EnableHelloSelector {
boolean isLinux();
}
4> 启动类(上述启动类内修改)
//@EnableHello //注入EnableHello
@EnableHelloSelector(isLinux = false)
5> 项目启动后,结果如下所示:
2、条件装配
1> 注解类
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
@Conditional({SystemCondition.class})
public @interface ConditionOnSystem {
String value();
}
2> Condition类
public class SystemCondition implements Condition {
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
//获取接口原数据
Map<String, Object> metaData = annotatedTypeMetadata.getAnnotationAttributes(ConditionOnSystem.class.getName());
String value = (String)metaData.get("value");
return value.equals("windows");
}
}
3> 配置类(条件判断)
@Configuration
public class HelloCOnfig2 {
@Bean
World world(){
System.out.println("加载HelloCOnfig2222222!");
return new World();
}
//value=“linux”时不会加载,而value=“Windows”时会加载
@ConditionOnSystem(value = "linux/Windows")
@Bean(name = "world2")
World world2(){
System.out.println("加载condition!");
return new World();
}
}
4> 目启动后,结果如下所示:
三、自动装配
自动装配文件spring.factories放置META-INF目录下,不用写注解类等
实现原则
理念:约定大于配置原则
装配:模式装配,@Enable注解装配,条件装配,基于手动装配
步骤:激活自动装配、实现自动装配,配置自动装配
实例:
(1)添加自动装配文件spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.config.HelloConfig
(2)注释启动类注解
//@EnableHello //注入EnableHello
//@EnableHelloSelector(isLinux = false)
(3)项目启动后,结果如下所示: