6 Replies Latest reply: Jul 21, 2009 9:12 AM by 843793 RSS

    Spring Injection in an Annotation Processor.

    843793
      I have a JAR which contains an Annotation Processing class, and the services .Processor file to go with it.

      I then call the comiler from my IDE with the following

      -proc:only -processorpath C:/source/annotations-processing/target/annotations-processor-1.0.0.jar

      Is there a way it would be possible to use Spring Dependency Injection within this application somehow. A way to get spring into the class path when the compiler runs and picks up the applicationContext.xml and does what it does best.

      I feel this is better suited here than it is in a Spring forum as I doubt there has been very much work in this area. If there is then I have trawled for hours and found nothing.

      I hope someone can help

      Best Regards,

      Chris
        • 1. Re: Spring Injection in an Annotation Processor.
          843793
          I have made some progress. I have managed to get the Spring libraries into the class path of the Annotation Processor JAR, however I can not load the applicationContext.xml file from within my JAR when the java compiler instantiates my processor.

          I can do

          java -jar annotations-processor-1.0.0.jar Main

          to execute a stand alone process which does

          BeanFactory beanFactory = new ClassPathXmlApplicationContext(
          "classpath*:META-INF/spring/applicationContext.xml");

          And this loads the context file.

          When I execute my compilation process with the same JAR it can not find the file.

          I can create a ClassPathXmlApplicationContext instance using a Resource. Is it possible to load my XML file from the JAR as a Resource from within the compilation process?

          I have tried some variations all which give me a file not found.

          Thanks

          Chris
          • 2. Re: Spring Injection in an Annotation Processor.
            843793
            The following fail

            BeanFactory x = new ClassPathXmlApplicationContext("/META-INF/spring/applicationContext.xml");
            BeanFactory x = new ClassPathXmlApplicationContext("META-INF/spring/applicationContext.xml");

            but the following passes

            URL resource = getClass().getResource("/META-INF/spring/applicationContext.xml");

            This is well outside the scope of Spring now, and is surely just a problem with class paths within the annotation processing environment.

            Thanks

            Chris
            • 3. Re: Spring Injection in an Annotation Processor.
              843793
              Milesy wrote:
              The following fail

              BeanFactory x = new ClassPathXmlApplicationContext("/META-INF/spring/applicationContext.xml");
              BeanFactory x = new ClassPathXmlApplicationContext("META-INF/spring/applicationContext.xml");

              but the following passes

              URL resource = getClass().getResource("/META-INF/spring/applicationContext.xml");

              This is well outside the scope of Spring now, and is surely just a problem with class paths within the annotation processing environment.
              Have you tried using Filer.getResource from within the annotation processor?

              http://java.sun.com/javase/6/docs/api/javax/annotation/processing/Filer.html#getResource(javax.tools.JavaFileManager.Location,%20java.lang.CharSequence,%20java.lang.CharSequence)

              I'm not sure if that method would enable the functionality you're seeking.
              • 4. Re: Spring Injection in an Annotation Processor.
                843793
                Hi.

                Thanks, but I can only get it to load from the "source directory" with that method. I can not get it to load from the processors JAR.

                When I use
                FileObject x = filer.getResource(StandardLocation.CLASS_PATH, "", "META-INF/spring/applicationContext.xml");
                I get

                An annotation processor threw an uncaught exception.
                Consult the following stack trace for details.

                org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from file [C:\source\annotations-processing\testing\processor.jar\META-INF\spring\applicationContext.xml];
                nested exception is java.io.FileNotFoundException: C:\source\annotations-processing\testing\processor.jar\META-INF\spring\applicationContext.xml (The system cannot find the path specified)

                Thanks

                Chris

                Edited by: Milesy on Jul 20, 2009 7:36 AM
                • 5. Re: Spring Injection in an Annotation Processor.
                  843793
                  Milesy wrote:
                  Hi.

                  Thanks, but I can only get it to load from the "source directory" with that method. I can not get it to load from the processors JAR.

                  When I use
                  FileObject x = filer.getResource(StandardLocation.CLASS_PATH, "", "META-INF/spring/applicationContext.xml");
                  I get

                  An annotation processor threw an uncaught exception.
                  Consult the following stack trace for details.
                  Depending on how your environment is configured, I suggest trying the other paths, like StandardLocation.ANNOTATION_PROCESSOR_PATH if you want to pull resources from the same location as annotation processors.
                  • 6. Re: Spring Injection in an Annotation Processor.
                    843793
                    Thanks. I managed to read the file in.