利用spring boot建立java app

利用spring boot建立java app

背景

在使用spring框架開發的過程當中,隨着功能以及業務邏輯的日益複雜,應用伴隨着大量的XML配置和複雜的bean依賴關係,特別是在使用mvc的時候各類配置文件錯綜複雜。隨着spring3.0的發佈,spring IO團隊開始放棄使用XML配置文件,而使用"約定優先配租"的思想來代替。java

spring boot 就是在這樣的背景中抽象出來的開發框架。它和sping已經大量的經常使用第三方庫集成在一塊兒,幾乎能夠零配置使用,使開發流程更方便mysql

Hello world

使用傳統的spring mvc,須要配置web.xml, applicationContext.xml,而後打包爲war 在tomcat中運行, 而若是使用 spring boot, 一切都變得簡單起來了。web

下面使用Maven來建立spring boot 的web app工程spring

pom.xmlsql

<?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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.springframework</groupId>
  <artifactId>gs-spring-boot</artifactId>
  <version>0.1.0</version>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.3.RELEASE</version>
  </parent>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>

  </dependencies>

  <properties>
    <java.version>1.8</java.version>
  </properties>


  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

</project>

HelloController數據庫

package hello;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class HelloController {

    @RequestMapping("/")
    public String index() {
        return "Greetings from Spring Boot!";
    }
}

其中:apache

  1. @RestController 表示使用spring mvc 來接收request請求
  2. @RequestMapping 映射到主頁
  3. 當請求返回的時候,是純文本,那是由於@RestController是由@Controller 和 @ResponseBody組成

Applicationjson

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);

        System.out.println("Let's inspect the beans provided by Spring Boot:");
    }

}

其中:tomcat

  1. @SpringBootApplication 表明了其有四個註解組成: @Configuration, @EnableAutoConfiguration, @EnableWebMvc, @ComponentScan
  2. 在SpringApplication.run中會去自動啓動tomcat
  3. run 方法返回上下文, 在這個上下文中能夠拿到全部的bean

沒有一行配置代碼、也沒有web.xml。基於Spring Boot的應用在大多數狀況下都不須要咱們去顯式地聲明各種配置,而是將最經常使用的默認配置做爲約定,在不聲明的狀況下也能適應大多數的開發場景。服務器

整體而言spring boot 是對 java web app 開發的簡化

單元測試

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MockServletContext.class)
@WebAppConfiguration
public class HelloControllerTest {

    private MockMvc mvc;
@Before
public void before() throws Exception {
    mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();
} 

@After
public void after() throws Exception { 
} 

/** 
* 
* Method: index() 
* 
*/ 
@Test
public void testIndex() throws Exception { 
//TODO: Test goes here...
    mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andExpect(content().string(equalTo("Greetings from Spring Boot!")));
}
    
}

創建restfullweb服務器

接上, 使用srping boot創建web服務器就很是簡單了, 首先創建一個pojo類

public class Greeting {

    private final long id;
    private final String content;
}

而後使用control來handle http請求

@RestController
public class GreetingController {

    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));
    }

}

其中:

  1. @RequestParam 代表了參數要求,若是要必填則設置required=true
  2. 返回是一個對象,會被自動轉換爲json

當咱們訪問:

  1. http://localhost:8080/greeting 時候返回 {"id":1,"content":"Hello, World!"}
  2. http://localhost:8080/greeting?name=User 時候返回 {"id":2,"content":"Hello, User!"}

數據庫訪問

另外一個很是經常使用的問題。在傳統開發中, 咱們須要配置:

  1. 類路徑上添加數據訪問驅動
  2. 實例化DataSource對象, 指定url, username, password
  3. 注入JdbcTemplate對象,若是使用Mybatis,還要配置框架信息

下面一個例子講述用用spring boot來代替。 數據訪問層咱們將使用Spring Data JPA和Hibernate(JPA的實現之一)。

開始以前先介紹兩個概念

spring data

爲了簡化程序與數據庫交互的代碼,spring提供了一個現成的dao層框架,spring家族提供的spring-data適用於關係型數據庫和nosql數據庫 ;
例如 Spring Data JPA, Spring Data Hadoop, Spring Data MongoDB ,Spring Data Solr 等;
具體的能夠參考官網:http://projects.spring.io/spring-data/;
他們的共同特色是給咱們提供了框架代碼,spring Data能自動建立實體dao的實現類和自定義查詢,再也不須要咱們本身去實現了

jpa

JPA全稱爲Java持久性API(Java Persistence API),JPA是Java EE 5標準之一,是一個ORM規範,由廠商來實現該規範,目前有hibernate、OpenJPA、TopLink、EclipseJPA等實現

而後咱們進入配置環節, 配置很簡單:

一: pom.xml映入 data-jpa 和mysql-connetor

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

二: 配置application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/testor
spring.datasource.username=root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.password = xxx

# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

其中, hibernate的ddl-auto=update配置表名,數據庫的表和列會自動建立

寫下實體類:

@Entity
@Table(name="student")
public class Student {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private  long id;

    @NotNull
    private String name;
    private String age;
}
  1. @Entity,說明被這個註解修飾的類應該與一張數據庫表相對應,表的名稱能夠由類名推斷,固然了,也能夠明確配置,只要加上@Table(name = "books")便可。須要特別注意,每一個Entity類都應該有一個protected訪問級別的無參構造函數,用於給Hibernate提供初始化的入口。
  2. @Id and @GeneratedValue:@Id註解修飾的屬性應該做爲表中的主鍵處理、@GeneratedValue修飾的屬性應該由數據庫自動生成,而不須要明確指定。
  3. @ManyToOne, @ManyToMany代表具體的數據存放在其餘表中,在這個例子裏,書和做者是多對一的關係,書和出版社是多對一的關係,所以book表中的author和publisher至關於數據表中的外鍵;而且在Publisher中經過@OneToMany(mapped = "publisher")定義一個反向關聯(1——>n),代表book類中的publisher屬性與這裏的books造成對應關係。
  4. @Repository 用來表示訪問數據庫並操做數據的接口,同時它修飾的接口也能夠被component scan機制探測到並註冊爲bean,這樣就能夠在其餘模塊中經過@Autowired織入。

dao:

@Repository
public interface CustomerRepository extends CrudRepository<Customer, Long> {

    List<Customer> findByLastName(String lastName);
}

詳細的能夠看 spring jpa的具體介紹。

最後使用:

@RestController
public class DbController {
    @Autowired
    private StudentDao dao;

    @RequestMapping("/get-student-counts")
    @ResponseBody
    public String getAllStudents(){
        List<Student> students = (List<Student>) dao.findAll();
        return String.format("%d", students.size());
    }
}

主要一點是: 我在CustomerRepository 實現中天天添加方法:findByLastName, @Autowired 就會一直報錯。

相關文章
相關標籤/搜索