如何在Intellij IDEA中创建Spring Maven项目

27
我正在尝试在IntelliJ IDEA中创建Spring + Maven项目。 我已经阅读了这些官方Wiki,但是当我完成步骤时,我的项目中没有Maven。 我尝试手动添加它,但似乎我的能力不够好:(,因为Maven依赖项、lib文件夹和我想要使用的类过着自己的生活(依赖项不在lib文件夹中,我尝试从依赖项中键入类,但没有自动补全)。 有人有逐步指南或链接吗?
4个回答

24

2
这个 https://start.spring.io 网站只适用于基于Spring Boot的应用程序,对吗? - Menuka Ishan
@MenukaIshan 是的,你说得对。 - jhyot
你指的是哪个开发文件夹?我可以在任何地方提取它吗? - Jay Dangar
1
@JayDangar 是的,你可以选择任何地方。 - jhyot
谢谢兄弟,这个答案真的帮了我很多。点赞并应该被接受为最佳答案。 - Jay Dangar

16

首先,通过点击“文件”->“新建”->“项目”,选择Spring来创建一个Spring项目。接着,要添加Maven支持,你可以右键点击项目,选择“添加框架支持”。这将弹出一个窗口,在其中选择“maven”。


7

我认为这是你要找的内容吧?

创建新项目 文件 > 新建 > 项目

  • 在新项目对话框的左侧单击 Maven
  • 选择 Create from archetype
  • 点击 Add Archetype 按钮
  • 将 Group Id 设置为 pl.codeleak
  • 将 Artifact Id 设置为 spring-mvc-quickstart
  • 将 Version 设置为 1.0.0
  • 将 Repository 设置为 http://kolorobot.github.io/spring-mvc-quickstart-archetype
  • 点击下一步并创建项目

欲了解更多,请参阅以下链接。

https://github.com/kolorobot/spring-mvc-quickstart-archetype

但是我更倾向于经典方式。使用 arch-type 生成 maven 项目,然后将 maven 项目导入 Intellij,最后在 pom.xml 中添加 Spring Maven 依赖项。这样更加清晰易懂。


-1
  1. 如果您想创建使用Maven的Spring-boot:大多数人使用Spring Initilzr。

    • 使用以下任一网站:https://start.spring.io/ - 您将获得打包的内容 - 最佳方式
    • 或者在IntelliJ Ultimate Edition中(个人最喜欢的工具):文件>新建>项目...>Spring Initilzr
    • 最后,您可以自己创建Maven + 添加带有spring-boot依赖项的POM.xml
  2. 如果您想从头开始创建使用Maven的Spring非boot:

    • 只需简单地选择 文件>新建>项目...>Maven ... [任何原型]
    • 编辑pom.xml,使其具有SPring依赖项:
在这种情况下(更困难的方式)- 从POM.xml开始进行Spring测试:
[ 在此处查看完整代码和整个IntelliJ转储:https://github.com/wkaczurba/stackoverflow/tree/springmaveninintellij ]
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.stackoverflow</groupId>
  <artifactId>someartifact</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>someartifact</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.13.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>4.3.13.RELEASE</version>
    </dependency>

    <!-- Test-related stuff -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

然后你可以创建你的第一个测试,例如:

package com.stackoverflow;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertTrue;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { AppConfig.class })
public class WhateverTest {

    @Autowired
    YourInterface objectUnderTest;

    @Test
    public void test1() {
        assertTrue(objectUnderTest.func());
    }
}

你的配置 + 接口 + Bean:

package com.stackoverflow;

import com.stackoverflow.YourInterface;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(value="com.stackoverflow")
public class AppConfig {
    // Add whatever is needed.
}

接口:

package com.stackoverflow;

import org.springframework.stereotype.Component;

@Component
public interface YourInterface {
    public boolean func();
}

实现:

package com.stackoverflow;

import org.springframework.stereotype.Component;

@Component
class YourInterfaceImpl implements YourInterface {

    public boolean func() {
        System.out.println("func called..."); // Always use logger in real world...
        return true;
    }
}

运行:

  1. 运行:mvn clean test
  2. 或在IntelliJ中创建配置以运行JUnit测试并运行。

@Configuration(classes={ whatever.class }) 这一行无法编译。 - Kuldeep Yadav
@Kuldeep Yadav - 你需要在那里指定你的配置类。我更新了代码,所以它包含了Configuration类,并提供了GITHUB链接,这样你就知道在哪里查找它了。 - Witold Kaczurba

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接