MicroSpring |
| Home |
MicroSpring was written in a weekend, as a standalone IOC container which is compatible with the Spring Framework's XML config format.
This means you can play with IOC, take the MicroSpring code and very easily change it - it's a tiny code base. It also means you can switch to the real Spring Framework with very little pain if you need to.
This html page, and the one page guide, and that's all. It would help if you understood Inversion Of Control as well!
It is tiny compared with Spring as a whole, and it doesn't even do all the IOC things, so what does it do?
Calling refresh on the FileSystemXmlApplicationContext will read the config file. Compare class names with new class names on all singletons, and if different, destroy the old singleton. It will then call all the property setters again on existing singletons, followed by the onApplicationEvent (if its an application listener). Finally it tells any child contexts to refresh. i.e. it doesn't call the init method again, so if you want common behavior on start up and on refresh do it yourself. The reset behaviour is the only concrete advantage of setter injection over constructor injection that I have seen (the big downside being the risk of null ptrs if your config is wrong).
The refresh function is also called on startup. This in turn calls broadcasts the ContextRefreshedEvent to all singleton=true, lazy-init=false, where the class implements ApplicationListener.
In fact the simplest thing to do is read the 1 page Spring crib sheet, MicroSpring does everything on this sheet ! And everything on that sheet is Spring best practice (as stated in their own documentation).
The class paths for the interfaces are tallsoft rather than spring. And you should comment out the doctype line of the xml config file.
The import's not matching are annoying, but this is simply solved by refactoring, or by renaming MicroSpring to be compatible. I do not presume to do this in my source code. After all MicroSpring is not endorsed or supported by the Spring Framework team.
There are two things going on here:
<?xml version="1.0" encoding="UTF-8"?>
<!-- !DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"-->
<beans>
<!-- example of list and map and ref and property values -->
<bean id="cityFinder" class="com.tallsoft.springeg.CityScape" singleton="true">
<property name="cityMap">
<map>
<entry key="LDN"><value>London</value></entry>
<entry key="FFT"><value>Frankfurt</value></entry>
</map>
</property>
</bean>
<!-- example of list and depends on for creation ordering-->
<bean id="region" class="com.tallsoft.springeg.RegionInfo" depends-on="cityFinder">
<property name="cityFinder"><ref bean="cityFinder"/></property>
<property name="regions">
<list>
<value>Europe</value>
<value>America</value>
</list>
</property>
</bean>
<!-- example of template, and the init method -->
<bean id="templateGeography" abstract="true"
class="com.tallsoft.springeg.BaseGeography">
<property name="planetName"><value>Earth</value></property>
<property name="system"><value>sol</value></property>
</bean>
<bean id="earth" class="com.tallsoft.springeg.PlanetInfo"
parent="templateGeography" />
<bean id="mars" class="com.tallsoft.springeg.PlanetInfo"
parent="templateGeography" init-method="initialize">
<!-- overriding the value here -->
<property name="planetName"><value>Mars</value></property>
</bean>
</beans>
try {
FileSystemXmlApplicationContext factory = new FileSystemXmlApplicationContext("example.xml");
RegionInfo r = (RegionInfo)factory.getBean("region");
} catch (Exception ex) {
ex.printStackTrace(System.out);
}