Comments (9)
I agree with you, @sinanoezdemir , about your note on the Javadoc of ControllerAdviceBean (in the beginning of your comment). I, too, find the exact match between your statements and the Javadoc statements. That should be altered.
However, pardon me for not diving deep into your usecase and context of micro services.
from spring-framework.
Related Issues
from spring-framework.
Hello @sinanoezdemir , thanks for bringing this up. We have discussed this today as a team and I'll summarize here.
We're not sure if you don't want your shared library to be scanned for components, or if it's scanned by micro-services but you would like to make this bit optional.
- If you don't want your library to be scanned by mistake, this is a quite common concer. Spring Boot is contributed numerous configuration classes that should never be scanned. If an application scans the
org.springframework.boot
namespace, this is considered as an invalid setup. There might be ways to explicitly reject such situations and tell the user. - If your library is scanned but you would like to optionally contribute this component, there are many ways to express that with Spring: profiles, conditions, functional registration of beans...
Are your applications using Spring Boot? If so, you could contribute your own auto-configurations and never rely on scanning for your library components. Your classes would still be annotated with @ControllerAdvice
but could be conditionally contributed to the application context.
Now depending on the outcome of this discussion, we might revisit the ControllerAdviceBean
javadoc and part of its implementation: this is a public type that needs to be shared for Spring MVC and WebFlux, but it's not meant for public use. As you've found out, non-annotated @ControllerAdvice
instances are not supported at the moment and we could streamline the current implementation to reflect that as it's only partially there.
from spring-framework.
@bclozel thanks for your response.
I just mentioned this issue because of what you sad in your last sentence
As you've found out, non-annotated @ControllerAdvice instances are not supported at the moment
In our team we try to avoid relying on component scanning for libraries. Instead we want to manually configure the beans within the services and use the libraries like a blue print for these kind of things. In this case exception handling.
I have a working workaround but I think it would be great if it would be possible to register ControllerAdvices by annotation and programmatically. Off course I understand that this is probably easier sad then done.
from spring-framework.
In our team we try to avoid relying on component scanning for libraries. Instead we want to manually configure the beans within the services and use the libraries like a blue print for these kind of things. In this case exception handling.
Thanks for your response.
I have a working workaround but I think it would be great if it would be possible to register ControllerAdvices by annotation and programmatically. Off course I understand that this is probably easier sad then done.
I don't think that pointing to a class would really qualify as programmatic registration. This class would still contain @ExceptionHandler
method and would still be annotated-driven. You could look at how we handle programmatic registration of mappings for controllers in Spring MVC. As you can see, there is no annotation involved here anymore.
Are your applications using Spring Boot? If so, you could contribute your own auto-configurations and never rely on scanning for your library components. Your classes would still be annotated with @ControllerAdvice but could be conditionally contributed to the application context.
The auto-configuration approach would be more aligned with your vision of programmatic registration in this case. Have you considered this?
from spring-framework.
I have the same concerns。
1、common-spring.jar
@RestControllerAdvice(basePackages="com.a") class GlobalExceptionHandler {}
org.springframework.boot.autoconfigure.AutoConfiguration.imports
xxx.xxx.GlobalExceptionHandler
2、app-1
app package
com.a.xxxxConotroller
pom.xml
maven import common-spring.jar
GlobalExceptionHandler is useful
2、app-2
app package
com.c.controller
pom.xml
maven import common-spring.jar
GlobalExceptionHandler is useless
We may no need to package a new common-spring.jar just only modify the basePackages
from spring-framework.
@qzmer1104 so you are looking for something like this, but for exception handling?
Have you considered custom auto-configurations?
from spring-framework.
Thank you, @bclozel, for your responses.
I agree that @ExceptionHandler and @configuration would still be necessary, but that is acceptable. My main goal was to avoid relying on component scanning. With the configuration class, you have to register the bean manually, and the library containing the exception handlers does not create beans by itself. We try to avoid the creation of beans within libraries whenever possible.
We also considered auto-configuration, and that approach would work. There are ways to implement a workaround, so that is not the issue. I just thought it would be beneficial if the framework supported this method of declaring controller advices.
from spring-framework.
We have discussed this as a team and think that conditional configurations or Spring Boot auto-configurations are the preferred way of handling this use case.
We'll repurpose this issue to update the ControllerAdviceBean
Javadoc to make it obvious that this is for internal use (it's really public because it must be used from different packages). In the process, we should review its constructors and code paths to check whether some of those partially implement this "programmatic registration" feature that we don't intend to support. If we find any, we should deprecate/remove those code paths.
from spring-framework.
Related Issues (20)
- Rename remaining use of urlTemplate to uriTemplate
- Spring Boot 3.2.6 with async mode AdviceMode.ASPECTJ IllegalStateException HOT 5
- Remove AllEncompassingFormHttpMessageConverter from WebMvcConfigurationSupport HOT 3
- Consider adding overloaded methods to MockHttpServletRequestBuilder for headers and params HOT 3
- After upgrading to Spring Boot 3.2, `WebDataBinder` does not work as intended HOT 2
- Support queries in opaque URLs HOT 2
- AnnotationUtils performance degrades with deep stacks
- AnnotationUtils performance degrades with deep stacks HOT 1
- AnnotationUtils performance degrades with deep stacks HOT 1
- WebFlux: @ControllerAdvice should handle all the exceptions including from WebFilter HOT 7
- `@TestBean` and `@MockitoBean` tests fail in AOT mode HOT 2
- Example program for uploading files to rsocket @MessageMapping("upload") HOT 1
- Reference documentation sample for `MethodValidationPostProcessor` is missing a `static` keyword HOT 9
- spring dynamic bean annotation not invoked HOT 1
- Introduce dedicated AOT tests for bean override support in the TCF HOT 1
- Support bean override features with AOT HOT 1
- RestClient ResponseSpec.body() when using Reactor Netty throws an exception instead of returning null if no response body is available HOT 1
- Cron scheduled methods may be executed a fraction of a second too early HOT 2
- Native Image with spring-boot 3.2.3 - MissingResourceException: Can't find bundle for base name org.aspectj.weaver.weaver-messages HOT 1
- Add `isKotlinDataClass` to `KotlinDetector` HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from spring-framework.