remove extra classes

This commit is contained in:
Loredana Crusoveanu 2018-06-24 19:16:02 +03:00
parent 1cea9ee088
commit c19dfa510a
6 changed files with 25 additions and 65 deletions

View File

@ -47,14 +47,14 @@ public class AppConfig {
} }
@Bean @Bean
public Function<String, PrototypeBeanWithParam> beanFactory() { public Function<String, PrototypeBean> beanFactory() {
return name -> prototypeBeanWithParam(name); return name -> prototypeBeanWithParam(name);
} }
@Bean @Bean
@Scope(value = "prototype") @Scope(value = "prototype")
public PrototypeBeanWithParam prototypeBeanWithParam(String name) { public PrototypeBean prototypeBeanWithParam(String name) {
return new PrototypeBeanWithParam(name); return new PrototypeBean(name);
} }
@Bean @Bean

View File

@ -1,27 +0,0 @@
package com.baeldung.scope;
import java.util.function.Function;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import com.baeldung.scope.prototype.PrototypeBeanWithParam;
@Configuration
public class PrototypeFactoryBeanConfig {
@Bean
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE, proxyMode = ScopedProxyMode.TARGET_CLASS)
public PrototypeBeanWithParam prototypeBeanWithParam(String name) {
return new PrototypeBeanWithParam(name);
}
@Bean
public Function<String, PrototypeBeanWithParam> prototypeBeanFactory() {
return runtimeArg -> prototypeBeanWithParam(runtimeArg);
}
}

View File

@ -9,4 +9,20 @@ public class PrototypeBean {
public PrototypeBean() { public PrototypeBean() {
logger.info("Prototype instance created"); logger.info("Prototype instance created");
} }
private String name;
public PrototypeBean(String name) {
this.name = name;
logger.info("Prototype instance " + name + " created");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
} }

View File

@ -1,28 +0,0 @@
package com.baeldung.scope.prototype;
import org.apache.log4j.Logger;
public class PrototypeBeanWithParam {
private String name;
private final Logger logger = Logger.getLogger(this.getClass());
public PrototypeBeanWithParam() {
logger.info("Prototype instance with param created");
}
public PrototypeBeanWithParam(String name) {
this.name = name;
logger.info("Prototype instance " + name + " created");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -4,15 +4,15 @@ import java.util.function.Function;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import com.baeldung.scope.prototype.PrototypeBeanWithParam; import com.baeldung.scope.prototype.PrototypeBean;
public class SingletonFunctionBean { public class SingletonFunctionBean {
@Autowired @Autowired
private Function<String, PrototypeBeanWithParam> beanFactory; private Function<String, PrototypeBean> beanFactory;
public PrototypeBeanWithParam getPrototypeInstance(String name) { public PrototypeBean getPrototypeInstance(String name) {
PrototypeBeanWithParam bean = beanFactory.apply(name); PrototypeBean bean = beanFactory.apply(name);
return bean; return bean;
} }

View File

@ -1,7 +1,6 @@
package com.baeldung.scope; package com.baeldung.scope;
import com.baeldung.scope.prototype.PrototypeBean; import com.baeldung.scope.prototype.PrototypeBean;
import com.baeldung.scope.prototype.PrototypeBeanWithParam;
import com.baeldung.scope.singletone.SingletonFunctionBean; import com.baeldung.scope.singletone.SingletonFunctionBean;
import com.baeldung.scope.singletone.SingletonLookupBean; import com.baeldung.scope.singletone.SingletonLookupBean;
import com.baeldung.scope.singletone.SingletonObjectFactoryBean; import com.baeldung.scope.singletone.SingletonObjectFactoryBean;
@ -70,8 +69,8 @@ public class PrototypeBeanInjectionIntegrationTest {
SingletonFunctionBean firstContext = context.getBean(SingletonFunctionBean.class); SingletonFunctionBean firstContext = context.getBean(SingletonFunctionBean.class);
SingletonFunctionBean secondContext = context.getBean(SingletonFunctionBean.class); SingletonFunctionBean secondContext = context.getBean(SingletonFunctionBean.class);
PrototypeBeanWithParam firstInstance = firstContext.getPrototypeInstance("instance1"); PrototypeBean firstInstance = firstContext.getPrototypeInstance("instance1");
PrototypeBeanWithParam secondInstance = secondContext.getPrototypeInstance("instance2"); PrototypeBean secondInstance = secondContext.getPrototypeInstance("instance2");
Assert.assertTrue("New instance expected", firstInstance != secondInstance); Assert.assertTrue("New instance expected", firstInstance != secondInstance);
} }