revised bean scopes examples

This commit is contained in:
Loredana Crusoveanu 2016-05-05 20:19:48 +03:00
parent c3abba5f2a
commit e792db4d6d
3 changed files with 7 additions and 20 deletions

View File

@ -8,9 +8,7 @@ public class Person {
}
public Person(final String name, final int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
@ -21,17 +19,9 @@ public class Person {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(final int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
return "Person [name=" + name + "]";
}
}

View File

@ -36,4 +36,10 @@ public class ScopesConfig {
return new HelloMessageGenerator();
}
@Bean
@Scope(value = WebApplicationContext.SCOPE_GLOBAL_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
public HelloMessageGenerator globalSessionMessage() {
return new HelloMessageGenerator();
}
}

View File

@ -9,10 +9,7 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ScopesTest {
private static final String NAME = "John Smith";
private static final int AGE = 30;
private static final String NAME_OTHER = "Anna Jones";
private static final int AGE_OTHER = 40;
@Test
public void testScopeSingleton() {
@ -22,10 +19,7 @@ public class ScopesTest {
final Person personSingletonB = (Person) applicationContext.getBean("personSingleton");
personSingletonA.setName(NAME);
personSingletonB.setAge(AGE);
Assert.assertEquals(NAME, personSingletonB.getName());
Assert.assertEquals(AGE, personSingletonB.getAge());
((AbstractApplicationContext) applicationContext).close();
}
@ -38,10 +32,7 @@ public class ScopesTest {
final Person personPrototypeB = (Person) applicationContext.getBean("personPrototype");
personPrototypeA.setName(NAME);
personPrototypeA.setAge(AGE);
personPrototypeB.setName(NAME_OTHER);
personPrototypeB.setAge(AGE_OTHER);
Assert.assertEquals(NAME, personPrototypeA.getName());
Assert.assertEquals(NAME_OTHER, personPrototypeB.getName());