Announcement Announcement Module
Collapse

JavaConfig forum decommissioned in favor of Core Container

As described at

http://static.springsource.org/sprin...fig/README.TXT

key features of the Spring JavaConfig project have been migrated into the core Spring Framework as of version 3.0.

Please see the Spring 3.0 documentation on @Configuration and @Bean support:

http://static.springsource.org/sprin...tml#beans-java

For any questions related to @Configuration classes and @Bean methods in Spring 3.0, please post in the dedicated 'Core Container' forum at

http://forum.springsource.org/forumdisplay.php?f=26
See more
See less
Problem with multiple java-config files Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with multiple java-config files

    Hi,
    I have two simple beans A and B, where B depends upon A. My requirement is that I have to define them in two separate config files.
    I need to be able to define something similar to the following, but obviously it does not compile because, ConfigB file does not know a().

    @Configuration
    public class ConfigA {
    @Bean
    public A a() { return new A(); }
    }

    @Configuration
    public class ConfigB {
    @Bean
    @Scope(value=”prototype”)
    public B b() { return new B(a()); }
    }

    How do I accomplish this? Appreciate any info on this.
    Thanks,
    Basheer.

  • #2
    http://static.springsource.org/sprin...ration-classes

    Comment


    • #3
      Thanks, that was helpful.
      Basheer.

      Comment


      • #4
        AnnotationConfigApplicationContext scan for @Configuration

        Hi ,
        1) I have an abstract class annotated by org.springframework.context.annotation.Configurati on :

        package package1.subpackage1;

        import org.springframework.context.annotation.Bean;
        import org.springframework.context.annotation.Configurati on;
        import package2.Pck2Class;

        @Configuration
        public abstract class ISConfigurationManager2 {

        @Bean()
        public Pck2Class pck2Class() {
        Pck2Class connector = new Pck2Class();
        return connector;
        }
        }

        2) I have a main class trying to scan for all configurations by
        AnnotationConfigApplicationContext.scan("packageNa me");
        The main method as follow:

        public static void main(String[] args) throws Exception {
        AnnotationConfigApplicationContext javaConfigContext =new AnnotationConfigApplicationContext();

        javaConfigContext.scan("package1");

        Object pck3Class = javaConfigContext.getBean("pck2Class");
        }

        I'm getting an exception saying that such an object/bean is defined
        In order to make sure that the issue is with the scan/@Configuration I took the next steps :
        1) In the main method I initialized the AnnotationConfigApplicationContext with the
        ISConfigurationManager2.class and then I got the desired bean smoothly:
        public static void main(String[] args) throws Exception {
        AnnotationConfigApplicationContext javaConfigContext =new AnnotationConfigApplicationContext(ISConfiguration Manager2.class);
        Object pck3Class = javaConfigContext.getBean("pck2Class");
        //Here we get the bean successfully!!!
        }
        2)I tried to to use the scan method for @Component (and not @Configuration )
        and here I got the bean with success :
        I) I changed the annotation and removed the abstract notifier :
        package package1.subpackage1;

        import org.springframework.context.annotation.Bean;
        import org.springframework.context.annotation.Configurati on;
        import package2.Pck2Class;
        @Component
        public class ISConfigurationManager2 {

        @Bean()
        public Pck2Class pck2Class() {
        Pck2Class connector = new Pck2Class();
        return connector;
        }
        }

        II) I used the next main method :
        public static void main(String[] args) throws Exception {
        AnnotationConfigApplicationContext javaConfigContext =new AnnotationConfigApplicationContext();

        javaConfigContext.scan("package1");

        Object pck3Class = javaConfigContext.getBean("pck2Class");
        }

        The difference is that this time I succeed getting the bean

        So , my conclusion is that there is an issue with scanning for classes annotated with @configuration , My questions as follow :
        1)Can some please highlight this issue with a solution for "How to scan for @configuration classes ?
        2)If I can get the beans from a class annotated by component ,what do I need the @Configuration for ?

        Comment


        • #5
          make sure to call context.refresh() after calling scan() and before calling getBean(). If you're doing this, I didn't see it in your code snippets.

          Also, please wrap any future code postings in the [ code ] ... [/ code ] tags. it really helps with readability. Thanks!

          Comment


          • #6
            Hi Chris,
            First I want to thank you for you attention and time .
            second , I did refresh (javaConfigContext,refresh() , I just forgot to mention it ).
            What I found is that the scan does not suppose to find classes annotated with @Configuration . You can read it here :http://static.springsource.org/sprin...spath-scanning search for section : 3.10.3 Using filters to customize scanning.
            So , I decide to use the @Component instead of @Configuration .

            Thanks again.

            Comment


            • #7
              Mordechai,

              Consider the following:
              Code:
              package a.pkg;
              
              import org.junit.Test;
              import org.springframework.context.annotation.AnnotationConfigApplicationContext;
              import org.springframework.context.annotation.Bean;
              import org.springframework.context.annotation.Configuration;
              
              
              public class ScanTests {
              	@Test
              	public void test() {
              		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
              		ctx.scan("a.pkg");
              		ctx.refresh();
              		ctx.getBean(FooConfig.class);
              		ctx.getBean(FooBean.class);
              	}
              }
              
              @Configuration
              class FooConfig {
              	@Bean FooBean fooBean() { return new FooBean(); }
              }
              
              class FooBean { }
              This test passes. the scan() method above does recognize @Configuration classes, because the @Configuration annotation is meta-annotated with @Component. By default, any directly or indirectly @Component-annotated class is a candidate for component scanning.

              Are you observing some behavior other than this?

              Comment


              • #8
                class annotated with @Configuration must NOT be abstract

                Hi Chris ,
                The problem was because of the ISConfigurationManager2 abstract notifier .

                Thanks again, it was areal help.

                Comment


                • #9
                  Thanks that was helpful.

                  Kristin

                  Comment


                  • #10
                    Really its a great suggestiovs.its helping me alot

                    Comment

                    Working...
                    X