• 搭建 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

  • 相关阅读:
    NERDTree 快捷键辑录
    linux 自动登录脚本
    INSERT INTO .. ON DUPLICATE KEY更新多行记录
    Linux环境PHP7.0安装
    SVN常用命令
    Linux下查看文件和文件夹大小
    Cacti安装详细步骤
    linux回到上次目录与历史命令查找快捷方式
    shell的if else 以及大于,小于等逻辑表达式
    Nginx_Lua
  • 原文地址:https://www.cnblogs.com/yun965861480/p/6278193.html
Copyright © 2020-2023  润新知