美文网首页
spring实际操作(1)

spring实际操作(1)

作者: IAmWhoAmI | 来源:发表于2018-07-29 13:57 被阅读45次

https://docs.spring.io/spring-boot/docs/2.0.3.RELEASE/reference/htmlsingle/#getting-started

根据上面的文章,开始搭建spring服务。

package bikePlane.solicitor;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

import java.util.Arrays;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        return 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);
            }
        };
    }

}
/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2018 All Rights Reserved.
 */
package bikePlane.solicitor;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 作为数据的主要入口,通过该入口将参数传入.
 * @author qz.zzm
 * @version $Id: MainEntry.java, v 0.1 2018年07月29日 下午1:08 qz.zzm Exp $
 */
@RestController
@EnableAutoConfiguration
public class MainEntry {

    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }
    public static void main(String[] args) throws Exception {
        SpringApplication.run(MainEntry.class, args);
    }
}
<?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>com.bikePlane</groupId>
    <artifactId>lawyer</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.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>
    <!--<repositories>-->
        <!--<repository>-->
            <!--<id>spring-releases</id>-->
            <!--<url>https://repo.spring.io/libs-release</url>-->
        <!--</repository>-->
    <!--</repositories>-->
    <!--<pluginRepositories>-->
        <!--<pluginRepository>-->
            <!--<id>spring-releases</id>-->
            <!--<url>https://repo.spring.io/libs-release</url>-->
        <!--</pluginRepository>-->
    <!--</pluginRepositories>-->
</project>

其中关于注释的部分:
执行 mvn spring-boot:run
可以正常执行。

但是如果
Application上面的 @SpringBootApplication 注释去掉。那么就会报错:

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.0.2.RELEASE:run (default-cli) on project gs-spring-boot: Execution default-cli of goal org.springframework.boot:spring-boot-maven-plugin:2.0.2.RELEASE:run failed: Unable to find a single main class from the following candidates [hello.HelloController, hello.Application] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException

这个时候,添加了,后面注释掉的maven就能正确执行。
说明这两个使用了不同的包,而我本地没有这俄国包。从远端拉。
https://blog.csdn.net/mr_ooo/article/details/54341184
在这个网站中找到的解决方法。
其实就是强制指定了pluginRepositories的maven库,说明本地里面的包缺失了部分内容。

但是,其实原因是因为两个都有main函数,实验了下,

删除@SpringBootApplication 的情况:

-情况1:注释掉 Application 类中的 main : 执行正确
-情况2:注视掉 MainEntry类中的main:
报错:

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.0.3.RELEASE:run (default-cli) on project lawyer: An exception occurred while running. null: InvocationTargetException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean. -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

问题说明,情况1的时候,执行了main函数,然后,执行
SpringApplication.run(Application.class, args); 之后,在spring启动环节中尝试找:

        String[] beanNames = this.getBeanFactory().getBeanNamesForType(ServletWebServerFactory.class);

但是没有找到注释 @SpringBootApplication 或是其他的相关的注解的类,所以找到有效的类,然后失败了,而第二个方式使用了@EnableAutoConfiguration ,走了默认配置,所以执行成功了。

@EnableAutoConfiguration

一旦使用该项目,则使用spring自动构造默认配置,然后使用默认配置。

$ jar tvf target/lawyer-1.0-SNAPSHOT.jar

$ java -jar target/lawyer-1.0-SNAPSHOT.jar

相关文章

网友评论

      本文标题:spring实际操作(1)

      本文链接:https://www.haomeiwen.com/subject/tovzmftx.html