1. 装配Bean
@Configuration
public class BeanConditionConfig {
@Conditional({BizBeanCondition.class})
@Bean
public EnvBean bizEnvBean(){
return new EnvBean("biz");
}
@Conditional({SysBeanCondition.class})
@Bean
public EnvBean sysEnvBean(){
return new EnvBean("sys");
}
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class EnvBean {
private String env;
}
1.1 实现 Condition 接口
- 重写
matches()
, 返回值为 true 时,表示把 Bean 注册到 Spring IOC 容器
public class BizBeanCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Environment environment = context.getEnvironment();
return "Mac OS X".equals(environment.getProperty("os.name"));
}
}
public class SysBeanCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
// 1. 操作系统信息
String osName = context.getEnvironment().getProperty("os.name");
// 2. 注册的bean
BeanDefinitionRegistry registry = context.getRegistry();
String[] beanDefinitionNames = registry.getBeanDefinitionNames();
// 3. BeanFactory
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
// 4. ResourceLoader
ResourceLoader resourceLoader = context.getResourceLoader();
return EnvUtil.Env.DAILY.equals(EnvUtil.getEnv());
}
}
public class EnvUtil {
public static Env getEnv() {
// Get env url
return Env.DAILY;
}
public enum Env {
DAILY,
PRE,
ONLINE
}
}
2. 查看注册的Bean
请求URL http://localhost:8080/getBeanByName?name=bizEnvBean
打印结果 {"env":"biz"}
请求URL http://localhost:8080/getBeanByClass
打印结果 {"bizEnvBean":{"env":"biz"},"sysEnvBean":{"env":"sys"}}
@RestController
public class SpringBeansController {
/**
* 获取所有的bean
*/
@RequestMapping("/getBeans")
public String getBeans() {
String[] beanDefinitionNames = SpringBeanUtils.getApplicationContext().getBeanDefinitionNames();
List beanNames = Arrays.stream(beanDefinitionNames)
.filter(str -> !str.startsWith("org.") && !str.startsWith("spring."))
.collect(Collectors.toList());
return JSON.toJSONString(beanNames, SerializerFeature.PrettyFormat);
}
@RequestMapping("/getBeanByName")
public String getBeanByName(String name) {
EnvBean bean = (EnvBean) SpringBeanUtils.getBean(name);
return JSON.toJSONString(bean);
}
@RequestMapping("/getBeanByClass")
public String getBeanByClass() {
// expected single matching bean but found 2: bizEnvBean,sysEnvBean
// EnvBean bean = SpringBeanUtils.getBean(EnvBean.class);
Map beansOfType = SpringBeanUtils.getBeansOfType(EnvBean.class);
return JSON.toJSONString(beansOfType);
}
}
SpringBeanUtils 工具类
@Component
public class SpringBeanUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringBeanUtils.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static T getBean(Class clazz) {
return applicationContext.getBean(clazz);
}
public static Object getBean(String name) {
return applicationContext.getBean(name);
}
public static Map getBeansOfType(Class clazz) {
return applicationContext.getBeansOfType(clazz);
}
}
【信息由网络或者个人提供,如有涉及版权请联系COOY资源网邮箱处理】
© 版权声明
部分内容为互联网分享,若有侵权请联系站长删除。
THE END
暂无评论内容