Merge pull request #9135 from albanoj2/BAEL-3965

BAEL-3965: Created examples for instance and static factory methods.
This commit is contained in:
Eric Martin 2020-04-25 22:07:06 -05:00 committed by GitHub
commit 9efd76c261
17 changed files with 294 additions and 0 deletions

View File

@ -652,6 +652,7 @@
<module>spring-core</module> <module>spring-core</module>
<module>spring-core-2</module> <module>spring-core-2</module>
<module>spring-core-3</module> <module>spring-core-3</module>
<module>spring-core-4</module>
<module>spring-cucumber</module> <module>spring-cucumber</module>
<module>spring-data-rest</module> <module>spring-data-rest</module>
@ -1157,6 +1158,7 @@
<module>spring-core</module> <module>spring-core</module>
<module>spring-core-2</module> <module>spring-core-2</module>
<module>spring-core-3</module> <module>spring-core-3</module>
<module>spring-core-4</module>
<module>spring-cucumber</module> <module>spring-cucumber</module>
<module>spring-data-rest</module> <module>spring-data-rest</module>

7
spring-core-4/README.md Normal file
View File

@ -0,0 +1,7 @@
## Spring Core
This module contains articles about core Spring functionality
## Relevant Articles:
- More articles: [[<-- prev]](/spring-core-3)

63
spring-core-4/pom.xml Normal file
View File

@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-core-4</artifactId>
<name>spring-core-4</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-spring-5</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-spring-5</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.version}</version>
</plugin>
</plugins>
</build>
<properties>
<maven.surefire.version>2.22.1</maven.surefire.version>
<annotation-api.version>1.3.2</annotation-api.version>
<spring.boot.version>2.2.2.RELEASE</spring.boot.version>
</properties>
</project>

View File

@ -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;
}
}

View File

@ -0,0 +1,5 @@
package com.baeldung.factorymethod;
public class Foo {
}

View File

@ -0,0 +1,8 @@
package com.baeldung.factorymethod;
public class InstanceBarFactory {
public Bar createInstance(String name) {
return new Bar(name);
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.factorymethod;
public class InstanceFooFactory {
public Foo createInstance() {
return new Foo();
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.factorymethod;
public class SingletonFooFactory {
private static final Foo INSTANCE = new Foo();
public static Foo createInstance() {
return INSTANCE;
}
}

View File

@ -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());
}
}

View File

@ -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);
}
}

View File

@ -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());
}
}

View File

@ -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);
}
}

View File

@ -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>

View File

@ -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>

View File

@ -0,0 +1,17 @@
<?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="bar"
class="com.baeldung.factorymethod.SingletonBarFactory"
factory-method="createInstance">
<constructor-arg value="someName" />
</bean>
</beans>

View File

@ -0,0 +1,15 @@
<?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="foo"
class="com.baeldung.factorymethod.SingletonFooFactory"
factory-method="createInstance" />
</beans>