SpringBoot是做微服務架構非常好的一套框架,下面來講講我的第一個SpringBoot項目; 1、依賴 - <properties>
- <spring.boot.version>1.3.2.RELEASE</spring.boot.version>
- </properties>
- dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- <version>${spring.boot.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter</artifactId>
- <version>${spring.boot.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- <version>${spring.boot.version}</version>
- </dependency>
- <dependency>
- <groupId>org.assertj</groupId>
- <artifactId>assertj-core</artifactId>
- <version>3.4.1</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- <version>2.5</version>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <version>${spring.boot.version}</version>
- <executions>
- <execution>
- <goals>
- <goal>repackage</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
2、Application類,這是SpringBoot啟動服務的類; - package simple;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.boot.CommandLineRunner;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import simple.service.ExampleOneService;
-
- /**
- * Created by LK on 2016/4/24.
- */
- @SpringBootApplication
- public class SimpleApplication implements CommandLineRunner{
-
- @Autowired
- @Qualifier("exampleOneService")
- private ExampleOneService exampleOneService;
-
- public void run(String... strings) throws Exception {
- System.out.println(this.exampleOneService.getExampleOneMessage());
- if(strings.length > 0 && strings[0].equals("exitcode")){
- throw new ExitException();
- }
- }
- public static void main(String[] args) {
- SpringApplication.run(SimpleApplication.class,args);
- }
- }
3、由于上面需要引用服務才可以獲得內容,所以我們也得創建一個服務類; - package simple.service;
-
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
-
- /**
- * Created by LK on 2016/4/24.
- */
- @Component
- public class ExampleOneService {
- @Value("${name:我的第一個SpringBoot}") //name:后面的值可以在resources里面的application.properties中配置,如果不配置,那么久默認為程序里面的值
- private String name;
-
- public String getExampleOneMessage(){
- return "看," + name;
- }
- }
4、上面說application.properties,那么我們還得創建一個這樣子的文件,并配置好需要的信息; 5、去到 - SimpleApplication 類中,右鍵運行main運行項目,又或者Teminal控制臺執行mvn spring-boot:run可以運行項目
- </pre><pre code_snippet_id="1660186" snippet_file_name="blog_20160424_7_6717521" name="code" class="java">
|