十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章主要介绍“Sring Boot怎么自动装配”,在日常操作中,相信很多人在Sring Boot怎么自动装配问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Sring Boot怎么自动装配”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

成都创新互联公司是专业的民权网站建设公司,民权接单;提供网站制作、成都网站设计,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行民权网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
@sevice @Conrtroller @Repository @Component
这几个注解在Srping源码的文章中已经将结果了,这里就不在赘述了。
Condition 注解作为条件,如果符合条件则将bean注入到IOC中,反之则不注入,实际是使用 @Conditional注解来实现,继承 Condition 接口,通过 matches 方法进行逻辑判断是否符合条件。
1: 创建ConditionOnSysProperty注解
/**
* @Auther: lantao
* @Date: 2019-07-18 17:52
* @Company: 随行付支付有限公司
* @maill: lan_tao@suixingpay.com
* @Description: TODO
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Documented
@Conditional({SysPropertyCondition.class})
public @interface ConditionOnSysProperty {
String value() default "lantao";
}2:创建@Condtional所需要的条件 SysPropertyCondition
/**
* @Auther: lantao
* @Date: 2019-07-18 17:52
* @Company: 随行付支付有限公司
* @maill: lan_tao@suixingpay.com
* @Description: TODO
*/
public class SysPropertyCondition implements Condition {
/**
* 匹配方法
*
* @param context
* @param metadata
* @return
*/
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
// 获取 ConditionOnSysProperty 注解的属性
Map attributes = metadata.getAnnotationAttributes(ConditionOnSysProperty.class.getName());
String value = String.valueOf(attributes.get("value"));
// 获取本机的user.name值
String propertieValue = System.getProperties().get("user.name").toString();
// 对比
return value.equals(propertieValue);
}
} 3:创建COnditionBootStrap测试
/**
* @Auther: lantao
* @Date: 2019-07-18 17:44
* @Company: 随行付支付有限公司
* @maill: lan_tao@suixingpay.com
* @Description: TODO
*/
public class ConditionBootStrap {
@Bean
// 这了因为value是lantao, 正好user.name也是lantao,所以条件成立,会将bean注入到ioc中
@ConditionOnSysProperty(value = "lantao")
private String helloWorld() {
return "Hello World ! Condition";
}
public static void main(String[] args) {
// 这种方式可以不使用 SpringBootApplication 注解
ConfigurableApplicationContext context = new SpringApplicationBuilder(ConditionBootStrap.class).run(args);
// 获取名为 helloWorld 的Bean,判断 ConditionOnSysProperty 条件是否生效
String helloWorld = context.getBean("helloWorld", String.class);
System.out.println(helloWorld);
// 关闭
context.close();
}
}
结果:
Hello World ! ConditionEanble注解内部使用@Import将 bean注入到ioc中,@import注解中可以直接放入bean,也可以做更灵活的配置,使用继承了ImportSeletor接口的bean,可以根据@Enable注解的属性(attrbutes)进行灵活的动态判断
1: 创建 EnableHelloWorld
/**
* @Auther: lantao
* @Date: 2019-07-18 18:03
* @Company: 随行付支付有限公司
* @maill: lan_tao@suixingpay.com
* @Description: TODO
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
// 直接使用无法灵活判断
//@Import(TestConfiguration.class)
// 使用 HelloWorldImportSeletor 可以有更灵活的判断
@Import(HelloWorldImportSeletor.class)
public @interface EnableHelloWorld {
String name() default "lantao";
}2: 创建TestConfiguration和Test1Configuration
public class TestConfiguration {
@Bean
private String helloWorld() {
return "hello World! Enable";
}
}
public class Test1Configuration {
@Bean
public String buzhidao() {
return "不知道";
}
}3: 创建HelloWorldImportSeletor
/**
* @Auther: lantao
* @Date: 2019-07-19 09:55
* @Company: 随行付支付有限公司
* @maill: lan_tao@suixingpay.com
* @Description: TODO
*/
public class HelloWorldImportSeletor implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
// 获取EnableHelloWorld注解的属性
Map attributes = importingClassMetadata.getAnnotationAttributes(EnableHelloWorld.class.getName());
// 根据attributes 灵活判断注入那个bean
if ("lantao".equals(attributes.get("name"))) {
System.out.println(TestConfiguration.class.getName());
return new String[]{TestConfiguration.class.getName()};
}
System.out.println(TestConfiguration.class.getName());
return new String[]{Test1Configuration.class.getName()};
}
} 4:创建bootStrap测试
/**
* @Auther: lantao
* @Date: 2019-07-18 18:04
* @Company: 随行付支付有限公司
* @maill: lan_tao@suixingpay.com
* @Description: TODO
*/
@EnableHelloWorld
//@EnableHelloWorld(name = "buzhidao")
public class EnableBootStrap {
public static void main(String[] args) {
// 这种方式可以不使用 SpringBootApplication 注解
ConfigurableApplicationContext context = new SpringApplicationBuilder(EnableBootStrap.class).run(args);
// 获取名为 helloWorld 的Bean,判断 ConditionOnSysProperty 条件是否生效
String helloWorld = context.getBean("helloWorld", String.class);
// 这里可以获取 bean名称为 'buzhidao',需要注解@EnableHelloWorld(name = "buzhidao"),
// 因为@EnableHelloWorld的name默认值是lantao,符合Condition的条件判断
// String helloWorld = context.getBean("buzhidao", String.class);
System.out.println(helloWorld);
// 关闭
context.close();
}
}
结果:
hello World! Enable自定义spring.factories,工厂模式装配可以自定义starter。
1: 创建SpringFactoriesConfiguration
/**
* @Auther: lantao
* @Date: 2019-07-22 10:10
* @Company: 随行付支付有限公司
* @maill: lan_tao@suixingpay.com
* @Description: TODO
*/
@EnableHelloWorld
@ConditionOnSysProperty(value = "lantao")
@Configuration
public class SpringFactoriesConfiguration {
// 这里没有写 bean 的注入,直接引用 Condition 和 eanble 模块注解。
// eanble注解内部使用import将bean注入到ioc中,@import注解中可以直接放入bean,也可以做更灵活的配置使用继承了ImportSeletor接口的bean,
// 可以根据enable注解的属性(attrbutes)进行灵活的动态判断
// Condition 注解作为条件,如果符合条件则将bean注入到IOC中,反之则不注入,实际是使用 @Conditional注解来实现,通过继承 Condition 接口,
// 通过 matches 方法进行逻辑判断是否符合条件。
}2: 创建 META-INF/spring.factories
# Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.lantao.springboot_leran.spring_factories_leran.config.SpringFactoriesConfiguration
3: 创建 SpringFactoriesBootStrap 引导类
/**
* @Auther: lantao
* @Date: 2019-07-19 16:01
* @Company: 随行付支付有限公司
* @maill: lan_tao@suixingpay.com
* @Description: TODO
*/
@EnableAutoConfiguration
public class SpringFactoriesBootStrap {
public static void main(String[] args) {
ConfigurableApplicationContext context = new SpringApplicationBuilder(SpringFactoriesBootStrap.class).run(args);
String helloWorld = context.getBean("helloWorld", String.class);
System.out.println(helloWorld);
context.close();
}
}
结果:
hello World! Enable到此,关于“Sring Boot怎么自动装配”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!