spring – 注释扫描不扫描类路径中的外部jar

前端之家收集整理的这篇文章主要介绍了spring – 注释扫描不扫描类路径中的外部jar前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

问题:Spring Component Annotation扫描没有拾取外部jar中注释的类,该类未包含在pom.xml中.但是我需要从外部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”).第三个模块的构建插件

此模块的应用程序上下文xml是

    

我有以下类,这是应用程序的起点

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

我们将第三个模块编译为

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”.

有人可以帮忙吗?

最佳答案
使用-jar选项执行jar文件时,将忽略-cp选项.

-jar选项的Oracle Java docs说:

-jar

Execute a program encapsulated in a JAR file. The first argument is
the name of a JAR file instead of a startup class name. In order for
this option to work,the manifest of the JAR file must contain a line
of the form Main-Class: classname. Here,classname identifies the
class having the public static void main(String[] args) method that
serves as your application’s starting point. See the Jar tool
reference page and the Jar trail of the Java Tutorial for information
about working with Jar files and Jar-file manifests.

When you use this option,the JAR file is the source of all user
classes,and other user class path settings are ignored.

另请查看这篇文章stackoverflow.com/questions/5879925/in-linux-how-to-execute-java-jar-file-with-external-jar-files

因此,您需要使用Class-Path:标头在清单文件中指定metric_process_external.jar.您应该可以使用Maven程序集插件来执行此操作.

如果这不实用,则需要在没有-jar标志的情况下运行应用程序:

java -cp "metric_process_external.jar:main_application-with-dependencies.jar" com.main.TriggerMetricProcessor
原文链接:https://www.f2er.com/spring/432706.html

猜你在找的Spring相关文章