用maven创建spring的hello工程

关于maven的安装配置参考上一篇博文:http://blog.csdn.net/sidihuo/article/details/54178417

这里用eclipse创建的demo:

1.右键新建工程,选择maven工程,点击next后得到:

用maven创建spring的hello工程_第1张图片

选择默认选中那个quickstart,点击next,生成一个包com.test.maven.spring

用maven创建spring的hello工程_第2张图片

点击finish完成;

2.打开pom.xml文件,第二个选项卡,depend里直接add,输入*spring如果能都搜到就直接添加过来,搜不到说明你本地仓库没有这个jar包,就直接在最后一个选项卡xml中编辑

添加


    	org.springframework
    	spring-context
    	3.1.1.RELEASE
    

依赖就配置完成了;

3.创建source folder:src/test/resources,里面创建一个SpringBeans.xml文件:


 
    
        
    
 

定义了一个HelloWorld类及其bean的属性;

接着就创建HelloWorld这个类,和默认生成的App类在一个包中;

package com.test.maven.spring;

public class HelloWorld {

	private String name;

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

	public void printHello() {
		System.out.println("Spring 3 : Hello ! " + name);
	}
}

在App类中填写代码测试:

package com.test.maven.spring;

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

/**
 * Hello world!
 */
public class App {

	public static void main(String[] args) {
		System.out.println("Hello World!");
		final ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml");

		final HelloWorld obj = (HelloWorld) context.getBean("helloBean");
		obj.printHello();
	}
}

执行此main函数即可,输出

Hello World!
一月 07, 2017 11:13:53 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1e5a36: startup date [Sat Jan 07 23:13:53 CST 2017]; root of context hierarchy
一月 07, 2017 11:13:53 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [SpringBeans.xml]
一月 07, 2017 11:13:53 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1f1ca93: defining beans [helloBean]; root of factory hierarchy
Spring 3 : Hello ! huaizuo
 
  

 
  

表示测试成功;

工程如图:

用maven创建spring的hello工程_第3张图片



你可能感兴趣的:(SSH,Maven)