2016-01-19 50 views
2

昨天我得到了非常奇怪的春天引導行爲春天開機不等待請求

例如:我試圖使用./gradlew bootRun啓動服務器:

... 
:findMainClass 
:bootRun 

:: Spring Boot ::  (v1.3.1.RELEASE) 

2016-01-19 16:37:15.315 INFO 6118 --- [   main] c.e.server.Application$Companion   : Starting Application.Companion on fake with PID 6118 (/home/user/code/xproject/server/build/classes/main started by user in /home/user/code/xproject/server) 
2016-01-19 16:37:15.320 INFO 6118 --- [   main] c.e.server.Application$Companion   : No active profile set, falling back to default profiles: default 
2016-01-19 16:37:15.400 INFO 6118 --- [   main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.spring[email protected]a3ec6b: startup date [Tue Jan 19 16:37:15 GMT 2016]; root of context hierarchy 
2016-01-19 16:37:17.908 INFO 6118 --- [   main] o.s.j.e.a.AnnotationMBeanExporter  : Registering beans for JMX exposure on startup 
2016-01-19 16:37:17.923 INFO 6118 --- [   main] c.e.server.Application$Companion   : Started Application.Companion in 3.048 seconds (JVM running for 3.421) 
2016-01-19 16:37:17.924 INFO 6118 --- [  Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.spring[email protected]a3ec6b: startup date [Tue Jan 19 16:37:15 GMT 2016]; root of context hierarchy 
2016-01-19 16:37:17.930 INFO 6118 --- [  Thread-2] o.s.j.e.a.AnnotationMBeanExporter  : Unregistering JMX-exposed beans on shutdown 

BUILD SUCCESSFUL 

Total time: 23.756 secs 

所以也沒有等待請求一如既往。我認爲classpath有一些問題,所以我在控制器和服務中使用了@PostConstruct作爲註釋的方法,並且可以確認它們被調用,並且所有依賴關係都在此時被注入。

這裏是我的gradle.build:

buildscript { 
    ext.kotlin_version = '1.0.0-beta-4584' 
    repositories { 
     mavenCentral() 
     jcenter() 
    } 
    dependencies { 
     classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 
     classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.1.RELEASE") 
    } 
} 

apply plugin: 'java' 
apply plugin: 'idea' 
apply plugin: 'spring-boot' 
apply plugin: 'kotlin' 

jar { 
    baseName = 'xproject' 
    version = '0.1.0' 
} 

repositories { 
    mavenCentral() 
    jcenter() 
} 

dependencies { 
    compile("org.springframework.boot:spring-boot-starter-web") 
    compile("org.springframework.boot:spring-boot-starter-tomcat") 
    compile("org.springframework.boot:spring-boot-devtools") 
    compile 'org.springframework.data:spring-data-mongodb:1.8.2.RELEASE' 
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 
    compile "com.restfb:restfb:1.18.1" 

    testCompile("junit:junit") 
    testCompile("org.springframework.boot:spring-boot-starter-test") 

} 

sourceSets { 
    main { 
     java.srcDirs += ['java', 'kotlin'] 
    } 

    test { 
     java.srcDirs += 'test' 
    } 
} 

UPD#1

這裏是應用類(我沒有從一開始接觸它)。

@SpringBootApplication 
open class Application { 
    companion object { 
     @JvmStatic fun main(args: Array<String>) { 
      SpringApplication.run(Application::class.java, *args) 
     } 
    } 
} 

UPD#2

小的自包含的代碼

@RestController 
@SpringBootApplication 
@ComponentScan 
public class GreetingController { 
    public static void main(String[] a) { 
     SpringApplication.run(GreetingController.class, a); 
    } 

    @PostConstruct 
    public void postConstruct() { 
     System.out.println("!! Post construct called"); 
    } 

    private static final String template = "Hello, %s!"; 
    private final AtomicLong counter = new AtomicLong(); 

    @RequestMapping("/greeting") 
    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) { 
     return new Greeting(counter.incrementAndGet(), 
          String.format(template, name)); 
    } 
} 

給我幾乎

:: Spring Boot ::  (v1.3.1.RELEASE) 

2016-01-19 17:18:19.790 INFO 8230 --- [ restartedMain] hello.GreetingController     : Starting GreetingController on fake with PID 8230 (/home/user/code/xproject/server/build/classes/main started by user in /home/user/code/xproject/server) 
2016-01-19 17:18:19.814 INFO 8230 --- [ restartedMain] hello.GreetingController     : No active profile set, falling back to default profiles: default 
2016-01-19 17:18:19.918 INFO 8230 --- [ restartedMain] s.c.a.AnnotationConfigApplicationContext : Refreshing org.spring[email protected]71ceb8: startup date [Tue Jan 19 17:18:19 GMT 2016]; root of context hierarchy 
!! Post construct called 
2016-01-19 17:18:22.860 INFO 8230 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer  : LiveReload server is running on port 35729 
2016-01-19 17:18:22.905 INFO 8230 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter  : Registering beans for JMX exposure on startup 
2016-01-19 17:18:22.922 INFO 8230 --- [ restartedMain] hello.GreetingController     : Started GreetingController in 4.022 seconds (JVM running for 4.949) 
2016-01-19 17:18:22.924 INFO 8230 --- [  Thread-8] s.c.a.AnnotationConfigApplicationContext : Closing org.spring[email protected]71ceb8: startup date [Tue Jan 19 17:18:19 GMT 2016]; root of context hierarchy 
2016-01-19 17:18:22.930 INFO 8230 --- [  Thread-8] o.s.j.e.a.AnnotationMBeanExporter  : Unregistering JMX-exposed beans on shutdown 

Process finished with exit code 1 
+0

請顯示您的「應用程序」或「Application.Companion」類。 – dunni

+0

@dunni我已更新問題 – ruX

+2

您的路徑以某種方式存在,它不檢測tomcat,因此不會立即啓動Web應用程序。 (它應該啓動一個Web內容,而不是一個簡單的上下文,確保沒有任何東西可以轉化爲網絡內容:) –

回答

0

我只是有完全相同的問題,您同樣的結果在我的情況下,我忘了添加spring-bo ot-starter-web作爲依賴。我從一個基本的例子中複製/粘貼了依賴關係,只是有一個spring-boot-starter,我誤解了它。 一旦我添加它,容器正常啓動,我有一個常規的Web應用程序在8080端口監聽。

這是我的第一個非常基本的測試,以便它只是有彈簧引導啓動,網絡,科特林-STDLIB和科特林,反映了作爲依賴和類,看起來像這樣:

@SpringBootApplication 
class Application { 
    private val log = LoggerFactory.getLogger(Application::class.java) 
} 

fun main(args: Array<String>) { 
    SpringApplication.run(Application::class.java, *args) 
} 

從我所看到的在你的代碼中,我能想到的唯一的事情就是你可能需要kotlin-reflection。我有從這blog postthis sample project包括它的想法也使用它... 祝你好運!