莫大人

搭建 spring 项目

参考原文:http://blog.csdn.net/binyao02123202/article/details/20387595

 

1.新建maven web 工程

 

2.编辑pom.xml添加依赖

 

                <dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.0.2.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.0.2.RELEASE</version>
		</dependency>        

  

3.配置spring监听器

编辑web.xml添加监听器

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  
  
  <!-- 设置Spring容器加载所有的配置文件的路径 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:applicationContext.xml</param-value>
	</context-param>
	
	<!-- 加载Spring容器配置,Spring监听器 -->
	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
</web-app>

  

4. applicationContext.xml

<?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"
	xmlns:context="http://www.springframework.org/schema/context" 
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.0.xsd ">
	
	<bean id="person" class="com.yun.entity.Person">
			<property name="name" value="张三"></property>
			<property name="age" value="33"></property>
	</bean>
	
	<bean id="helloWorld" class="com.yun.util.test.impl.IHelloWorldImpl">
		<property name="person" ref="person"></property>
	</bean>
	
</beans>

  

5.编写测试类 

package com.yun.entity;

public class Person {

	private String name;
	
	private int age;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
	
}

  

接口 IHelloWorld

package com.yun.util.test;

public interface IHelloWorld {

	String sayHello();
	
}

  

接口实现 IHelloWorldImpl

package com.yun.util.test.impl;

import com.yun.entity.Person;
import com.yun.util.test.IHelloWorld;

public class IHelloWorldImpl implements IHelloWorld{
	
	private Person person;
	
	public String sayHello() {
		return "hello world!"+person.getName()+",i am "+person.getAge();
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	
}

 

测试类 JunitTest

package com.yun.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.yun.util.test.IHelloWorld;

public class JunitTest {

	@Test
	public void test(){
		System.out.println("......................");
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		IHelloWorld helloWorld = (IHelloWorld)context.getBean("helloWorld");
		System.out.println(helloWorld.sayHello());
	}
}

  运行结果

 

6.工程中应用了 junit 如下添加junit

选中工程右键-->Properties-->Java Build Path-->add Library-->JUnit-->next

 

7.如果想透彻理解,请参考博文开头的参考文章。

工程将在百度云盘中给出。

百度云: http://pan.baidu.com/s/1pLvvxQb

 

posted on 2017-01-12 14:42  莫大人  阅读(150)  评论(0编辑  收藏  举报

导航