结构:
HelloWorld.java:
package com.spring.test.helloWorld;public interface HelloWorld { public void sayHello(); }
HelloWorldService.java:
package com.spring.test.helloWorld;public class HelloWorldService { private HelloWorld helloWorld; public HelloWorldService() { } public void setHelloWorld(HelloWorld helloWorld) { this.helloWorld = helloWorld; } public HelloWorld getHelloWorld() { return this.helloWorld; } }
SpringHelloWorld.java:
package com.spring.test.helloWorld.impl;import com.spring.test.helloWorld.HelloWorld;public class SpringHelloWorld implements HelloWorld { @Override public void sayHello() { System.out.println("Spring Say Hello!!"); } }
StrutsHelloWorld.java:
package com.spring.test.helloWorld.impl;import com.spring.test.helloWorld.HelloWorld;public class StrutsHelloWorld implements HelloWorld { @Override public void sayHello() { System.out.println("Struts Say Hello!!"); } }
beans.xml:
pom.xml
4.0.0 com.spring Spring 0.0.1-SNAPSHOT org.springframework spring-core 4.1.4.RELEASE org.springframework spring-context 4.1.4.RELEASE
HelloProgram.java:
package com.spring.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.spring.test.helloWorld.HelloWorld;import com.spring.test.helloWorld.HelloWorldService;public class HelloProgram { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");//读取beans.xml 文件来创建一个应用程序上下文对象 HelloWorldService service = (HelloWorldService) context.getBean("helloWorldService"); HelloWorld hw= service.getHelloWorld(); hw.sayHello(); }}
运行HelloProgram.java
修改beans.xml:
运行HelloProgram.java: