// Interface HelloWorld public interface HelloWorld { public void sayHello(); } // Class implements HelloWorld public class SpringHelloWorld implements HelloWorld { public void sayHello() { System.out.println("Spring say Hello!"); } } // Other class implements HelloWorld public class StrutsHelloWorld implements HelloWorld { public void sayHello() { System.out.println("Struts say Hello!"); } } // And Service class public class HelloWorldService { // Field type HelloWorld private HelloWorld helloWorld; // Constructor HelloWorldService // It initializes the values for the field 'helloWorld' public HelloWorldService() { this.helloWorld = new StrutsHelloWorld(); } }
IoC = Inversion of Control
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.yiibai</groupId> <artifactId>HelloSpring</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <!-- Spring Core --> <!-- http://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.4.RELEASE</version> </dependency> <!-- Spring Context --> <!-- http://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.4.RELEASE</version> </dependency> </dependencies> </project>
package com.yiibai.tutorial.spring.helloworld; public interface HelloWorld { public void sayHello(); }
package com.yiibai.tutorial.spring.helloworld; public class HelloWorldService { private HelloWorld helloWorld; public HelloWorldService() { } public void setHelloWorld(HelloWorld helloWorld) { this.helloWorld = helloWorld; } public HelloWorld getHelloWorld() { return this.helloWorld; } }
package com.yiibai.tutorial.spring.helloworld.impl; import com.yiibai.tutorial.spring.helloworld.HelloWorld; public class SpringHelloWorld implements HelloWorld { @Override public void sayHello() { System.out.println("Spring Say Hello!!"); } }
package com.yiibai.tutorial.spring.helloworld.impl; import com.yiibai.tutorial.spring.helloworld.HelloWorld; public class StrutsHelloWorld implements HelloWorld { @Override public void sayHello() { System.out.println("Struts Say Hello!!"); } }
package com.yiibai.tutorial.spring; import com.yiibai.tutorial.spring.helloworld.HelloWorld; import com.yiibai.tutorial.spring.helloworld.HelloWorldService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class HelloProgram { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); HelloWorldService service = (HelloWorldService) context.getBean("helloWorldService"); HelloWorld hw= service.getHelloWorld(); hw.sayHello(); } }
<beansxmlns="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"> <beanid="springHelloWorld" class="com.yiibai.tutorial.spring.helloworld.impl.SpringHelloWorld"></bean> <beanid="strutsHelloWorld" class="com.yiibai.tutorial.spring.helloworld.impl.StrutsHelloWorld"></bean> <beanid="helloWorldService" class="com.yiibai.tutorial.spring.helloworld.HelloWorldService"> <propertyname="helloWorld"ref="springHelloWorld"/> </bean> </beans>
<!-- Original --> <beanid="helloWorldService" class="com.yiibai.tutorial.spring.helloworld.HelloWorldService"> <propertyname="helloWorld"ref="springHelloWorld"/> </bean> <!-- Change to: --> <beanid="helloWorldService" class="com.yiibai.tutorial.spring.helloworld.HelloWorldService"> <propertyname="helloWorld"ref="strutsHelloWorld"/> </bean>
ApplicationContext context = newClassPathXmlApplicationContext("beans.xml");
<!-- beans.xml --> <beanid="helloWorldService" class="com.yiibai.tutorial.spring.helloworld.HelloWorldService"> <!-- Call: helloWorldService.setHelloWorld(springHelloWorld) --> <propertyname="helloWorld"ref="springHelloWorld"/> </bean>