[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reference documentation sample for MethodValidationPostProcessor is missing a static keyword #32929

Closed
ozooxo opened this issue May 30, 2024 · 9 comments
Assignees
Labels
status: feedback-provided Feedback has been provided type: documentation A documentation task
Milestone

Comments

@ozooxo
Copy link
Contributor
ozooxo commented May 30, 2024

I followed the Spring official document to define MethodValidationPostProcessor for bean validation. But I am triggering the below warning:

2024-05-30T17:53:41.988-04:00 WARN 74724 --- [demo] [ main] trationDelegate$BeanPostProcessorChecker : Bean 'appConfig' of type [com.example.demo.AppConfig$$SpringCGLIB$$0] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying). The currently created BeanPostProcessor [validationPostProcessor] is declared through a non-static factory method on that class; consider declaring it as static instead.

To reproduce it, see https://github.com/ozooxo/spring-jakarta-validation-post-processors-warning

  • Commit #1 is solely generated by spring starter.
  • Commit #2 defines the MethodValidationPostProcessor bean following the doc.

then if I ./gradlew bootRun I'll see the above warning.

It seems that a similar problem has been coming up before in ~2017:

but it has been coming back.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label May 30, 2024
@quaff
Copy link
Contributor
quaff commented May 31, 2024

Have you tried adding @JvmStatic on fun validationPostProcessor()?

@sbrannen
Copy link
Member

Have you tried adding @JvmStatic on fun validationPostProcessor()?

@ozooxo, please try what @quaff suggested and let us know if that gets rid of the warning for you.

@sbrannen sbrannen added the status: waiting-for-feedback We need additional information before we can continue label May 31, 2024
@snicoll
Copy link
Member
snicoll commented May 31, 2024

The warning is as follows:

Bean 'appConfig' of type [com.example.demo.AppConfig$$SpringCGLIB$$0] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying). The currently created BeanPostProcessor [validationPostProcessor] is declared through a non-static factory method on that class; consider declaring it as static instead.

The documentation is wrong, that Java example should have the static keyword. We'll get that fixed but, as others have said, please read the warning in the log. I don't know how to achieve that with Kotlin though.

As for the sample demo, if you're using Spring Boot, you don't need to register that bean as the auto-configuration does that already.

@snicoll snicoll changed the title Defining MethodValidationPostProcessor triggers Bean *** is not eligible for getting processed by all BeanPostProcessors warning Reference documentation sample for MethodValidationPostProcessor is missing a static keyword May 31, 2024
@snicoll snicoll added type: documentation A documentation task and removed status: waiting-for-feedback We need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged or decided on labels May 31, 2024
@snicoll snicoll added this to the 6.1.x milestone May 31, 2024
@snicoll snicoll added the status: waiting-for-feedback We need additional information before we can continue label May 31, 2024
@sbrannen
Copy link
Member

I don't know how to achieve that with Kotlin though.

I think it would look like this:

@Configuration
class AppConfig {

    companion object {
        @JvmStatic
        @Bean
        fun validationPostProcessor(): MethodValidationPostProcessor {
            return MethodValidationPostProcessor()
        }
    }
}

@snicoll
Copy link
Member
snicoll commented May 31, 2024

Yes, I am aware how @JvmStatic works. Have you tried it?

@sdeleuze offered to look why the code above doesn't work.

@ozooxo

This comment was marked as duplicate.

@ozooxo

This comment was marked as duplicate.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels May 31, 2024
@ozooxo
Copy link
Contributor Author
ozooxo commented May 31, 2024

As for the sample demo, if you're using Spring Boot, you don't need to register that bean as the auto-configuration does that already.

I am using Spring boot, and remove this bean can get rid of the warning. However,

  • For unit tests to cover @Validated and @Valid, only @SpringBootTest instead of @ExtendWith(SpringExtension::class) can work. That makes sense because the auto-configured bean is not in my @Import list.
  • Guess then I can no longer customize the post processor e.g. processor.setAdaptConstraintViolations(true).

To see this, I have commit #3 and in it the testInsertWithInvalidEmail() test can raise ConstraintViolationException as expected. And with commit #4 in order for the tests to still pass, I need @SpringBootTest.

@sdeleuze sdeleuze self-assigned this Jun 3, 2024
@sdeleuze sdeleuze modified the milestones: 6.1.x, 6.1.9 Jun 3, 2024
@sdeleuze sdeleuze changed the title Reference documentation sample for MethodValidationPostProcessor is missing a static keyword Reference documentation sample for MethodValidationPostProcessor is missing a static keyword Jun 3, 2024
@sdeleuze
Copy link
Contributor
sdeleuze commented Jun 3, 2024

Documentation fixed for the Java use case. Declaring BeanPostProcessor beans is (surprisingly) not possible yet with Kotlin but this will be supported as of 6.2 via #32946. With previous versions, please use Java classes as a workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: feedback-provided Feedback has been provided type: documentation A documentation task
Projects
None yet
Development

No branches or pull requests

6 participants