Skip to content

Instantly share code, notes, and snippets.

@rwinch
Created October 15, 2013 17:53
Show Gist options
  • Save rwinch/6995720 to your computer and use it in GitHub Desktop.
Save rwinch/6995720 to your computer and use it in GitHub Desktop.
Imported Configuration can reference beans from Configuration that imported it
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
public class Test {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(A.class);
try {
B bean = context.getBean(B.class);
if(bean.c == null) {
throw new IllegalStateException("Failed!");
}
else {
System.out.println("SUCESS!!!!!!!!!!!!!!!!!!!");
}
} finally {
context.close();
}
}
@Import(B.class)
@Configuration
static class A {
@Bean
public C c() { return new C(); }
}
@Configuration
static class B {
@Autowired
private C c;
}
static class C {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment