This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package ch06.springbook.factorybean; | ||
|
||
public class Message { | ||
|
||
private String text; | ||
|
||
private Message(String text) { | ||
this.text = text; | ||
} | ||
|
||
public String getText() { | ||
return text; | ||
} | ||
|
||
public static Message newMessage(String text) { | ||
return new Message(text); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/ch06/springbook/factorybean/MessageFactoryBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package ch06.springbook.factorybean; | ||
|
||
import org.springframework.beans.factory.FactoryBean; | ||
|
||
public class MessageFactoryBean implements FactoryBean<Message> { | ||
|
||
private String text; | ||
|
||
public void setText(String text) { | ||
this.text = text; | ||
} | ||
|
||
public Message getObject() throws Exception { | ||
return Message.newMessage(this.text); | ||
} | ||
|
||
public Class<? extends Message> getObjectType() { | ||
return Message.class; | ||
} | ||
|
||
public boolean isSingleton() { | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/test/java/ch06/springbook/factorybean/FactoryBeanTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package ch06.springbook.factorybean; | ||
|
||
import static org.hamcrest.core.Is.is; | ||
import static org.junit.Assert.assertThat; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
|
||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@ContextConfiguration(locations = "/applicationContext.xml") | ||
public class FactoryBeanTest { | ||
|
||
@Autowired | ||
private ApplicationContext applicationContext; | ||
|
||
@Test | ||
public void getMessageFromFactoryBeanTest() { | ||
Object message = applicationContext.getBean("message"); | ||
assertThat(Message.class.isInstance(message), is(true)); | ||
assertThat(((Message)message).getText(), is("Factory Bean")); | ||
} | ||
|
||
@Test | ||
public void getFactoryBeanTest() { | ||
Object factory = applicationContext.getBean("&message"); | ||
assertThat(MessageFactoryBean.class.isInstance(factory), is(true)); | ||
} | ||
} |