Results 1 to 10 of 10

Thread: Problem with multiple java-config files

  1. #1
    Join Date
    Mar 2006
    Posts
    29

    Default 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. #2
    Join Date
    Apr 2007
    Posts
    307

  3. #3
    Join Date
    Mar 2006
    Posts
    29

    Default

    Thanks, that was helpful.
    Basheer.

  4. #4

    Thumbs down 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 ?

  5. #5
    Join Date
    Apr 2007
    Posts
    307

    Default

    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!
    Chris Beams
    Spring Framework committer, VMware
    http://github.com/cbeams

  6. #6

    Default

    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.

  7. #7
    Join Date
    Apr 2007
    Posts
    307

    Default

    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?
    Chris Beams
    Spring Framework committer, VMware
    http://github.com/cbeams

  8. #8

    Default 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.

  9. #9
    Join Date
    Apr 2010
    Posts
    9

    Default

    Thanks that was helpful.

    Kristin

  10. #10

    Default

    Really its a great suggestiovs.its helping me alot

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •