Created
September 20, 2012 12:24
-
-
Save bijukunjummen/3755598 to your computer and use it in GitHub Desktop.
Factory Method with Argument
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
package org.bk.samples.factorymethod; | |
public class A { | |
private C c; | |
public A(C c){ | |
this.c = c; | |
} | |
public String toString(){ | |
return c.toString(); | |
} | |
} |
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
package org.bk.samples.factorymethod; | |
public class B { | |
public C getInstance(String name){ | |
return new C(name); | |
} | |
} |
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
package org.bk.samples.factorymethod; | |
public class C { | |
private String val; | |
public C(String val){ | |
this.val = val; | |
} | |
public String toString(){ | |
return this.val; | |
} | |
} |
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
package org.bk.samples.factorymethod; | |
import static org.junit.Assert.*; | |
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(locations="classpath:/org/bk/samples/factorymethod/factorymethodtest.xml") | |
public class FactoryMethodTest { | |
@Autowired A a; | |
@Test | |
public void testFactoryMethod() { | |
System.out.println(a.toString()); | |
} | |
} |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> | |
<bean name="b" class="org.bk.samples.factorymethod.B" /> | |
<bean name="a" class="org.bk.samples.factorymethod.A"> | |
<constructor-arg type="org.bk.samples.factorymethod.C"> | |
<bean factory-bean="b" factory-method="getInstance"> | |
<constructor-arg value="aparamval"> | |
</constructor-arg> | |
</bean> | |
</constructor-arg> | |
</bean> | |
</beans> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment