Skip to content

Instantly share code, notes, and snippets.

@harschware
Created May 24, 2018 23:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harschware/cc5bd0f3716436ec2e5aca8f17f3302b to your computer and use it in GitHub Desktop.
Save harschware/cc5bd0f3716436ec2e5aca8f17f3302b to your computer and use it in GitHub Desktop.
package harschware.sandbox.scratch;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Arrays;
import java.util.Objects;
/**
* Demonstrates wrapping a java object as a Spring bean
*/
public class AddInstanceToContext {
private static Car theCar = new Car("chevy");
/**
* Main entry point into program
*
* @param args: list of args
*/
public static void main(String[] args) {
ApplicationContext ctx = createSpringContext(theCar);
System.out.println(Arrays.asList(ctx.getBeanDefinitionNames()));
SwappableBeans.Person personFromXml = (SwappableBeans.Person) ctx.getBean("personFromXml");
System.out.println("Person = " + personFromXml);
Car chevy = (Car) ctx.getBean("carFromXml");
System.out.println("Car = " + chevy);
System.out.println("Are the cars equal? " + theCar.equals(chevy));
theCar.setType("Toyota");
System.out.println("Was type changed? " + chevy);
}
public static ApplicationContext createSpringContext(final Car car) {
CarHolder carHolder = new CarHolder(car);
ApplicationContext context = new ClassPathXmlApplicationContext("scratch/applicationContext-addBean.xml");
return context;
} // end static method
public static class Car {
String type;
public Car(String type) {
this.type = type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Car{");
sb.append("type='").append(type).append('\'');
sb.append('}');
return sb.toString();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Car car = (Car) o;
return Objects.equals(type, car.type);
}
@Override
public int hashCode() {
return Objects.hash(type);
}
} // end class
public static class CarFactory implements FactoryBean<Car> {
@Override
public Car getObject() throws Exception {
return CarHolder.getCar();
}
@Override
public Class<?> getObjectType() {
return Car.class;
}
@Override
public boolean isSingleton() {
return true;
}
} // end class
public static class CarHolder {
static Car car;
public CarHolder(Car ctx) {
this.car = ctx;
}
public static Car getCar() {
if (car == null) {
throw new IllegalStateException("CarHolder not initialized");
}
return car;
}
} // end class
} // end class
<?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.scratch.SwappableBeans.Person">
<constructor-arg index="0" value="Tim"/>
<constructor-arg index="1" type="java.lang.Integer" value="46"/>
</bean>
<bean id="carFromXml" class="harschware.sandbox.scratch.AddInstanceToContext$CarFactory" />
</beans>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment