maven搭建spring项目

前提

安装jdk
安装maven
安装eclipse

创建maven项目

maven搭建spring项目_第1张图片
maven搭建spring项目_第2张图片

搭建spring项目

pom.xml

<dependency>
	<groupId>org.springframeworkgroupId>
	<artifactId>spring-contextartifactId>
	<version>5.0.4.RELEASEversion>
dependency>

新建hellospring.java

package com.wujialiang.springstu01;

public class HelloSpring {
	private String userName;

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}
	
	public void show() {
		System.out.println(userName+" 你好");
	}
}

修改App.java

package com.wujialiang.springstu01;

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

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
    	ApplicationContext ctx =new 
    			ClassPathXmlApplicationContext("applicationContext.xml");
        HelloSpring hello =(HelloSpring)ctx.getBean("helloSpring");
        hello.show();
    }
}

main下新建resources文件夹,该文件下新建applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	   http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
	<bean id="helloSpring"
		class="com.wujialiang.springstu01.HelloSpring">
		<property name="userName" value="李四">property>
	bean>
beans>

运行项目
maven搭建spring项目_第3张图片

你可能感兴趣的:(java,maven,spring,java)