問題:Spring組件註釋掃描不會拾取未包含在pom.xml中的外部罐子中註釋的類。但我需要掃描具有來自外部罐子的特定註釋的類。這些外部jar將被放置在類路徑中,但在編譯期間不會被我的應用程序知道。註釋掃描不掃描類路徑中的外部罐子
1)我們有一個行家模塊(的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)我們有另一個行家模塊(「的artifactId = 「metric_processor_external」),它產生一個罐(metric_processor_external.jar),並且包括 「metric_processor」 模塊編譯時間範圍。
package com.metric;
@ProcessMetric(name="TEST_METRIC_EXTERNAL")
@Component
public class TestMetricProcessor implements MetricProcessor {
....
}
3)我們有三分之一(主)行家模塊(的artifactId = 「main_application」),其是一個獨立的應用程序(使用彈簧),其中包括es模塊「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與 - dependencies.jar」
然後我們運行它作爲
java -cp "metric_process_external.jar" -jar main_application-with-dependencies.jar
現在,應用程序只能找到「LatencyMetricProcessor」,不找到「TestMetricProcessor」。
有人可以幫忙嗎?
我們使用的彈簧2.5.6 – Thiyanesh 2013-05-12 09:52:29