lodsve.redis.core.annotations.RedisBeanDefinitionRegistrar Maven / Gradle / Ivy
/*
* Copyright (C) 2018 Sun.Hao
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package lodsve.redis.core.annotations;
import lodsve.redis.core.connection.RedisDataSourceBeanDefinitionFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;
/**
* redis数据源注册.
*
* @author sunhao([email protected])
* @date 16/1/23 下午11:20
*/
public class RedisBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
private static final String DATASOURCE_NAME_ATTRIBUTE_NAME = "name";
@Override
public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {
AnnotationAttributes attributes = AnnotationAttributes.fromMap(annotationMetadata.getAnnotationAttributes(EnableRedis.class.getName(), false));
String[] names = attributes.getStringArray(DATASOURCE_NAME_ATTRIBUTE_NAME);
for (String name : names) {
if (beanDefinitionRegistry.containsBeanDefinition(name)) {
continue;
}
beanDefinitionRegistry.registerBeanDefinition(name, new RedisDataSourceBeanDefinitionFactory(name).build());
}
}
}