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