纯spring项目
目录结构
子项目builde.gradle
buildscript {
repositories {
mavenLocal()
mavenCentral()
maven { url "http://repo.spring.io/release" }
maven { url "http://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/libs-snapshot" }
maven { url "http://repo.spring.io/milestone" }
maven { url "https://repo.spring.io/libs-milestone" }
}
dependencies {
classpath boot.springBootPlugin
}
}
apply plugin: 'org.springframework.boot'
dependencies {
compile boot.starter
}
jar {
manifest {
attributes("Created-By" : "Iuliana Cosmina",
"Specification-Title": "Pro Spring 5",
"Main-Class" : "com.apress.prospring5.ch4.Application",
"Class-Path" : configurations.compile.collect { it.getName() }.join(' '))
}
}
boot.springBootPlugin行引用了父项目的 build.gradle文件中定义的 boot数组的 springBootPlugin属性。该文件的 内容片段(仅与 SpringBoot相关的配置)如下所示:
ext{
...
bootVersion = '2.0.6.RELEASE'
...
boot = [
springBootPlugin: "org.springframework.boot:spring-boot-gradle-plugin:$bootVersion",
starter:"org.springframework.boot:spring-boot-starter:$bootVersion",
...
}
package com.apress.prospring5.ch4;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
* Created by iuliana.cosmina on 3/19/17.
*/
@Component
public class HelloWorld {
private static Logger logger = LoggerFactory.getLogger(HelloWorld.class);
public void sayHi() {
logger.info("Hello World!");
}
}
package com.apress.prospring5.ch4;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import java.util.Arrays;
/**
* Created by iuliana.cosmina on 3/19/17.
*/
@SpringBootApplication
public class Application {
private static Logger logger = LoggerFactory.getLogger(Application.class);
public static void main(String... args) throws Exception {
ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);
assert (ctx != null);
logger.info("The beans you were looking for:");
// listing all bean definition names
Arrays.stream(ctx.getBeanDefinitionNames()).forEach(logger::info);
HelloWorld hw = ctx.getBean(HelloWorld.class);
hw.sayHi();
System.in.read();
ctx.close();
}
}
spring web项目
文件结构
子项目build.gradle目录
buildscript {
repositories {
mavenLocal()
mavenCentral()
maven { url "http://repo.spring.io/release" }
maven { url "http://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/libs-snapshot" }
maven { url "http://repo.spring.io/milestone" }
maven { url "https://repo.spring.io/libs-milestone" }
}
dependencies {
classpath boot.springBootPlugin
}
}
apply plugin: 'org.springframework.boot'
dependencies {
compile boot.starterWeb
}
jar {
manifest {
attributes("Created-By" : "Iuliana Cosmina",
"Specification-Title": "Pro Spring 5",
"Main-Class" : "com.apress.prospring5.ch4.WebApplication",
"Class-Path" : configurations.compile.collect { it.getName() }.join(' '))
}
}
package com.apress.prospring5.ch4.ctrl;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by iuliana.cosmina on 3/19/17.
*/
@RestController
public class HelloWorld {
@RequestMapping("/")
public String sayHi() {
return "Hello World!";
}
}
package com.apress.prospring5.ch4;
import com.apress.prospring5.ch4.ctrl.HelloWorld;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import java.util.Arrays;
/**
* Created by iuliana.cosmina on 3/19/17.
*/
@SpringBootApplication(scanBasePackageClasses = HelloWorld.class)
public class WebApplication {
private static Logger logger = LoggerFactory.getLogger(WebApplication.class);
public static void main(String... args) throws Exception {
ConfigurableApplicationContext ctx = SpringApplication.run(WebApplication.class, args);
assert (ctx != null);
logger.info("Application started...");
System.in.read();
ctx.close();
}
}