gpt4 book ai didi

java - 上下文组件扫描找不到我的自定义注释 bean

转载 作者:行者123 更新时间:2023-11-29 09:30:46 25 4
gpt4 key购买 nike

您好,我正在尝试为 Spring MVC webapp 编写自定义注释。但不知何故,Spring applicationContext 未检测到带注释的类。这是我的相关代码。

注释类:

@Component
@MigrationRequired(migrateFrom="Tiff",migrateTo="PDF-A")
public class WordTemplate extends Template{

}

CustomAnnotationDefinition:

@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MigrationRequired {

String migrateFrom() default "Tiff";
String migrateTo() default "PDF-A";
}

用于检测 ContextLoading 上的注释的类

public class MigrationHandler implements ApplicationContextAware, InitializingBean {

private ApplicationContext applicationContext;

@Override
public void afterPropertiesSet() throws Exception {

final Map<String, Object> myMaps = applicationContext.getBeansWithAnnotation(MigrationRequired.class);
System.out.println("inside after properties set" + myMaps );//**Always giving an empty set**
for (final Object myMap : myMaps.values()) {
final Class<? extends Object> myClass = myMap .getClass();
final MigrationRequired annotation = myClass .getAnnotation(MigrationRequired.class);
System.out.println("Found myClass: " + myClass + ", with tags: " + annotation.migrateFrom());
}
}

@Override
public void setApplicationContext(final ApplicationContext applicationContext)
throws BeansException {
System.out.println("This is called");
this.applicationContext = applicationContext;
}
}

applicationContext.xml 相关配置

<bean id="migrationHandler" class="com.test.annotation.MigrationHandler"/>

<context:component-scan base-package="com.test" >
<context:include-filter type="annotation"expression="com.test.annotation.MigrationRequired"/>
</context:component-scan>
<!--Other beans definition-->

所以在 MigrationHandler afterPropertiesSet() 方法中,我总是将 myMaps 设置为空。难道我做错了什么?谢谢。

最佳答案

MigrationHandler 是一个 spring bean 吗?如果是,为什么不 Autowiring MigrationRequired。

关于java - 上下文组件扫描找不到我的自定义注释 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13528073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com