10 Replies Latest reply on Jun 24, 2009 6:20 AM by gurkanerdogdu

    Install from Annotations

    gurkanerdogdu

      Hi;
      I am trying to install bean into kernel via bean class annotations. I wrote the following code

      BeanMetaData meta = AnnotationToBeanMetaDataFactory.createBeanMetaData(Person.class);
      KernelControllerContext context = getKernel().getController().install(meta);
      


      I am getting exception with "Null name".

      But if I set meta.setName(name), no exception.
      Actually, Person class is annotated with @Bean(name="name"), why it throws Exception?

      Thanks;

      Gurkan

        • 1. Re: Install from Annotations
          alesj

          The matching annotation plugin is missing usage of this name:

          public class BeanAnnotationPlugin extends ClassAnnotationPlugin<Bean>
          {
           public static final BeanAnnotationPlugin INSTANCE = new BeanAnnotationPlugin();
          
           protected BeanAnnotationPlugin()
           {
           super(Bean.class);
           }
          
           protected List<? extends MetaDataVisitorNode> internalApplyAnnotation(ClassInfo info, Bean annotation, BeanMetaData beanMetaData)
           {
           AbstractBeanMetaData abmd = checkIfNotAbstractBeanMetaDataSpecific(beanMetaData);
          
           if (abmd.getAutowireType() == null)
           abmd.setAutowireType(annotation.autowireType());
           if (abmd.getMode() == null)
           abmd.setMode(annotation.mode());
           if (abmd.getErrorHandlingMode() == null)
           abmd.setErrorHandlingMode(annotation.errorHandlingMode());
           if (abmd.getAccessMode() == null)
           abmd.setAccessMode(annotation.accessMode());
          
           // we don't put bmd back to be inspected
           // since the changes we apply *here* don't really
           // trigger any change in metadata - all enums
           return null;
           }
          }


          I did put this comment here
          public @interface Bean
          {
           /**
           * Get the bean's name.
           *
           * This is only meant to be used before
           * bean meta data is already installed
           * in Controller.
           *
           * @return bean's name
           */
           String name() default "";
          


          But thinking about it more + your use case,
          I'm not sure anymore why I left it out from the plugin.

          You can create your temp @Bean annotation plugin and register it against BasicBeanMetaDataAnnotationAdapter.
          I'll add this name usage + test if it breaks something.

          • 2. Re: Install from Annotations
            gurkanerdogdu

            thanks for answer.

            But if I do not mind to use -beans.xml, is it required to implement new plugin and register it ? Is there any other way ?

            Actually my question is that, is there any discovery process/class to discover annotated bean classes without configuring XML at boot time?

            • 3. Re: Install from Annotations
              alesj

               

              "gurkanerdogdu" wrote:

              Actually my question is that, is there any discovery process/class to discover annotated bean classes without configuring XML at boot time?

              Yes.
              Check the Deployers project.
              It's has @Bean auto-discovery as a test case.

              • 4. Re: Install from Annotations
                alesj

                 

                "alesj" wrote:
                "gurkanerdogdu" wrote:

                Actually my question is that, is there any discovery process/class to discover annotated bean classes without configuring XML at boot time?

                Yes.
                Check the Deployers project.
                It's has @Bean auto-discovery as a test case.

                http://anonsvn.jboss.org/repos/jbossas/projects/jboss-deployers/trunk/deployers-vfs/src/main/java/org/jboss/deployers/vfs/deployer/kernel/BeanScanningDeployer.java
                http://anonsvn.jboss.org/repos/jbossas/projects/jboss-deployers/trunk/deployers-vfs/src/test/java/org/jboss/test/deployers/vfs/deployer/bean/test/BeanScanningUnitTestCase.java

                • 5. Re: Install from Annotations
                  gurkanerdogdu

                  BeanDeployer class gets DeploymentUnit class as an input parameter. I just want is that when there is an empty *-beans.xml file in the standalone project's META-INF folder, its configured kernel collects all of annotated classes from classpath and register them with registry.

                  • 6. Re: Install from Annotations
                    alesj

                    BeanDeployer != BeanScanningDeployer ;-)

                    • 7. Re: Install from Annotations
                      gurkanerdogdu

                       

                      "alesj" wrote:
                      BeanDeployer != BeanScanningDeployer ;-)

                      Upps, I mean BeanScanningDeployer

                      Thanks;

                      • 8. Re: Install from Annotations
                        gurkanerdogdu

                        Hi;

                        Is there any class responsible for creating BeanMetaData class from annotated bean ? AFAIK, there is a BeanMetaDataBuilder class, but you have to set all of its parameters.

                        Example :
                        @Bean(name="person")
                        class Person(){}

                        BeanMetaData metaData = MetaDataBuilder.parseClass(Person.class);

                        "parseClass", parse all annotations from class and return configured BeanMetaData instance.

                        Thanks;

                        • 9. Re: Install from Annotations
                          alesj

                          You are already using it, see your first post. ;-)
                          - AnnotationToBeanMetaDataFactory

                          Then it's up to the annotation plugins how they treat found annotations.
                          By default BasicBeanMetaDataAnnotationAdapter already has a bunch of them pre-configured.
                          But you can easily add your own, or override/replace existing.

                          • 10. Re: Install from Annotations
                            gurkanerdogdu

                             

                            "alesj" wrote:
                            You are already using it, see your first post. ;-)
                            - AnnotationToBeanMetaDataFactory


                            I am totally lost my way in APIs, SPIs, Plugins :)

                            Thanks;