3 Replies Latest reply on Oct 29, 2019 5:27 AM by manovotn

    WELD-000119 Bean not found when searching bean under spring boot fat jar

    alany07

      Hi there,

       

      I am running latest Weld inside a spring boot application. My SpringBootApplication will initialize Weld via the following code.

       

      {code}

      public static void main(String[] args) {

       

         SpringApplication app = new SpringApplication(MyApplication.class);
         app.addInitializers(new ApplicationContextInitializer<ConfigurableApplicationContext>() {

           @Override
           public void initialize(ConfigurableApplicationContext applicationContext) {

             new Weld().initialize();
           }

         });
         app.run(args);
      }

      {code}

       

      Then, Weld will scan/load all the beans defined under my application project. This works fine when I run it on Intellij without creating a fat job.

       

      When I create a fat job, all my application classes go to BOOT-INF/classes and all the dependencies (including weld jars) go to BOOT-INF/lib. Weld finds all my application classes as "BOOT-INF.classes.com.mycompany.MyBean.class, for example. Then the classloader fails to load it and throws

       

           WELD-000119: Not generating any bean definitions from BOOT-INF.classes.com.mycompany.MyBean because of underlying class loading error

       

      I searched and found this issue before  https://issues.jboss.org/browse/WELD-2254. But it doesn't really solve the problem after trying a few different weld versions.

       

      I debugged and find that it can be solved with  a check to remove "BOOT-INF.classes." if it's present in class name inside WeldResourceLoader.java, like

       

        if (name.startsWith("BOOT-INF.classes.")) {

          name = name.substring("BOOT-INF.classes.".length());
        }

        return getClassLoader().loadClass(name);

       

      Is there already some other solution/configuration which can solve the problem? If not, do you think you can add this to your next release?

       

      Thanks,

      -Alan

        • 1. Re: WELD-000119 Bean not found when searching bean under spring boot fat jar
          manovotn

          Hello,

           

          let me start by saying that combining Spring and Weld is brave step into mostly uncharted (or undocumented?) territory ;-)

          Partly, this is similar to [WELD-2569] BeanArchives can't be resolved in URL form - JBoss Issue Tracker  in a way that it asks Weld to recognize some non-standard format for archives, or, in this case, location of files representing beans.

           

          If this is something you'd like to see happen, then please create a Weld JIRA and send a PR. Do you have a reproducer for this? Some simple GH project for instance.

          I see you have done some digging, but changing this directly inside WeldResourceLoader is a bad idea, ideally, you want to change it at the location where we transform file location into class name.

          I suspect this will be around FileSystemBeanArchiveHandler#add() method. Maybe the utils method could be tempered with to account for this? I am just guessing though, haven't debugged this myself.

          • 2. Re: WELD-000119 Bean not found when searching bean under spring boot fat jar
            alany07

            H Matej,

             

            Thanks for the response. It's quite a lot of efforts to combine both Spring and Weld. I am migrating one service from jboss based to spring boot based and still like to maintain all the weld cdi development we had, therefore the adventure.

             

            Thanks for pointing out the place to make changes in FileSystemBeanArchiveHandler. I agree that WeldResourceLoader is not a good place. It's just easier to meet my goal. FileSystemBeanArchiveHandler is a lot more complex and I was afraid that it may break other discovery functions. My fix has least impact I think. I will file JIRA and try to contribute to a PR with fix in FileSystemBeanArchiveHandler. I will create a reproducer project for it.

             

            Thanks,

            -Alan

            • 3. Re: WELD-000119 Bean not found when searching bean under spring boot fat jar
              manovotn

              alany07  wrote:

              I will file JIRA and try to contribute to a PR with fix in FileSystemBeanArchiveHandler. I will create a reproducer project for it.

              Awesome, thanks!