啥也不说了,先来项目的目录把。
先说一下者几个文件之间的关系,如图所示。
上面的图已经说的很明白了,不再说了。
下面把所有的代码复制过来,中间不再有文字叙述,毕竟不能打扰大家复制代码哈,,,
HelloProgram.java
package com.fei.springDo; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.fei.springInterface.HelloWorld; import com.fei.springIoC.HelloWorldService; public class HelloProgram { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); HelloWorldService service = (HelloWorldService) context.getBean("helloWorldService"); HelloWorld hw = service.getHelloWorld(); hw.sayHello(); } }
SpringHelloWorld.java
package com.fei.springImp; import com.fei.springInterface.HelloWorld; public class SpringHelloWorld implements HelloWorld { public void sayHello() { // TODO Auto-generated method stub System.out.println("spring mvc is here"); } }
StrutsHelloWorld.java
package com.fei.springInterface; public interface HelloWorld { public void sayHello(); }
HelloWorld.java
package com.fei.springInterface; public interface HelloWorld { public void sayHello(); }
HelloWorldService.java
package com.fei.springIoC; import com.fei.springInterface.HelloWorld; public class HelloWorldService { private HelloWorld helloWorld; public HelloWorldService(){ } public HelloWorld getHelloWorld() { return helloWorld; } public void setHelloWorld(HelloWorld helloWorld) { this.helloWorld = helloWorld; } }
beans.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="springHelloWorld" class="com.fei.springImp.SpringHelloWorld"></bean> <bean id="strutsHelloWorld" class="com.fei.springImp.StrutsHelloWorld"></bean> <bean id="helloWorldService" class="com.fei.springIoC.HelloWorldService"> <property name="helloWorld" ref="strutsHelloWorld"/> </bean> </beans> 运行HelloProgram.java,run as java application,得到下面的结果。
这是输出了StrutsHelloWorld 类中的 内容。
大家尝试一下,能不能输出SpringHelloWorld类中的内容。
@@@@@@@@@@@@@@@@@@@@@@@@@收工了@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@