前提
安装jdk
安装maven
安装eclipse
创建maven项目
搭建spring项目
pom.xml
<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.4.RELEASE</version>
</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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><bean id="helloSpring"class="com.wujialiang.springstu01.HelloSpring"><property name="userName" value="李四"></property></bean>
</beans>
运行项目