获取给定Springbean的类注释
我有两个自定义注释,如下所述获取给定Springbean的类注释,spring,spring-bean,Spring,Spring Bean,我有两个自定义注释,如下所述 CustomAnnotationMain是基于弹簧组件的注释 CustomAnnotationChild是基于Springbean的注释 下面是使用2个自定义注释的代码段 @CustomAnnotationMain(value = "parent") public class MainClass{ @CustomAnnotationChild(value = "child1") public ObjectBuilder getObject1(
- CustomAnnotationMain是基于弹簧组件的注释
- CustomAnnotationChild是基于Springbean的注释
@CustomAnnotationMain(value = "parent")
public class MainClass{
@CustomAnnotationChild(value = "child1")
public ObjectBuilder getObject1() {
// logic
}
@CustomAnnotationChild(value = "child2")
public ObjectBuilder getObject2() {
// logic
}
}
问题:如何获取所有CustomAnnotationMain注释类以及作为组件一部分可用的所有beans+注释信息的列表
我执行了以下操作,以使用@CustomAnnotationChild对所有bean进行注释。但是我不确定如何访问bean所在的类。我需要访问给定bean的@CustomAnnotationMain
allBuilders = context.getBeansOfType(ObjectBuilder.class);
PS:这不是基于Spring引导的项目。我只使用spring core库。我做了类似的事情。引入了一个可代理的接口,需要查找所有用该接口注释的bean,或者为所有定义的接口创建代理 在您的情况下,应使用CustomAnnotationMain替换Proxyable ClassPathScanningCandidateComponentProvider定义的逻辑可以更改以反映您的筛选器(我只需要这些接口)
public void registerBeanDefinitions(注释元数据元数据,BeanDefinitionRegistry注册表){
调试(“注册@Proxyable bean”);
//获取ProxyableScan注释属性
Map annotationAttributes=metadata.getAnnotationAttributes(ProxyableScan.class.getCanonicalName());
if(annotationAttributes!=null){
String[]basePackages=(String[])annotationAttributes.get(“值”);
if(basePackages.length==0){
//如果未设置value属性,则回退到带注释类的包
basePackages=新字符串[]{((StandardAnnotationMetadata)元数据);
}
//使用这些包,扫描带有Proxyable注释的接口
ClassPathScanningCandidateComponentProvider=新的ClassPathScanningCandidateComponentProvider(false,环境){
//重写isCandidateComponent以仅扫描接口
@凌驾
受保护的布尔值isCandidateComponent(AnnotatedBeanDefinition beanDefinition){
AnnotationMetadata=beanDefinition.getMetadata();
返回metadata.isIndependent()&&metadata.isInterface();
}
};
addIncludeFilter(新的AnnotationTypeFilter(Proxyable.class));
ControllerFactory=getControllerFactory((DefaultListableBeanFactory)注册表);
//扫描所有包
for(字符串basePackage:basePackages){
对于(BeanDefinition BeanDefinition:provider.findCandidateComponents(basePackage)){
试一试{
c类=this.getClass().getClassLoader().loadClass(beanDefinition.getBeanClassName());
如果(!hasImplementingClass(c,basePackages)){
//将跳过创建缺少的bean逻辑
}
}catch(classnotfounde异常){
抛出新的SOAControllerCreationException(“无法为”+beanDefinition.getBeanClassName()创建代理”);
}
}
}
}
}
希望能有所帮助谢谢您的回复。我假设您建议修改我当前的类,以实现一个接口,使其工作。。但不幸的是,我不能这样做(您可以检查
ClassPathScanningCandidateComponentProvider
和受保护的布尔值isCandidateComponent(AnnotatedBeanDefinition beanDefinition)
。AnnotationMetadata
具有方法布尔值hasAnnotation(String annotationName)
和布尔值HasMetaAnnotationName(String metaAnnotationName)
。看起来这是你的案子
public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
LOG.debug("Registering @Proxyable beans");
// Get the ProxyableScan annotation attributes
Map<String, Object> annotationAttributes = metadata.getAnnotationAttributes(ProxyableScan.class.getCanonicalName());
if (annotationAttributes != null) {
String[] basePackages = (String[]) annotationAttributes.get("value");
if (basePackages.length == 0) {
// If value attribute is not set, fallback to the package of the annotated class
basePackages = new String[]{((StandardAnnotationMetadata) metadata).getIntrospectedClass().getPackage().getName()};
}
// using these packages, scan for interface annotated with Proxyable
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false, environment) {
// Override isCandidateComponent to only scan for interface
@Override
protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
AnnotationMetadata metadata = beanDefinition.getMetadata();
return metadata.isIndependent() && metadata.isInterface();
}
};
provider.addIncludeFilter(new AnnotationTypeFilter(Proxyable.class));
ControllerFactory factory = getControllerFactory((DefaultListableBeanFactory) registry);
// Scan all packages
for (String basePackage : basePackages) {
for (BeanDefinition beanDefinition : provider.findCandidateComponents(basePackage)) {
try {
Class c = this.getClass().getClassLoader().loadClass(beanDefinition.getBeanClassName());
if (!hasImplementingClass(c, basePackages)) {
//creating missing beans logic is skipped
}
} catch (ClassNotFoundException e) {
throw new SOAControllerCreationException("cannot create proxy for " + beanDefinition.getBeanClassName());
}
}
}
}
}