Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
[ch05] FactoryBean example.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhsim86 committed Sep 9, 2017
1 parent 249bba7 commit 6fdfcdb
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/main/java/ch06/springbook/factorybean/Message.java
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 src/main/java/ch06/springbook/factorybean/MessageFactoryBean.java
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;
}
}
4 changes: 4 additions & 0 deletions src/main/resources/applicationContext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@
<property name="userService" ref="userServiceImpl" />
</bean>

<bean id="message" class="ch06.springbook.factorybean.MessageFactoryBean">
<property name="text" value="Factory Bean" />
</bean>

</beans>
32 changes: 32 additions & 0 deletions src/test/java/ch06/springbook/factorybean/FactoryBeanTest.java
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));
}
}

0 comments on commit 6fdfcdb

Please sign in to comment.