Skip to content

Instantly share code, notes, and snippets.

@A-pZ
Last active December 16, 2015 13:19
Show Gist options
  • Save A-pZ/5440752 to your computer and use it in GitHub Desktop.
Save A-pZ/5440752 to your computer and use it in GitHub Desktop.
staticメソッドのファクトリでBeanGenerateする方法。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/tx/spring-context-2.5.xsd "
default-autowire="byName">
<bean id="outer" class="lumi2.spring.SampleFactory" factory-method="createOuter" ></bean>
<bean id="another" class="lumi2.spring.SampleFactory" factory-method="createYetAnother" ></bean>
</beans>
public class Main {
private static final Logger log = Logger.getLogger("Main");
public static void main(String[] args) throws Exception {
Main m = new Main();
m.start();
}
public void start() throws Exception {
log.info("start");
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {
"applicationContext.xml"
});
Outer outer = (Outer) context.getBean("outer");
log.info("conf:" + outer.getConf());
YetAnother another = (YetAnother) context.getBean("another");
log.info("another:" + another);
log.info("end");
}
}
public class SampleFactory {
private SampleFactory() {
}
private static Outer outer = Outer.createInstance();
private static YetAnother another = new YetAnother();
public static Outer createOuter() {
outer.setConf("conf!!!");
return outer;
}
public static YetAnother createYetAnother() {
return another;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment