Skip to content

Instantly share code, notes, and snippets.

@harschware
Last active December 17, 2018 15:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harschware/111416de4c8fe53689b8c5ca15140a67 to your computer and use it in GitHub Desktop.
Save harschware/111416de4c8fe53689b8c5ca15140a67 to your computer and use it in GitHub Desktop.
import org.springframework.aop.target.HotSwappableTargetSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
/**
* Inspired by: https://stackoverflow.com/a/12808171/154461
*/
public class SwappableBeans {
public static void main(String... args) {
ApplicationContext ctx = new GenericXmlApplicationContext("swappableBeans.xml");
Person personFromXml = (Person) ctx.getBean("personFromXml");
System.out.println(String.format("personFromXml: Hello '%s', age '%s'", personFromXml.getName(), personFromXml.getAge()));
Person personProxy = (Person) ctx.getBean("personProxy");
System.out.println(String.format("personProxy: Hello '%s', age '%s'", personProxy.getName(), personProxy.getAge()));
Person lori = new Person("Lori", 49);
HotSwappableTargetSource personSwapper = (HotSwappableTargetSource) ctx.getBean("personSwapper");
Person previousPerson = (Person) personSwapper.swap(lori);
System.out.println(String.format("previousPerson: Hello '%s', age '%s'", previousPerson.getName(), previousPerson.getAge()));
personFromXml = (Person) ctx.getBean("personFromXml");
System.out.println(String.format("personFromXml: Hello '%s', age '%s'. Still same!", personFromXml.getName(), personFromXml.getAge()));
personProxy = (Person) ctx.getBean("personProxy");
System.out.println(String.format("personProxy: Hello '%s', age '%s'. Was swapped!", personProxy.getName(), personProxy.getAge()));
/** OUTPUT:
* personFromXml: Hello 'Tim', age '46'
* personProxy: Hello 'Tim', age '46'
* previousPerson: Hello 'Tim', age '46'
* personFromXml: Hello 'Tim', age '46'. Still same!
* personProxy: Hello 'Lori', age '49'. Was swapped!
*/
} // end main
} // end class
public class Person {
private String name;
private Integer age;
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
}
<?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">
<!--https://stackoverflow.com/questions/12800769/replace-spring-bean-in-one-context-with-mock-version-from-another-context-->
<bean id="personFromXml" class="harschware.sandbox.Person">
<constructor-arg index="0" value="Tim"/>
<constructor-arg index="1" type="java.lang.Integer" value="46"/>
</bean>
<bean id="personSwapper" class="org.springframework.aop.target.HotSwappableTargetSource">
<constructor-arg ref="personFromXml"/>
</bean>
<bean id="personProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="targetSource" ref="personSwapper"/>
</bean>
</beans>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment