Skip to content

Commit 69842b7

Browse files
MherBaghinyanpivovarit
authored andcommitted
BAEL-1592 (eugenp#3773)
* Injecting Prototype Beans into Singleton Instance * removed final keywords from the test
1 parent d3c292a commit 69842b7

10 files changed

Lines changed: 249 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.scope;
2+
3+
import com.baeldung.scope.prototype.PrototypeBean;
4+
import com.baeldung.scope.singletone.SingletonAppContextBean;
5+
import com.baeldung.scope.singletone.SingletonBean;
6+
import com.baeldung.scope.singletone.SingletonObjectFactoryBean;
7+
import com.baeldung.scope.singletone.SingletonProviderBean;
8+
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.ComponentScan;
11+
import org.springframework.context.annotation.Configuration;
12+
import org.springframework.context.annotation.Scope;
13+
14+
@Configuration
15+
@ComponentScan("com.baeldung.scope")
16+
public class AppConfig {
17+
18+
@Bean
19+
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
20+
public PrototypeBean prototypeBean() {
21+
return new PrototypeBean();
22+
}
23+
24+
@Bean
25+
public SingletonBean singletonBean() {
26+
return new SingletonBean();
27+
}
28+
29+
@Bean
30+
public SingletonProviderBean singletonProviderBean() {
31+
return new SingletonProviderBean();
32+
}
33+
34+
@Bean
35+
public SingletonAppContextBean singletonAppContextBean() {
36+
return new SingletonAppContextBean();
37+
}
38+
39+
@Bean
40+
public SingletonObjectFactoryBean singletonObjectFactoryBean() {
41+
return new SingletonObjectFactoryBean();
42+
}
43+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.scope;
2+
3+
import com.baeldung.scope.prototype.PrototypeBean;
4+
import com.baeldung.scope.singletone.SingletonBean;
5+
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
6+
import org.springframework.context.annotation.*;
7+
8+
@Configuration
9+
@ComponentScan("com.baeldung.scope")
10+
public class AppProxyScopeConfig {
11+
12+
@Bean
13+
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE, proxyMode = ScopedProxyMode.TARGET_CLASS)
14+
public PrototypeBean prototypeBean() {
15+
return new PrototypeBean();
16+
}
17+
18+
@Bean
19+
public SingletonBean singletonBean() {
20+
return new SingletonBean();
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.scope;
2+
3+
import com.baeldung.scope.prototype.PrototypeBean;
4+
import com.baeldung.scope.singletone.SingletonBean;
5+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
6+
import org.springframework.util.Assert;
7+
8+
public class BeanInjectionStarter {
9+
10+
public static void main(String[] args) throws InterruptedException {
11+
12+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
13+
SingletonBean firstSingleton = context.getBean(SingletonBean.class);
14+
PrototypeBean firstPrototype = firstSingleton.getPrototypeBean();
15+
16+
SingletonBean secondSingleton = context.getBean(SingletonBean.class);
17+
PrototypeBean secondPrototype = secondSingleton.getPrototypeBean();
18+
19+
Assert.isTrue(firstPrototype.equals(secondPrototype), "The same instance is returned");
20+
21+
}
22+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.scope.prototype;
2+
3+
import org.apache.log4j.Logger;
4+
5+
public class PrototypeBean {
6+
7+
private final Logger logger = Logger.getLogger(this.getClass());
8+
9+
public PrototypeBean() {
10+
logger.info("Prototype instance created");
11+
}
12+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.scope.singletone;
2+
3+
import com.baeldung.scope.prototype.PrototypeBean;
4+
import org.springframework.beans.BeansException;
5+
import org.springframework.context.ApplicationContext;
6+
import org.springframework.context.ApplicationContextAware;
7+
8+
public class SingletonAppContextBean implements ApplicationContextAware {
9+
10+
private ApplicationContext applicationContext;
11+
12+
public PrototypeBean getPrototypeBean() {
13+
return applicationContext.getBean(PrototypeBean.class);
14+
}
15+
16+
@Override
17+
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
18+
this.applicationContext = applicationContext;
19+
}
20+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.scope.singletone;
2+
3+
import com.baeldung.scope.prototype.PrototypeBean;
4+
import org.apache.log4j.Logger;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
7+
import java.time.LocalTime;
8+
9+
public class SingletonBean {
10+
11+
private final Logger logger = Logger.getLogger(this.getClass());
12+
13+
@Autowired
14+
private PrototypeBean prototypeBean;
15+
16+
public SingletonBean() {
17+
logger.info("Singleton instance created");
18+
}
19+
20+
public PrototypeBean getPrototypeBean() {
21+
logger.info(String.valueOf(LocalTime.now()));
22+
return prototypeBean;
23+
}
24+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.scope.singletone;
2+
3+
import com.baeldung.scope.prototype.PrototypeBean;
4+
import org.springframework.beans.factory.annotation.Lookup;
5+
import org.springframework.stereotype.Component;
6+
7+
@Component
8+
public class SingletonLookupBean {
9+
10+
@Lookup
11+
public PrototypeBean getPrototypeBean() {
12+
return null;
13+
}
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.scope.singletone;
2+
3+
import com.baeldung.scope.prototype.PrototypeBean;
4+
import org.springframework.beans.factory.ObjectFactory;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
7+
public class SingletonObjectFactoryBean {
8+
9+
@Autowired
10+
private ObjectFactory<PrototypeBean> prototypeBeanObjectFactory;
11+
12+
public PrototypeBean getPrototypeInstance() {
13+
return prototypeBeanObjectFactory.getObject();
14+
}
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.scope.singletone;
2+
3+
import com.baeldung.scope.prototype.PrototypeBean;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
6+
import javax.inject.Provider;
7+
8+
public class SingletonProviderBean {
9+
10+
@Autowired
11+
private Provider<PrototypeBean> myPrototypeBeanProvider;
12+
13+
public PrototypeBean getPrototypeInstance() {
14+
return myPrototypeBeanProvider.get();
15+
}
16+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung.scope;
2+
3+
import com.baeldung.scope.prototype.PrototypeBean;
4+
import com.baeldung.scope.singletone.SingletonLookupBean;
5+
import com.baeldung.scope.singletone.SingletonObjectFactoryBean;
6+
import com.baeldung.scope.singletone.SingletonProviderBean;
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
11+
import org.springframework.context.support.AbstractApplicationContext;
12+
import org.springframework.test.context.ContextConfiguration;
13+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
14+
import org.springframework.test.context.support.AnnotationConfigContextLoader;
15+
16+
@RunWith(SpringJUnit4ClassRunner.class)
17+
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = AppConfig.class)
18+
public class PrototypeBeanInjectionTest {
19+
20+
@Test
21+
public void givenPrototypeInjection_WhenObjectFactory_ThenNewInstanceReturn() {
22+
23+
AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
24+
25+
SingletonObjectFactoryBean firstContext = context.getBean(SingletonObjectFactoryBean.class);
26+
SingletonObjectFactoryBean secondContext = context.getBean(SingletonObjectFactoryBean.class);
27+
28+
PrototypeBean firstInstance = firstContext.getPrototypeInstance();
29+
PrototypeBean secondInstance = secondContext.getPrototypeInstance();
30+
31+
Assert.assertTrue("New instance expected", firstInstance != secondInstance);
32+
}
33+
34+
@Test
35+
public void givenPrototypeInjection_WhenLookup_ThenNewInstanceReturn() {
36+
37+
AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
38+
39+
SingletonLookupBean firstContext = context.getBean(SingletonLookupBean.class);
40+
SingletonLookupBean secondContext = context.getBean(SingletonLookupBean.class);
41+
42+
PrototypeBean firstInstance = firstContext.getPrototypeBean();
43+
PrototypeBean secondInstance = secondContext.getPrototypeBean();
44+
45+
Assert.assertTrue("New instance expected", firstInstance != secondInstance);
46+
}
47+
48+
@Test
49+
public void givenPrototypeInjection_WhenProvider_ThenNewInstanceReturn() {
50+
51+
AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
52+
53+
SingletonProviderBean firstContext = context.getBean(SingletonProviderBean.class);
54+
SingletonProviderBean secondContext = context.getBean(SingletonProviderBean.class);
55+
56+
PrototypeBean firstInstance = firstContext.getPrototypeInstance();
57+
PrototypeBean secondInstance = secondContext.getPrototypeInstance();
58+
59+
Assert.assertTrue("New instance expected", firstInstance != secondInstance);
60+
}
61+
}

0 commit comments

Comments
 (0)