Sorun: Yay Bileşeni Ek açıklama taraması, pom.xml dosyasında bulunmayan dış kavanozda açıklanmış olan sınıfı seçmiyor. Ama dış kavanozlardan belirli bir açıklama ile sınıfları taramak gerekiyor. Bu harici kavanozlar sınıf yoluna yerleştirilecek ancak derleme sırasında başvurumla bilinmeyecek.Notlama taraması sınıf yolunda harici kavanozları taramıyor
1) Bir kavanoz dosyası (metric_processor.jar) üretir ve sahip olan bir maven modülü (artifactId = "metric_processor") aşağıdaki sınıfların
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 {
.....
}
) Biz (başka bir Maven modülüne sahip olması "artifactId = bir kavanoz (metric_processor_external.jar) üretir ve derleme zaman kapsamı olarak "metric_processor" modülü içerir) "metric_processor_external".
package com.metric;
@ProcessMetric(name="TEST_METRIC_EXTERNAL")
@Component
public class TestMetricProcessor implements MetricProcessor {
....
}
3) üçüncü bir (ana) maven modülü (artifactId = "main_application" sahip) olan Dahil olan tek başına bir uygulamadır (yay kullanır) derleme kapsamındaki "metric_processor" es modülü. (Ancak "metric_processor_external" dahil değildir). Üçüncü modül için yapı eklentisi, bu modül için
<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>
uygulama bağlamında 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>
I uygulama
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());
}
}
}
başlangıç noktası olduğu aşağıdaki sınıfı olduğu biz üçüncü modülü
olarak derleyinBu "main_application-ile-dependencies.jar" kavanoz dosyası
Sonra çalıştırmak üreten onun
java -cp "metric_process_external.jar" -jar main_application-with-dependencies.jar
Şimdi uygulama yalnızca "LatencyMetricProcessor" bulur ve "TestMetricProcessor" bulamazsa olarak.
Birisi yardım edebilir mi?
Kullandığımız bahar 2.5.6 – Thiyanesh