Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
a spring module with an initial example of a circular dependency that…
… spring can't figure out without help
- Loading branch information
Showing
3 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>testbed</artifactId> | ||
<groupId>testbed</groupId> | ||
<version>1.1-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
<artifactId>spring</artifactId> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-core</artifactId> | ||
<version>3.2.1.RELEASE</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-beans</artifactId> | ||
<version>3.2.1.RELEASE</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-context</artifactId> | ||
<version>3.2.1.RELEASE</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>ch.qos.logback</groupId> | ||
<artifactId>logback-classic</artifactId> | ||
<version>1.0.9</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>cglib</groupId> | ||
<artifactId>cglib</artifactId> | ||
<version>2.2</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package rds.spring; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.DependsOn; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* Created with IntelliJ IDEA. | ||
* User: ryan | ||
* Date: 3/3/13 | ||
* Time: 10:49 PM | ||
*/ | ||
public class CircularDependencies { | ||
@Component("a") | ||
@DependsOn("b") // Without this annotation, Spring tries to create an A first and fails when creating the B that depends on the A that's in creation. | ||
public static class A { | ||
@Autowired | ||
public A(B b) { | ||
} | ||
} | ||
|
||
@Component("b") | ||
public static class B { | ||
private A a; | ||
|
||
public B() { | ||
} | ||
|
||
@Autowired | ||
public void setA(A a) { | ||
this.a = a; | ||
} | ||
} | ||
|
||
@Configuration | ||
@ComponentScan | ||
static class Config { | ||
} | ||
|
||
public static void main(String[] args) { | ||
AnnotationConfigApplicationContext context = | ||
new AnnotationConfigApplicationContext(Config.class); | ||
A a = context.getBean(A.class); | ||
B b = context.getBean(B.class); | ||
System.out.println(a); | ||
System.out.println(b); | ||
} | ||
} |