Spring — Static Factories | Code Factory

Spring Tutorial Index Page: Link

<bean id="c" class="java.util.Calendar" /> // don't create object of singleton like this
<bean id="c" class="java.util.Calendar" factory-method="getInstance" />
<bean id="lf" class="LoggerFactory" factory-method="getLogger" />
<bean id="sf" class="SessionFactory" />
<bean id="s" factory-bean="sf" factory-method="openSession" />
Logger l = getBean("lf");
Session s = getBean("s");

Create Java Project

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="t" class="com.codeFactory.beans.Test" factory-method="getInstance" />
<bean id="c" class="java.util.Calendar" factory-method="getInstance" />
</beans>
package com.codeFactory.beans;/**
* @author code.factory
*
*/
public class Test {
private static Test t;

private Test() {
System.out.println("Private constructor.");
}

public static Test getInstance() {
if(t == null) {
t = new Test();
}
return t;
}

@Override
protected Object clone() throws CloneNotSupportedException {
return new CloneNotSupportedException();
}
}
package com.codeFactory.beans;/**
* @author code.factory
*
*/
public class Test {
private static Test t;

private Test() {
System.out.println("Private constructor.");
}

public static Test getInstance() {
if(t == null) {
t = new Test();
}
return t;
}

@Override
protected Object clone() throws CloneNotSupportedException {
return new CloneNotSupportedException();
}
}
Private constructor.
true
true

Create Java Project

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="cf" class="com.codeFactory.beans.CarFactory" >
<property name="companyName" value="com.codeFactory.beans.JLR" />
</bean>

<bean id="c" factory-bean="cf" factory-method="getInstance" />
</beans>
package com.codeFactory.beans;/**
* @author code.factory
*
*/
public interface Car {
public void drive();
}
package com.codeFactory.beans;/**
* @author code.factory
*
*/
public class CarFactory {
public String companyName;
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
/* instance / non-static factory */
public Car getInstance() throws Exception {
return (Car) Class.forName(companyName).newInstance();
}
}
package com.codeFactory.beans;/**
* @author code.factory
*
*/
public class Audi implements Car {
@Override
public void drive() {
System.out.println("Maximum speed is 200");
}
}
package com.codeFactory.beans;/**
* @author code.factory
*
*/
public class JLR implements Car {
@Override
public void drive() {
System.out.println("Maximum speed is 250");
}
}
package com.codeFactory.test;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.codeFactory.beans.Car;/**
* @author code.factory
*
*/
public class Client {
public static void main(String... args) {
ApplicationContext context = new ClassPathXmlApplicationContext("com/codeFactory/resources/spring.xml");

Car c = (Car) context.getBean("c");
c.drive();
}
}
Maximum speed is 250

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store