一、准备工作
下载Spring库
二、新建web项目
其中:HelloProgram 为主程序,HelloWorldService 作为接口和两个实现类的中间类,依靠spring的注入,通过配置,决定service中运行的具体是哪个实现类。
三、具体代码
接口类HelloWorld
package com.yibai.spring.helloworld; public interface HelloWorld { public void sayHello(); }
实现类SpringHelloWorld
package com.yibai.spring.helloworld.impl; import com.yibai.spring.helloworld.HelloWorld; public class SpringHelloWorld implements HelloWorld{ @Override public void sayHello() { System.out.println("spring hello world"); } }
实现类StrutsHelloWorld
package com.yibai.spring.helloworld.impl; import com.yibai.spring.helloworld.HelloWorld; public class StrutsHelloWorld implements HelloWorld{ @Override public void sayHello() { System.out.println("struts hello world"); } }
service类
package com.yibai.spring.helloworld; public class HelloWorldService { private HelloWorld helloWorld; //通过service bean中注入 不同的实现类,来实现不同的逻辑 public HelloWorldService(){ } public HelloWorld getHelloWorld() { return helloWorld; } public void setHelloWorld(HelloWorld helloWorld) { this.helloWorld = helloWorld; } }
main类
package com.yibai.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.yibai.spring.helloworld.HelloWorld; import com.yibai.spring.helloworld.HelloWorldService; public class HelloProgram { public static void main(String[] args) { // 创建上下文对象 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloWorldService service = (HelloWorldService) context.getBean("helloWorldService"); HelloWorld helloWorld = service.getHelloWorld(); helloWorld.sayHello(); } }
四、web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>spring01</display-name> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
Spring配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <bean id="springHelloWorld" class="com.yibai.spring.helloworld.impl.SpringHelloWorld"> </bean> <bean id="strutsHelloWorld" class="com.yibai.spring.helloworld.impl.StrutsHelloWorld"> </bean> <bean id="helloWorldService" class="com.yibai.spring.helloworld.HelloWorldService"> <!-- 通过 ref属性,注入需要的bean,该属性会通过 set方法注入 --> <property name = "helloWorld" ref="strutsHelloWorld" /> </bean> </beans>
相关包
五、运行HelloProgram类,改变spring配置,执行不同的实现类。