Spring Boot如何使项目在Eclipse中的服务器上运行 [英] Spring boot how to make project run on server in eclipse

查看:60
本文介绍了Spring Boot如何使项目在Eclipse中的服务器上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Spring Boot的新手,我的Eclipse中有示例Spring Boot代码IDE.

I am new to Spring Boot, I have sample spring boot code in my Eclipse IDE.

现在运行项目.在项目中有类应用程序",我运行使用以Java身份运行"应用程序,然后在给定端口上运行它.

Now to run the project. In project there is class "Application", I run that using Run As Java Application and then Its running on given port.

但是我希望它使用Eclipse的在服务器上运行"选项来运行,所以每当我尝试在服务器上运行它时,它都会显示404.

But I want it to run using Run on Server option of Eclipse, so whenever I try to run that on server it gives me 404.

请给我任何解决此问题的想法.

Please give me any idea to resolve this issue.

代码:

@ComponentScan
@Configuration
@EnableAutoConfiguration
public class Application extends SpringBootServletInitializer {

@Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(Application.class);
        }

    public static void main(String[] args) {
        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);
        System.out.println(Arrays.toString(beanNames));
    }
}

application.properties

application.properties

server.address=localhost
server.port=8080
server.contextPath=/spring-security-cas
app.service.security=http://localhost:8080/j_spring_cas_security_check
app.service.home=http://localhost:8080/

pom.xml

<?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.esco.demo</groupId>
    <artifactId>spring-security-cas</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>demo-spring-security-cas</name>
    <description>Demo project for Spring Boot</description>

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

    <dependencies>
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-cas</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-taglibs</artifactId>
    </dependency>
    <!-- Usefull for accessing to jsp -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    <!-- END Usefull for accessing to jsp -->
        <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.14.8</version>
    </dependency>
</dependencies>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <start-class>org.esco.demo.ssc.Application</start-class>
        <java.version>1.7</java.version>
    </properties>

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

</project>

注意:在以Java身份运行应用程序的情况下运行应用程序时,项目正在运行,问题是我希望它使用在服务器上运行"选项运行.

Note: Project running when run Application as Run as Java Application, problem is I want it to run using Run on Server option.

推荐答案

如果要将其部署到容器中,请使用

If you want to deploy it to a container, instead of using the embedded container follow this section in the reference guide.

简而言之,步骤是

  1. 使用 war 包装而不是 jar 包装
  2. 让您的 Application 类扩展 SpringBootServletInitializer 并实现 configure 方法.
  3. 将容器依赖项(我猜是tomcat)标记为提供
  1. Use war packaging instead of jar packaging
  2. Let your Application class extends SpringBootServletInitializer and implement the configure method.
  3. Mark the container dependencies (tomcat I guess) as provided

总而言之

  1. < packaging> jar</packaging> 更改为< packaging> war</packaging>
  2. 更改您的应用程序

public class Application extends SpringBootServletInitializer {
    ...
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
    }
}

  • 将tomcat依赖项添加为提供的.

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifcatId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    

  • 有关更多详细信息,请参见参考指南.

    For more elaborate information check the reference guide.

    这篇关于Spring Boot如何使项目在Eclipse中的服务器上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆