开发一个全新的项目,需要先进行开发环境的搭建
要确定技术框架以及版本,还要考虑各个框架之间的版本兼容问题,对新项目进行配置以及测试
受到Ruby On Rails、Node.js等技术的影响,JavaEE领域需要一种更为简单的开发方式,所以推出了SpringBoot
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.atm.cloudgroupId>
<artifactId>atm-testartifactId>
<packaging>warpackaging>
<version>0.0.1-SNAPSHOTversion>
<name>atm-test Maven Webappname>
<url>http://maven.apache.orgurl>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>3.8.1version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<version>1.5.4.RELEASEversion>
dependency>
dependencies>
<build>
<finalName>atm-testfinalName>
build>
project>
package com.atm.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class,args);
}
}
package com.atm.cloud;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@GetMapping("/hello")
@ResponseBody
public String hello(){
return "Hello World";
}
}
package com.atm.cloud;
public class Person {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
package com.atm.cloud;
//import org.springframework.stereotype.Controller;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
//@Controller
@RestController
public class MyController {
@GetMapping("/hello")
@ResponseBody
public String hello() {
return "Hello World";
}
@RequestMapping(value = "/person/{personId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public Person findPerson(@PathVariable("personId") Integer personId) {
Person person = new Person();
person.setId(personId);
person.setName("atm");
person.setAge(18);
return person;
}
}
test.user.name=1
package com.atm.cloud;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class PropController {
@Autowired
private ApplicationContext context;
@GetMapping("/getProp")
@ResponseBody
public String getProp(){
System.out.println("Come into getProp");
return context.getEnvironment().getProperty("test.user.name");
}
}
server.port=80
package com.atm.cloud;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = new SpringApplicationBuilder(
MyApplication.class)
.properties(
"spring.config.location=classpath:/atm-test/application.properties")
.run(args);
/*
ConfigurableApplicationContext context = new SpringApplicationBuilder(
MyApplication.class)
.properties(
"spring.profiles.active=linux")
.run(args);
*/
/*
ConfigurableApplicationContext context = new SpringApplicationBuilder(
MyApplication.class)
.properties(
"spring.profiles.active=linux","server.port=8081")
.run(args);
*/
//SpringApplication.run(MyApplication.class, args);
}
}
package com.atm.cloud;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = new SpringApplicationBuilder(
MyApplication.class)
.properties(
"spring.config.location=classpath:/atm-test/application.yml")
.run(args);
//SpringApplication.run(MyApplication.class, args);
}
}
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.atm.cloudgroupId>
<artifactId>atm-testartifactId>
<packaging>warpackaging>
<version>0.0.1-SNAPSHOTversion>
<name>atm-test Maven Webappname>
<url>http://maven.apache.orgurl>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>3.8.1version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<version>1.5.4.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<version>1.5.4.RELEASEversion>
<optional>trueoptional>
dependency>
dependencies>
<build>
<finalName>atm-testfinalName>
build>
project>
<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.0modelVersion>
<groupId>com.atm.cloudgroupId>
<artifactId>atm_testartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<name>atm_testname>
<description>Demo project for Spring Bootdescription>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.5.10.RELEASEversion>
<relativePath/>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<version>1.5.4.RELEASEversion>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<fork>truefork>
configuration>
plugin>
plugins>
build>
project>