Skip to content

Instantly share code, notes, and snippets.

@bijukunjummen
Created August 19, 2012 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bijukunjummen/3395329 to your computer and use it in GitHub Desktop.
Save bijukunjummen/3395329 to your computer and use it in GitHub Desktop.
package org.bk.samples.circularref;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class TestScopedProxy {
@Autowired A a;
@Test
public void test() {
assertThat(a, is(notNullValue()));
assertThat(a.getB().getImportantValueFromA(), is("stringvalue"));
a.setImportantValue("anewValue");
assertThat(a.getB().getImportantValueFromA(), is("anewValue"));
}
@Configuration
@ComponentScan(basePackages="org.bk.samples.circularref")
public static class ContextConfig{
}
}
@Component
@Scope(value="prototype", proxyMode=ScopedProxyMode.TARGET_CLASS)
class B {
@Value("#{a.importantValue}")
private String importantValueFromA;
public String getImportantValueFromA() {
return importantValueFromA;
}
public void setImportantValueFromA(String importantValueFromA) {
this.importantValueFromA = importantValueFromA;
}
}
@Component("a")
class A {
private String importantValue = "stringvalue";
@Autowired
private B b;
public String getImportantValue() {
return importantValue;
}
public void setImportantValue(String importantValue) {
this.importantValue = importantValue;
}
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment