Skip to content
This repository has been archived by the owner on Feb 23, 2023. It is now read-only.

Inner bean definition's beanClass may not be resolved and generated with Object rather than the target type #1481

Closed
jnizet opened this issue Feb 5, 2022 · 2 comments
Assignees
Labels
type: bug A general bug
Milestone

Comments

@jnizet
Copy link

jnizet commented Feb 5, 2022

When an application that uses @WebFilter and @ServletComponentScan to automatically register and autowire servlet filters (rather than defining beans of type FilterRegistrationBean) is made native, it fails to start, with the following error:

***************************
APPLICATION FAILED TO START
***************************

Description:

Native reflection configuration for java.lang.Object.<init>(com.example.demo.Foo) is missing.

Action:

Native configuration for a method accessed reflectively is likely missing.
You can try to configure native hints in order to specify it explicitly.
See https://docs.spring.io/spring-native/docs/current/reference/htmlsingle/#native-hints for more details.

Here's a minimal repro (create with Spring initializr, with Kotlin, gradle and Web). This code works as expected in normal, non-native mode.

package com.example.demo

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.boot.web.servlet.ServletComponentScan
import org.springframework.stereotype.Component
import javax.servlet.Filter
import javax.servlet.FilterChain
import javax.servlet.ServletRequest
import javax.servlet.ServletResponse
import javax.servlet.annotation.WebFilter

@SpringBootApplication
@ServletComponentScan
class Demo1Application

fun main(args: Array<String>) {
    runApplication<Demo1Application>(*args)
}

@Component
class Foo {
    val message = "Hello from filter"
}

@WebFilter("/*")
class DemoFilter(private val foo: Foo) : Filter {
    override fun doFilter(request: ServletRequest, response: ServletResponse, chain: FilterChain) {
        response.writer.println(foo.message)
    }
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Feb 5, 2022
@sdeleuze
Copy link
Contributor

sdeleuze commented Feb 7, 2022

The generated code looks suspicious indeed:

BeanDefinitionRegistrar.of("com.example.webmvc.DemoFilter", FilterRegistrationBean.class)
        .instanceSupplier(() -> new FilterRegistrationBean()).customize((bd) -> {
      MutablePropertyValues propertyValues = bd.getPropertyValues();
      propertyValues.addPropertyValue("asyncSupported", false);
      propertyValues.addPropertyValue("dispatcherTypes", Set.of(DispatcherType.REQUEST));
      propertyValues.addPropertyValue("filter", BeanDefinitionRegistrar.inner(Object.class).withConstructor(Foo.class)
          .instanceSupplier((instanceContext) -> instanceContext.create(beanFactory, (attributes) -> new DemoFilter(attributes.get(0)))).toBeanDefinition());
      propertyValues.addPropertyValue("initParameters", Map.of());
      propertyValues.addPropertyValue("name", "com.example.webmvc.DemoFilter");
      propertyValues.addPropertyValue("servletNames", new String[] {  });
      propertyValues.addPropertyValue("urlPatterns", new String[] { "/*" });
    }).register(beanFactory);

@sdeleuze sdeleuze added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Feb 7, 2022
@sdeleuze sdeleuze added this to the 0.11.3 milestone Feb 7, 2022
@snicoll
Copy link
Contributor

snicoll commented Feb 8, 2022

The problem seems to be that the inner bean has not been post-processed the same way as regular "root" beans and its beanClass attribute has not been resolved. I am working on a fix.

@snicoll snicoll self-assigned this Feb 8, 2022
@snicoll snicoll changed the title ServletComponentScan seems to be unsupported Inner bean definition's beanClass may not be resolved and generated with Object rather than the target type Feb 8, 2022
@snicoll snicoll closed this as completed in e184df9 Feb 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: bug A general bug
Development

No branches or pull requests

4 participants