1. 程式人生 > >第二章 第二個spring-boot程序

第二章 第二個spring-boot程序

html hid schema client classes 企業應用 引入 獲取 系列

上一節的代碼是spring-boot的入門程序,也是官方文檔上的一個程序。這一節會引入spring-boot官方文檔推薦的方式來開發代碼,並引入我們在spring開發中service層等的調用。

1、代碼結構如下

技術分享

2、pom.xml

技術分享
<?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/maven-v4_0_0.xsd"
> <modelVersion>4.0.0</modelVersion> <groupId>com.xxx</groupId> <artifactId>myboot</artifactId> <version>1.0-SNAPSHOT</version> <properties> <java.version>1.8</java.version><!-- 官方推薦 --> </properties
> <!-- 引入spring-boot-starter-parent做parent是最好的方式, 但是有時我們可能要引入我們自己的parent,此時解決方式有兩種: 1)我們自己的parent的pom.xml的parent設為spring-boot-starter-parent(沒有做過驗證,但是感覺可行) 2)使用springboot文檔中的方式:見spring-boot-1.2.5-reference.pdf的第13頁 --> <parent> <groupId
>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.2.5.RELEASE</version> </parent> <!-- <dependencyManagement> <dependencies> <dependency> Import dependency management from Spring Boot <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.2.5.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> --> <!-- 引入實際依賴 --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <!-- 用於將應用打成可直接運行的jar(該jar就是用於生產環境中的jar) 值得註意的是,如果沒有引用spring-boot-starter-parent做parent, 且采用了上述的第二種方式,這裏也要做出相應的改動 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
View Code

說明:pom.xml文件與上一節的完全一樣。

3、Application.java

技術分享
package com.xxx.firstboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @EnableAutoConfiguration:spring boot的註解,一般只用於主類,
 * 是無xml配置啟動的關鍵部分,明確指定了掃描包的路徑為其修飾的主類的包(這也就是為什麽主類要放在根包路徑下的原因)
 * 
 * @ComponentScan 進行包的掃描,[email protected]
 * 
 * 主類要位於根包路徑下,方便之後的掃描(We generally recommend that you locate your main application class in a root package above other classes.)
 */
@SpringBootApplication        //same as @[email protected][email protected]
public class Application {
    /**
     * spring boot的入口,在整個子項目在內,
     * 只能有一個main方法,否則spring boot啟動不起來
     */
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}
View Code

註意:

  • 主類要位於根包路徑下(例如,com.xxx.firstboot),這是推薦做法,方便掃描
  • 每一個jar(即每一個子項目)都要有一個主方法,用於啟動該jar(也就是一個微服務)
  • [email protected],該註解相當於添加了如下三個註解
    • @Configuration:該註解指明該類由spring容器管理
    • @EnableAutoConfiguration:該註解是無xml配置啟動的關鍵部分
    • @ComponentScan:該註解指定掃描包(如果主類不是位於根路徑下,這裏需要指定掃描路徑),類似於spring的包掃描註解

4、application.properties

技術分享
1 #user info
2 user.id=1
3 user.username=zhaojigang
4 user.password=123
View Code

註意:

  • application.properties文件是spring-boot的默認文件,一般各種配置(包括:數據源配置,httpclient配置等)都配在這裏就好

5、User.java

技術分享
package com.xxx.firstboot.domain;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @ConfigurationProperties(prefix="user")
 * 自動讀取application.properties(是spring-boot默認查找的文件)文件中的user.*的屬性
 * [email protected],[email protected]("${user.id}")來一個個指定屬性的值
 * 
 * 註意:[email protected]@Value,[email protected],
 * 因為在後邊的對該類的使用中,[email protected],這樣這些屬性的自動註入才起作用,
 * 具體使用查看"UserService"
 */
@Component
@ConfigurationProperties(prefix="user")
public class User {
    
    //@Value("${user.id}")
    private int id;
    
    //@Value("wangna")
    private String username;
    
    private String password;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}
View Code

註意:

  • 該類就是一個普通的model,在ssm框架中我們並沒有將這樣的model歸給spring容器去管理,[email protected],這樣在之後的使用中,就可以直接將該model註入到其使用類中。
  • [email protected](prefix="user")註解,這樣的意思就是可以自動掃描application.properties文件相關前綴的配置,並根據名稱配置到該類的每一個屬性上去
  • [email protected],[email protected],如果配置了,@Value註解失效

6、UserService.java

技術分享
package com.xxx.firstboot.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xxx.firstboot.domain.User;

@Service
public class UserService {
    
    @Autowired
    private User user;
    
    public User getUser(){
        return user;
    }

}
View Code

註意:

  • 這裏直接註入了User,這和類正是上邊的那個model

7、UserController.java

技術分享
package com.xxx.firstboot.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xxx.firstboot.domain.User;
import com.xxx.firstboot.service.UserService;
/**
 * @RestController:spring mvc的註解,
 * [email protected]@ResponseBody的合體,可以直接返回json
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/getUser")
    public User getUser() {
        return userService.getUser();
    }

}
View Code

說明:

  • 這個類其實就是開發中,開發一個spring-boot程序的最基本最常用的方式。(在微服務應用中,用到類似於"Java企業應用開發實踐"系列中的父子模塊開發,之後再說)
  • 相對於ssm而言,spring-boot的讀取屬性文件的方式也相當容易,讀取屬性文件常用的三種方式
    • 使用FileUtil去讀:見第一章 屬性文件操作工具類
    • 使用如上的註解實現(最推薦的方式)
    • 使用Environment這個類來獲取就行(這個可能寫錯類名了)

對於spring-boot而言,其本身有很多集成的jar包(見下邊),我們可以根據自己的需求引入相應的jar,但是暫無與mybatis集成的jar。

spring-boot相關的依賴包(可以根據需求自己引入):

技術分享

技術分享

技術分享

技術分享

技術分享

第二章 第二個spring-boot程序