Category: Spring

Declaring Spring Programmatically

I’ve always been a big fan of Picocontainer because it has a lovely programmatic interface as an IoC container, and I generally value programmatic APIs over external representations, particularly for config files (note that it doesn’t mean external representations aren’t useful).

Unfortunately the followers of Spring love writing their code in XML, and many of their examples don’t show what you need to do to wire up all their examples declaratively in code. I spent some time trying to work out how to do some things.

Bean with an id ‘myBean’

<bean id="myBean" class="com.thekua.example.SomeClass">

in code:

GenericBeanDefinition someClassDefinition = BeanDefinitionBuilder.rootBeanDefinition(SomeClass.class).getBeanDefinition();
getDefaultListableBeanFactory().register("myBean", someClassDefinition);

Links that I used:

I’ll add more examples for the more complicated items later.