-
点击File---New----project
選擇Maven--Project SDK 選擇1.8--勾選 Create from archetype
-- 展開org.apache.maven.archetypes:maven-architype-quickstart
-- 選擇 maven-archetype-quickstart:RELEASE
-- 最後點擊 Next
image.png 在Groupid中填入项目的包名即可(com.xag)。Artifactid 填入項目名(spring_sample)
image.png
-
配置 maven 環境 & 新建 archetypeCatalog:internal 屬性
image.png -
配置項目名(spring_sample)及項目位置
image.png -
完成項目的創建
image.png
6 .瀏覽默認結構
image.png
在main文件夹下创建一个resources文件夹:右键main,new->Directory
右键新建的resources文件夹 -> Make Directory As -> Resources Root 作为资源文件夹, resources下新建META-INF目錄(Directory),META-INF里创建applicationContext.xml配置文件(待編輯pom.xml文件後):new - >XML Configuration File -> Spring Config
注:右上角有个提示需要点击編輯pom.xml 文件,增加如下信息
<!--spring容器-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
- 創建測試代碼(com.xag 下新建 controller package)
--controller 下新建 HelloWord.java
package com.xag.controller;
public class HelloWord
{
public String name;
public void setName(String name) {
this.name = name;
}
public void hello()
{
System.out.print("hello:" + name);
}
}
--新建 Main_Hello.java
package com.xag.controller;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main_Hello
{
public static void main(String[] args)
{
//HelloWord he = new HelloWord();
//1.创建spring的ioc容器对象
ApplicationContext ctx = new ClassPathXmlApplicationContext("META-INF/applicationContext.xml");
//2.从ioc容器中获取bean实例
HelloWord helloWorld = (HelloWord) ctx.getBean("helloWorld");
helloWorld.hello();
}
}
- 在applicationContext.xml中配置bean
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="helloWorld" class="com.xag.controller.HelloWord">
<property name="name" value="xag"></property>
</bean>
</beans>
11.測試運行
image.png
image.png
-
上傳項目到 gitee 上
image.png
image.png
image.png