Spring入门——Helloworld

前言

  跟着网上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>

 执行后结果为:

工作原理




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 19
    评论
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值