maven搭建spring项目

前提

安装jdk

安装maven

安装eclipse

创建maven项目


搭建spring项目

pom.xml

xml 复制代码
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
	<version>5.0.4.RELEASE</version>
</dependency>

新建hellospring.java

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

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

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>

运行项目

相关推荐
霸王龙的小胳膊1 小时前
SpringMVC-请求和响应
java·mvc
二两小咸鱼儿2 小时前
Java Demo - JUnit :Unit Test(Assert Methods)
java·后端·junit
字节源流2 小时前
【spring】配置类和整合Junit
java·后端·spring
跪在镜子前喊帅3 小时前
【面试】Java 多线程
java·面试
好看资源平台3 小时前
Java/Kotlin逆向基础与Smali语法精解
java·开发语言·kotlin
zimoyin3 小时前
解决 Java/Kotlin 资源加载问题
java·python·kotlin
阿木看源码5 小时前
bindingAdapter的异常错误
java·开发语言
跪在镜子前喊帅5 小时前
【面试】框架
java·面试
~Yogi5 小时前
每日学习Java之一万个为什么
java·开发语言·学习
Simon523145 小时前
数据结构---八大排序
java·数据结构·排序算法