BAEL-3965: Created examples for instance and static factory methods.
This commit is contained in:
parent
ca443a08a3
commit
374905467e
@ -0,0 +1,18 @@
|
|||||||
|
package com.baeldung.factorymethod;
|
||||||
|
|
||||||
|
public class Bar {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Bar(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
package com.baeldung.factorymethod;
|
||||||
|
|
||||||
|
public class Foo {
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package com.baeldung.factorymethod;
|
||||||
|
|
||||||
|
public class InstanceBarFactory {
|
||||||
|
|
||||||
|
public Bar createInstance(String name) {
|
||||||
|
return new Bar(name);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package com.baeldung.factorymethod;
|
||||||
|
|
||||||
|
public class InstanceFooFactory {
|
||||||
|
|
||||||
|
public Foo createInstance() {
|
||||||
|
return new Foo();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package com.baeldung.factorymethod;
|
||||||
|
|
||||||
|
public class SingletonBarFactory {
|
||||||
|
|
||||||
|
private static final Bar INSTANCE = new Bar("unnamed");
|
||||||
|
|
||||||
|
public static Bar createInstance(String name) {
|
||||||
|
INSTANCE.setName(name);
|
||||||
|
return INSTANCE;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package com.baeldung.factorymethod;
|
||||||
|
|
||||||
|
public class SingletonFooFactory {
|
||||||
|
|
||||||
|
private static final Foo INSTANCE = new Foo();
|
||||||
|
|
||||||
|
public static Foo createInstance() {
|
||||||
|
return INSTANCE;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.baeldung.factorymethod;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ContextConfiguration("/factorymethod/instance-bar-config.xml")
|
||||||
|
public class InstanceBarFactoryIntegrationTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private Bar instance;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenValidInstanceFactoryConfig_whenCreateInstance_thenNameIsCorrect() {
|
||||||
|
assertNotNull(instance);
|
||||||
|
assertEquals("someName", instance.getName());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.baeldung.factorymethod;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ContextConfiguration("/factorymethod/instance-foo-config.xml")
|
||||||
|
public class InstanceFooFactoryIntegrationTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private Foo foo;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenValidInstanceFactoryConfig_whenCreateFooInstance_thenInstanceIsNotNull() {
|
||||||
|
assertNotNull(foo);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.baeldung.factorymethod;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ContextConfiguration("/factorymethod/static-bar-config.xml")
|
||||||
|
public class SingletonBarFactoryIntegrationTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private Bar instance;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenValidStaticFactoryConfig_whenCreateInstance_thenNameIsCorrect() {
|
||||||
|
assertNotNull(instance);
|
||||||
|
assertEquals("someName", instance.getName());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.baeldung.factorymethod;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ContextConfiguration("/factorymethod/static-foo-config.xml")
|
||||||
|
public class SingletonFooFactoryIntegrationTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private Foo singleton;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenValidStaticFactoryConfig_whenCreateInstance_thenInstanceIsNotNull() {
|
||||||
|
assertNotNull(singleton);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns:util="http://www.springframework.org/schema/util"
|
||||||
|
xsi:schemaLocation="
|
||||||
|
http://www.springframework.org/schema/beans
|
||||||
|
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||||
|
http://www.springframework.org/schema/util
|
||||||
|
http://www.springframework.org/schema/util/spring-util.xsd">
|
||||||
|
|
||||||
|
<bean id="instanceBarFactory"
|
||||||
|
class="com.baeldung.factorymethod.InstanceBarFactory" />
|
||||||
|
|
||||||
|
<bean id="bar"
|
||||||
|
factory-bean="instanceBarFactory"
|
||||||
|
factory-method="createInstance">
|
||||||
|
<constructor-arg value="someName" />
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
</beans>
|
@ -0,0 +1,18 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns:util="http://www.springframework.org/schema/util"
|
||||||
|
xsi:schemaLocation="
|
||||||
|
http://www.springframework.org/schema/beans
|
||||||
|
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||||
|
http://www.springframework.org/schema/util
|
||||||
|
http://www.springframework.org/schema/util/spring-util.xsd">
|
||||||
|
|
||||||
|
<bean id="instanceFooFactory"
|
||||||
|
class="com.baeldung.factorymethod.InstanceFooFactory" />
|
||||||
|
|
||||||
|
<bean id="foo"
|
||||||
|
factory-bean="instanceFooFactory"
|
||||||
|
factory-method="createInstance" />
|
||||||
|
|
||||||
|
</beans>
|
@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns:context="http://www.springframework.org/schema/context"
|
||||||
|
xmlns:util="http://www.springframework.org/schema/util"
|
||||||
|
xsi:schemaLocation="
|
||||||
|
http://www.springframework.org/schema/beans
|
||||||
|
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||||
|
http://www.springframework.org/schema/context
|
||||||
|
http://www.springframework.org/schema/context/spring-context.xsd
|
||||||
|
http://www.springframework.org/schema/util
|
||||||
|
http://www.springframework.org/schema/util/spring-util.xsd">
|
||||||
|
|
||||||
|
<bean id="bar"
|
||||||
|
class="com.baeldung.factorymethod.SingletonBarFactory"
|
||||||
|
factory-method="createInstance">
|
||||||
|
<constructor-arg value="someName" />
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
</beans>
|
@ -0,0 +1,18 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns:context="http://www.springframework.org/schema/context"
|
||||||
|
xmlns:util="http://www.springframework.org/schema/util"
|
||||||
|
xsi:schemaLocation="
|
||||||
|
http://www.springframework.org/schema/beans
|
||||||
|
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||||
|
http://www.springframework.org/schema/context
|
||||||
|
http://www.springframework.org/schema/context/spring-context.xsd
|
||||||
|
http://www.springframework.org/schema/util
|
||||||
|
http://www.springframework.org/schema/util/spring-util.xsd">
|
||||||
|
|
||||||
|
<bean id="foo"
|
||||||
|
class="com.baeldung.factorymethod.SingletonFooFactory"
|
||||||
|
factory-method="createInstance" />
|
||||||
|
|
||||||
|
</beans>
|
Loading…
x
Reference in New Issue
Block a user