【问题标题】:Dependency Management in Spring BootSpring Boot 中的依赖管理
【发布时间】:2016-08-28 09:16:08
【问题描述】:

我是 Spring Boot 的新手。我提到了this guide

我正在使用 Spring Tool Suite (STS)。当我在 STS 中创建我的第一个“Spring Legacy Project”时,已经存在以下 POM 文件:

<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>org.springframework.samples.service.service</groupId>
  <artifactId>SpringBoot</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

    <properties>

        <!-- Generic properties -->
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <!-- Web -->
        <jsp.version>2.2</jsp.version>
        <jstl.version>1.2</jstl.version>
        <servlet.version>2.5</servlet.version>


        <!-- Spring -->
        <spring-framework.version>3.2.3.RELEASE</spring-framework.version>

        <!-- Hibernate / JPA -->
        <hibernate.version>4.2.1.Final</hibernate.version>

        <!-- Logging -->
        <logback.version>1.0.13</logback.version>
        <slf4j.version>1.7.5</slf4j.version>

        <!-- Test -->
        <junit.version>4.11</junit.version>

    </properties>

    <dependencies>

        <!-- Spring MVC -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring-framework.version}</version>
        </dependency>

        <!-- Other Web dependencies -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>${servlet.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>${jsp.version}</version>
            <scope>provided</scope>
        </dependency>

        <!-- Spring and Transactions -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring-framework.version}</version>
        </dependency>

        <!-- Logging with SLF4J & LogBack -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
            <scope>runtime</scope>
        </dependency>

        <!-- Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>


        <!-- Test Artifacts -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring-framework.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

    </dependencies> 
</project>

现在,根据我对 Spring Boot 的理解,它为我管理所有添加所需的依赖项。在我提到的the link 中,我被告知在创建项目后立即将以下内容添加到项目中:

<?xml version="1.0" encoding="UTF-8"?>
<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>org.springframework</groupId>
    <artifactId>gs-spring-boot</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <properties>
        <java.version>1.8</java.version>
    </properties>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

我的问题是, Spring Boot pom 文件的上述内容是否必须与现有内容一起添加,还是应该剪切旧内容并粘贴这个新的??如果 Spring Boot 要为我管理我的依赖项,那么现有的依赖项不应该存在,对吧??

提前致谢。

弹簧配置

package config;

import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

public class SpringConfig {

    public void addResourceHandlers(ResourceHandlerRegistry reg)
    {
        reg.addResourceHandler("/resources/**/").addResourceLocations("/resources/**");
    }

    public InternalResourceViewResolver viewResolver()
    {
        InternalResourceViewResolver vr = new InternalResourceViewResolver();
        vr.setPrefix("/WEB-INF/view/");
        vr.setSuffix(".jsp");
        return vr;
    }

}

WebAppInit

package config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class WebAppInit extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        // TODO Auto-generated method stub
        return new Class[]{SpringConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected String[] getServletMappings() {
        // TODO Auto-generated method stub
        return new String[]{"/"};
    }

}

控制器

package hello;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @RequestMapping("/")
    public String index() {
        return "Greetings from Spring Boot!";
    }
}

应用程序(主)

package hello;

import java.util.Arrays;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
         ApplicationContext ctx = SpringApplication.run(Application.class, args);
         System.out.println("Let's inspect the beans provided by Spring Boot:");

         String[] beanNames = ctx.getBeanDefinitionNames();
          Arrays.sort(beanNames);
            for (String beanName : beanNames) {
                System.out.println(beanName);
            }
    }

}

index.jsp

<!DOCTYPE html>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>


<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>

<html>
    <head>
        <meta charset="utf-8">
        <title>Welcome</title>
    </head> 
    <body>

        Hello
    </body>
</html>

【问题讨论】:

    标签: java spring-boot


    【解决方案1】:

    据我所知,您现有的 POM 不包含任何不受 Spring 管理的依赖项,因此您应该将旧的 POM 替换为新的、Spring Boot 管理的 POM。

    一般来说,最好的方法是从您现有的 POM 开始,并一一删除托管依赖项。

    【讨论】:

    • 我应该如何运行这个应用程序?作为 Java 应用程序?或者,既然有视图,我应该在服务器上运行它 - 'Run On Server' ?
    • 作为一个SpringBoot应用,如果应用会寻找main方法,是否应该作为Java应用运行?
    • 这完全取决于您 - Spring Boot 允许您选择。但默认情况下,您通过 main 方法运行 Spring Boot 应用程序。您可以查看official documentation了解更多详情。
    • 这可能是因为您没有正确修改配置。 Spring Boot 允许您选择如何运行应用程序,但它不会自动支持部署到服务器。看看docs.spring.io/spring-boot/docs/current/reference/html/…spring.io/guides/gs/convert-jar-to-war
    • 我在我的手机上,明天之前我可能无法试一试;在那之前,你能把你的代码推送到 GitHub 上吗?复制和运行会更容易。但我现在可以告诉你的是,你使用的是@RestController,所以即使你的配置没问题,你也无法显示索引页面。
    猜你喜欢
    • 2022-12-23
    • 1970-01-01
    • 2020-09-10
    • 2021-12-29
    • 2018-08-24
    • 2019-02-26
    • 2017-09-29
    • 1970-01-01
    • 2021-07-05
    相关资源
    最近更新 更多