前言
跟着网上spring教程学习一个入门实例
项目结构
步骤
创建一个maven project,输入Group Id:com.yiibai Artifact Id:HelloSpring
右击项目——poperties——Java Coompiler
pom.xml
<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>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
</dependencies>
</project>
HelloWorld.java
package com.yiibai.totorial.spring.helloworld;
public interface HelloWorld {
public void sayHello();
}
HelloWorldService.java
package com.yiibai.totorial.spring.helloworld;
public class HelloWorldService {
private HelloWorld helloWorld;
public HelloWorld getHelloWorld() {
return helloWorld;
}
public void setHelloWorld(HelloWorld helloWorld){
this.helloWorld=helloWorld;
}
}
SpringHelloWorld.java
package com.yiibai.totorial.spring.helloworld.impl;
import com.yiibai.totorial.spring.helloworld.HelloWorld;
public class SpringHelloWorld implements HelloWorld {
@Override
public void sayHello(){
System.out.println("Spring Say Hello!");
}
}
StrutsHelloWorld.java
package com.yiibai.totorial.spring.helloworld.impl;
import com.yiibai.totorial.spring.helloworld.HelloWorld;
public class StrutsHelloWorld implements HelloWorld {
@Override
public void sayHello(){
System.out.println("Struts Say Hello!");
}
}
HelloProgram.java
package com.yiibai.totorial.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.yiibai.totorial.spring.helloworld.HelloWorld;
import com.yiibai.totorial.spring.helloworld.HelloWorldService;
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();
}
}
beans.xml
<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.yiibai.totorial.spring.helloworld.impl.SpringHelloWorld"></bean>
<bean id="strutsHelloWorld"
class="com.yiibai.totorial.spring.helloworld.impl.StrutsHelloWorld"></bean>
<!-- spring say Hello-->
<bean id="helloWorldService"
class="com.yiibai.totorial.spring.helloworld.HelloWorldService">
<property name="helloWorld" ref="springHelloWorld"/>
</bean>
</beans>
执行Run As Java Application
beans.xml配置文件改为
<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.yiibai.totorial.spring.helloworld.impl.SpringHelloWorld"></bean>
<bean id="strutsHelloWorld"
class="com.yiibai.totorial.spring.helloworld.impl.StrutsHelloWorld"></bean>
<!-- spring say Hello
<bean id="helloWorldService"
class="com.yiibai.totorial.spring.helloworld.HelloWorldService">
<property name="helloWorld" ref="springHelloWorld"/>
</bean>
-->
<!-- struts say Hello-->
<bean id="helloWorldService"
class="com.yiibai.totorial.spring.helloworld.HelloWorldService">
<property name="helloWorld" ref="strutsHelloWorld"/>
</bean>
</beans>
执行后结果为: