Skip to content

Commit

Permalink
a spring module with an initial example of a circular dependency that…
Browse files Browse the repository at this point in the history
… spring can't figure out without help
  • Loading branch information
zzantozz committed Mar 7, 2013
1 parent 163a417 commit 6babf95
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Expand Up @@ -40,6 +40,7 @@
<module>guava-concatenate-streams</module>
<module>spring-data</module>
<module>spring-mvc</module>
<module>spring</module>
</modules>
<properties>
<!--
Expand Down
39 changes: 39 additions & 0 deletions spring/pom.xml
@@ -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>
51 changes: 51 additions & 0 deletions spring/src/main/java/rds/spring/CircularDependencies.java
@@ -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);
}
}

0 comments on commit 6babf95

Please sign in to comment.