春季 – 在两个其他项目中使用外部jar的异常处理程序

huangapple go评论74阅读模式
英文:

Spring - Using an exception handler from an external jar for 2 other projects

问题

以下是您要翻译的内容:

我有两个项目,它们拥有相同的异常,并使用相同的异常处理程序。
因此,所有的异常和异常处理程序都被移动到一个外部模块。

这些异常被很好地导入了,但是异常处理程序似乎没有被调用。
据我了解,如果我有@ComponentScan注解,处理程序应该会从外部jar中被调用。

以下是我的异常处理程序的样式:

@ComponentScan(basePackages = {"com.*first-project*.controller",
        "com.*second-project*.controller"})
@ControllerAdvice(basePackages = {"com.*first-project*.controller",
        "com.*second-project*.controller"})
public class GlobalExceptionHandler {

...

}

当异常处理程序作为各个项目的一部分时,它运行得很好。那时它只在@ControllerAdvice注解中有该项目的路径,并且没有@ComponentScan注解。

将包含异常处理程序的外部jar目前导入方式如下:

compile files('libs/*jar-name*.jar')
英文:

I have 2 projects that have the same exceptions and use the same exception handler.
So all of the exceptions and the exception handler were moved to an external module.

The exceptions are imported just fine, but the exception handler does not seem to be invoked.
As far as I understand, the handler should be invoked from the external jar if I have the @ComponentScan annotation.

Here's how my exception handler looks like:

@ComponentScan(basePackages = {"com.*first-project*.controller",
        "com.*second-project*.controller"})
@ControllerAdvice(basePackages = {"com.*first-project*.controller",
        "com.*second-project*.controller"})
public class GlobalExceptionHandler {

...

}

The exception handler worked fine when it was part of each project individually. Then it only had that project's path in the @ControllerAdvice annotation and had no @ComponentScan.

The external jar with the exception handler is currently imported with:

compile files('libs/*jar-name*.jar')

答案1

得分: 4

在项目中添加 @Import(GlobalExceptionHandler.class) 可以修复这个问题,现在处理程序已经被正确调用。

英文:

Adding @Import(GlobalExceptionHandler.class) in the projects fixed the issue and the handler is being invoked properly now.

答案2

得分: 0

你应该正确地扫描组件,既包括主项目又包括扩展的JAR包。这个扫描工作应该在主项目(project-1和project-2)中进行。

因此,在 project-1project-2 中,你需要扫描在 jar-name 中找到的包。你可以创建一个单独的配置文件,或者在你的 GlobalExceptionHandler 中进行配置。

@ControllerAdvice(basePackages = {"jar.package"})
public class GlobalExceptionHandler {...}
英文:

You should properly scan the components, both from the main project and from the extended jar. This scanning should be done on the main project(project-1 & project-2).

So, in project-1 & project-2, you have to scan the packages found in jar-name. You can create a separate configuration file or in your GlobalExceptionHandler.

@ControllerAdvice(basePackages = {"jar.package"})
public class GlobalExceptionHandler {...}

答案3

得分: 0

>我之前也遇到了同样的问题,我所做的是:

> 第一步:在GlobalExceptionHandler.java中使用 @ComponenetScan 和 @ControllerAdvice 声明控制器基本包。如下所示:

> @ComponentScan(basePackages = "com.xxx.controller")
> @ControllerAdvice(basePackages = "com.xxx.controller")
> class GlobalExceptionHandler extends ResponseEntityExceptionHandler{}

> 第二步:在 SpringbootMain 应用程序类中使用 @Import 注解导入 GlobalExceptionHandler 类。如下所示:
> @SpringBootApplication
>@Import(CustomExceptionHandler.class)
>public class SpringBootDemoApplication { }

第三步:在其他服务项目中的maven依赖中包含 GlobalExceptionHandler,无论您想在哪里使用它。

英文:

>I was also facing the same issue what I did is:

> Step 1: In GlobalExceptionHandler.java declare controller as base > packages using @ComponenetScan and @ControllerAdvice. Like as below

> @ComponentScan(basePackages = "com.xxx.controller")
> @ControllerAdvice(basePackages = "com.xxx.controller")
> class GlobalExceptionHandler extends ResponseEntityExceptionHandler{}

> Step 2: use @Import annotation and import GlobalExceptionHandler class > >in SpringbootMain Application class. Like as below
> @SpringBootApplication
>@Import(CustomExceptionHandler.class)
>public class SpringBootDemoApplication { }

Step 3: Include GlobalExceptionHandler maven dependency in other service projects wherever you want to use it.

<I>


huangapple
  • 本文由 发表于 2020年10月23日 02:33:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64488481.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定