File System Resource Demo : IoC Resource « Spring « Java






File System Resource Demo

/*
Pro Spring
By Rob Harrop
Jan Machacek
ISBN: 1-59059-461-4
Publisher: Apress
*/


///////////////////////////////////////////////////////////////////////////////////////
//File: beans.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean id="oracle" name="wiseworm" class="BookwormOracle"/>
</beans>


///////////////////////////////////////////////////////////////////////////////////////
public interface Oracle {

    public String defineMeaningOfLife();
}


///////////////////////////////////////////////////////////////////////////////////////
public class Encyclopedia {

}
///////////////////////////////////////////////////////////////////////////////////////

public class BookwormOracle implements Oracle {

    private Encyclopedia enc;

    public void setEncyclopedia(Encyclopedia enc) {
        this.enc = enc;
    }

    public String defineMeaningOfLife() {
        return "Encyclopedia's are a waste of money - use the Internet";
    }

}
///////////////////////////////////////////////////////////////////////////////////////
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.FileSystemResource;

public class XmlConfig {

    public static void main(String[] args) {
        DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
        XmlBeanDefinitionReader rdr = new XmlBeanDefinitionReader(factory);
        rdr.loadBeanDefinitions(new FileSystemResource("build/beans.xml"));
        Oracle oracle = (Oracle)factory.getBean("oracle");
    }
}

           
       








FileSystemResourceDemo.zip( 1,198 k)

Related examples in the same category

1.Spring Load Resource from file, classpath and URL
2.Message Source