Dashboard > GreenPepper Open Documentation Refactoring > ... > 8. Developer examples > Spring Example
  GreenPepper Open Documentation Refactoring Log In View a printable version of the current page.  
  Spring Example
Added by Laurent Cobos, last edited by Francois Denommee on Feb 12, 2010  (view change)
Labels: 
(None)

The Spring container system under development

Spring is an Inversion Of Control (IOC) container get more information about it on Spring website

SpringSystemUnderDevelopment code can be found in greenpepper-extensions-spring: SpringSystemUnderDevelopment.java

package com.greenpepper.extensions.spring;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;

import com.greenpepper.reflect.DefaultFixture;
import com.greenpepper.reflect.Fixture;
import com.greenpepper.systemunderdevelopment.DefaultSystemUnderDevelopment;

public class SpringSystemUnderDevelopment extends DefaultSystemUnderDevelopment
{
    private BeanFactory beanFactory;

    public SpringSystemUnderDevelopment(String... applicationCtxes)
    {
        this.beanFactory = new GreenPepperXMLAplicationContext(applicationCtxes).getBeanFactory();
    }
	
    public SpringSystemUnderDevelopment(BeanFactory beanFactory)
    {
        this.beanFactory = beanFactory;
    }

    @Override
    public Fixture getFixture(String fixtureName, String... params) throws Throwable
    {
		Fixture fixture;
		if (params.length != 0)
        {
		    // When params are used, we use the PlainOldSystemUnderDevelopment to instantiate the fixture
			// these params are passed to the constructor
            fixture = super.getFixture(fixtureName, params);
        }
		else
		{
	        try
	        {
	            fixture = new DefaultFixture(beanFactory.getBean(fixtureName));
	        }
	        catch (NoSuchBeanDefinitionException e)
	        {
	            fixture = new DefaultFixture(instantiateAsAutowiredBean(fixtureName));
	        }
		}
		
		return fixture;
    }

    private Object instantiateAsAutowiredBean(String fixtureName) throws Exception
    {
        Class fixtureClass = loadType(fixtureName).getUnderlyingClass();

        BeanDefinition beanDef = new RootBeanDefinition(fixtureClass, RootBeanDefinition.AUTOWIRE_AUTODETECT);
        
        DefaultListableBeanFactory fallFactory = new DefaultListableBeanFactory(beanFactory);
        fallFactory.registerBeanDefinition(fixtureName, beanDef);

        return fallFactory.getBean(fixtureName);
    }
}

DEMONSTRATION LICENSE - This Confluence site is for demonstration purposes only. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.4.3 Build:#705 Mar 21, 2007) - Bug/feature request - Contact Administrators