项目目录结构如下:
HelloWorld.java
package com.thief.demo; public class HelloWorld { public void sayHello() {
System.out.println("hello world!");
} }
helloworld-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
<beans>
<bean id="helloworld" class="com.thief.demo.HelloWorld"/>
</beans>
Test.java
package com.thief.demo; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; public class Test { public static void main(String[] args) { DefaultListableBeanFactory beanRegistry = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanRegistry);
reader.loadBeanDefinitions("classpath:com/thief/demo/helloworld-config.xml");
BeanFactory container = (BeanFactory)beanRegistry; HelloWorld helloworld = (HelloWorld)container.getBean("helloworld");
helloworld.sayHello(); } }