revised bean scopes examples
This commit is contained in:
parent
c3abba5f2a
commit
e792db4d6d
|
@ -8,9 +8,7 @@ public class Person {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Person(final String name, final int age) {
|
public Person(final String name, final int age) {
|
||||||
super();
|
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.age = age;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
|
@ -21,17 +19,9 @@ public class Person {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getAge() {
|
|
||||||
return age;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAge(final int age) {
|
|
||||||
this.age = age;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Person [name=" + name + ", age=" + age + "]";
|
return "Person [name=" + name + "]";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,4 +36,10 @@ public class ScopesConfig {
|
||||||
return new HelloMessageGenerator();
|
return new HelloMessageGenerator();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@Scope(value = WebApplicationContext.SCOPE_GLOBAL_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
|
||||||
|
public HelloMessageGenerator globalSessionMessage() {
|
||||||
|
return new HelloMessageGenerator();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,10 +9,7 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
public class ScopesTest {
|
public class ScopesTest {
|
||||||
|
|
||||||
private static final String NAME = "John Smith";
|
private static final String NAME = "John Smith";
|
||||||
private static final int AGE = 30;
|
|
||||||
|
|
||||||
private static final String NAME_OTHER = "Anna Jones";
|
private static final String NAME_OTHER = "Anna Jones";
|
||||||
private static final int AGE_OTHER = 40;
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testScopeSingleton() {
|
public void testScopeSingleton() {
|
||||||
|
@ -22,10 +19,7 @@ public class ScopesTest {
|
||||||
final Person personSingletonB = (Person) applicationContext.getBean("personSingleton");
|
final Person personSingletonB = (Person) applicationContext.getBean("personSingleton");
|
||||||
|
|
||||||
personSingletonA.setName(NAME);
|
personSingletonA.setName(NAME);
|
||||||
personSingletonB.setAge(AGE);
|
|
||||||
|
|
||||||
Assert.assertEquals(NAME, personSingletonB.getName());
|
Assert.assertEquals(NAME, personSingletonB.getName());
|
||||||
Assert.assertEquals(AGE, personSingletonB.getAge());
|
|
||||||
|
|
||||||
((AbstractApplicationContext) applicationContext).close();
|
((AbstractApplicationContext) applicationContext).close();
|
||||||
}
|
}
|
||||||
|
@ -38,10 +32,7 @@ public class ScopesTest {
|
||||||
final Person personPrototypeB = (Person) applicationContext.getBean("personPrototype");
|
final Person personPrototypeB = (Person) applicationContext.getBean("personPrototype");
|
||||||
|
|
||||||
personPrototypeA.setName(NAME);
|
personPrototypeA.setName(NAME);
|
||||||
personPrototypeA.setAge(AGE);
|
|
||||||
|
|
||||||
personPrototypeB.setName(NAME_OTHER);
|
personPrototypeB.setName(NAME_OTHER);
|
||||||
personPrototypeB.setAge(AGE_OTHER);
|
|
||||||
|
|
||||||
Assert.assertEquals(NAME, personPrototypeA.getName());
|
Assert.assertEquals(NAME, personPrototypeA.getName());
|
||||||
Assert.assertEquals(NAME_OTHER, personPrototypeB.getName());
|
Assert.assertEquals(NAME_OTHER, personPrototypeB.getName());
|
||||||
|
|
Loading…
Reference in New Issue