Skip to content

Instantly share code, notes, and snippets.

@michalkowol
Last active August 29, 2015 14:10
Show Gist options
  • Select an option

  • Save michalkowol/360bab621fa243e67894 to your computer and use it in GitHub Desktop.

Select an option

Save michalkowol/360bab621fa243e67894 to your computer and use it in GitHub Desktop.
<?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-3.0.xsd">
<bean id="apple" class="Apple" />
<bean id="juicer" class="Juicer">
<constructor-arg ref="apple" />
<constructor-arg ref="peeler" />
</bean>
<bean id="peeler" class="Peeler">
<constructor-arg ref="apple" />
</bean>
</beans>
<!-- ... -->
<bean id="juicer" class="Juicer" scope="prototype">
<!-- ... -->
public interface Fruit {
void eat();
String getName();
}
public interface Peelable {
void peel();
}
public class Apple implements Fruit, Peelable {
public void peel() { }
public void eat() { }
String getName() { return "apple"; }
}
public class Peeler {
private final Peelable peelable;
public Peeler(Peelable peelable) {
this.peelable = peelable;
}
}
public class Juicer {
private final Peelable peelable;
private final Peeler peeler;
public Juicer(Peelable peelable, Peeler peeler) {
this.peelable = peelable;
this.peeler = peeler;
}
}
public class MainAppPrototype {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans_prototype.xml");
Juicer j1 = context.getBean(Juicer.class);
Juicer j2 = context.getBean(Juicer.class);
System.out.println(j1 == j2);
}
}
public class MainAppSingleton {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Juicer j1 = context.getBean(Juicer.class);
Juicer j2 = context.getBean(Juicer.class);
System.out.println(j1 == j2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment