Sự cố: Quét chú thích thành phần mùa xuân không chọn lớp được chú thích trong bình bên ngoài không được bao gồm trong tệp pom.xml. Nhưng tôi cần phải quét cho các lớp học với chú thích cụ thể từ các lọ bên ngoài. Các lọ bên ngoài này sẽ được đặt trong classpath nhưng sẽ không được biết đến trong ứng dụng của tôi trong suốt thời gian biên dịch.Quét chú thích không quét các lọ bên ngoài trong đường dẫn lớp
1) Chúng tôi có một mô-đun maven (artifactId = "metric_processor") mà tạo ra một tập tin jar (metric_processor.jar) và có các lớp sau
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) Chúng tôi có một mô-đun maven ("artifactId = "metric_processor_external") mà tạo ra một lọ (metric_processor_external.jar) và bao gồm "metric_processor" mô-đun như phạm vi thời gian biên dịch.
package com.metric;
@ProcessMetric(name="TEST_METRIC_EXTERNAL")
@Component
public class TestMetricProcessor implements MetricProcessor {
....
}
3) Chúng tôi có một (chính) maven module thứ ba (artifactId = "main_application") mà là một ứng dụng độc lập (sử dụng mùa xuân) bao gồm es module "metric_processor" trong phạm vi biên dịch. (Nhưng không bao gồm "metric_processor_external"). Việc xây dựng plugin cho các mô-đun thứ ba là
<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>
Application bối cảnh xml cho mô-đun này là
<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>
Tôi có lớp sau đó là điểm khởi đầu của ứng dụng
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());
}
}
}
chúng tôi biên dịch mô-đun thứ ba thành
maven clean install assembly:single
này tạo ra các tập tin jar "main_application-với-dependencies.jar"
Sau đó chúng ta chạy nó như
java -cp "metric_process_external.jar" -jar main_application-with-dependencies.jar
Bây giờ ứng dụng tìm thấy chỉ "LatencyMetricProcessor" và không tìm ra "TestMetricProcessor".
Ai đó có thể giúp bạn không?
chúng tôi sử dụng lò xo 2.5.6 – Thiyanesh