eclipse 搭建spring boot框架

本文详细介绍了Spring Boot项目中建立Maven工程及配置工程的步骤。建立工程需通过File - New - Other选择Maven Project,输入相关信息完成创建。配置工程包括新增文件夹、添加配置文件、更改pom.xml文件、更新项目,最后新建入口类运行并通过浏览器访问。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.建立maven工程

①File——New——Other

②选择Maven Project后,点击Next

④.输入Group Id、Artifact Id

⑤.点击Finish后,建立工程如下:

 

二、配置工程:

①.鼠标右键点击工程目录,新增Package,起名为resources (这个文件夹放配置文件用 ,其实不用这个spring boot也能启动)

②.在resources文件夹下,增加文件application.properties,内容如下

server.port是启动的端口。

同样建立conf目录

把resouces和conf目录加到资源目录中

 

 

③.更改pom.xml文件

<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.demo.springboot</groupId>
    <artifactId>spring-boot</artifactId>
    <version>0.0.1-SNAPSHOT</version>


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <!-- 打jar包时候用 -->
    <build>
        <resources>
            <resource>
                <directory>resources</directory>
                <!-- 打包包含 -->
                <includes>
                    <include>application.properties</include>
                </includes>
                <!-- 打包排除 -->
                <excludes>
                    <exclude>*.xml</exclude>
                    <exclude>*.yaml</exclude>
                </excludes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!-- 资源文件拷贝到jar以外 -->
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <!-- 拷贝目标目录,打包时候,拷贝到jar外边的conf,这样该配置的时候就不用更改jar了 -->
                            <outputDirectory>${project.build.directory}/conf</outputDirectory>
                            <resources>
                                <resource>
                                <!-- 配置文件目录   -->
                                    <directory>conf</directory>
                                    <filtering>true</filtering>
                                </resource>
                            </resources>
                        </configuration>

                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

 

④.右键点工程Maven——Update Project

⑤.新建Application.java作为main入口,@RequestMapping("/")作http访问用根目录地址

 

package com.neusoft;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
@EnableAutoConfiguration
@ComponentScan
public class Application implements EmbeddedServletContainerCustomizer{
//implements EmbeddedServletContainerCustomizer
    

    public static void main(String[] args) {
        // http://localhost:8080/
        SpringApplication.run(Application.class, args);
    }

    public void customize(ConfigurableEmbeddedServletContainer arg0) {
        //arg0.setPort(8081);
        
    }
    
    @RequestMapping("/")
    public String hello(){
        return "Greetings from Spring Boot!";
    }
    
    @RequestMapping("/first.do")
    String home() {
        return "Hello World!";
    }
}
 

保存后,运行

打开浏览器,访问

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值