问题:Spring Component Annotation 扫描未拾取在 pom.xml 中未包含的外部 jar 中注释的类。但我需要从外部 jar 中扫描具有特定注释的类。这些外部 jar 将被放置在类路径中,但在编译期间我的应用程序不会知道。
1)我们有一个maven模块(artifactId="metric_processor"),它产生一个jar文件(metric_processor.jar)并具有以下类
package com.metric;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ProcessMetric {
String name();
}
package com.metric;
public interface MetricProcessor {
int computeMetric();
}
package com.metric;
@ProcessMetric(name="LATENCY")
@Component
public class LatencyMetricProcessor implements MetricProcessor {
.....
}
2) 我们有另一个 maven 模块 ("artifactId="metric_processor_external") 生成一个 jar(metric_processor_external.jar) 并包含 "metric_processor" 模块作为编译时范围。
package com.metric;
@ProcessMetric(name="TEST_METRIC_EXTERNAL")
@Component
public class TestMetricProcessor implements MetricProcessor {
....
}
3)我们有第三个(主)maven模块(artifactId =“main_application”),它是一个独立的应用程序(使用spring),其中包括编译范围内的模块“metric_processor”。(但不包括“metric_processor_external”)。第三个模块的构建插件是
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.main.TriggerMetricProcessor</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
此模块的应用程序上下文 xml 是
<beans>
<context:component-scan base-package="com.metric">
<context:include-filter type="annotation" expression="com.metric.ProcessMetric" />
</context:component-scan>
<bean id="triggerMetricProcessor" class="com.main.TriggerMetricProcessor" />
</beans>
我有以下课程,这是应用程序的起点
package com.main;
import ...
public class TriggerMetricProcessor {
public static void main(String[] args) throws Exception {
ApplicationContext context =
new ClassPathXmlApplicationContext("application-context.xml");
TriggerMetricProcessor triggerMetricProcessor = (TriggerMetricProcessor) context.getBean("triggerMetricProcessor");
triggerMetricProcessor.initMetricProcessor(context);
}
private void initMetricProcessor(ApplicationContext context) {
GenericBeanFactoryAccessor beanFactoryAccessor = new GenericBeanFactoryAccessor(context);
final Map<String, Object> metricProcessors = beanFactoryAccessor.getBeansWithAnnotation(ProcessMetric.class);
for (final Object metricProcessor : metricProcessors.values()) {
final Class<? extends MetricProcessor> metricProcessorClass = (Class<? extends MetricProcessor>)metricProcessor.getClass();
final ProcessMetric annotation = metricProcessorClass.getAnnotation(ProcessMetric.class);
System.out.println("Found MetricProcessor class: " + metricProcessorClass + ", with name: " + annotation.name());
}
}
}
我们将第三个模块编译为
maven clean install assembly:single
这将生成 jar 文件“main_application-with-dependencies.jar”
然后我们运行它
java -cp "metric_process_external.jar" -jar main_application-with-dependencies.jar
现在应用程序只找到“LatencyMetricProcessor”,没有找到“TestMetricProcessor”。
有人可以帮忙吗?