springboot工程结构推荐

项目地址:https://github.com/snjl/springboot.myproject.git

Spring Boot框架本身并没有对工程结构有特别的要求,但是按照最佳实践的工程结构可以帮助我们减少可能会遇见的坑,尤其是Spring包扫描机制的存在,如果您使用最佳实践的工程结构,可以免去不少特殊的配置工作。

典型示例

  • root package结构:com.example.myproject
  • 应用主类Application.java置于root package下,通常我们会在应用主类中做一些框架配置扫描等配置,我们放在root package下可以帮助程序减少手工配置来加载到我们希望被Spring加载的内容
  • 实体(Entity)与数据访问层(Repository)置于com.example.myproject.domain包下
  • 逻辑层(Service)置于com.example.myproject.service包下
  • Web层(web)置于com.example.myproject.web包下
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    com
    +- example
    +- myproject
    +- Application.java
    |
    +- domain
    | +- Customer.java
    | +- CustomerRepository.java
    |
    +- service
    | +- CustomerService.java
    |
    +- web
    | +- CustomerController.java
    |

创建:

image
项目结构:

image

工程构建

在上述基础,构建项目:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Customer.java
package com.example.myproject.domain;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Customer {
@Value("${com.example.myproject.customer.id}")
private String id;
@Value("${com.example.myproject.customer.name}")
private String name;

public String getId() {
return id;
}

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

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
CustomerService.java
package com.example.myproject.service;

import com.example.myproject.domain.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CustomerService {
@Autowired
private Customer customer;

public void printCustomer() {
System.out.println(customer.getName());
}

public Customer getCustomer(){
return customer;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
CustomerController.java
package com.example.myproject.web;

import com.example.myproject.domain.Customer;
import com.example.myproject.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
* @author 34924
*/
@Controller
public class CustomerController {
@Autowired
CustomerService customerService;

@RequestMapping("/customer")
public String customer(ModelMap map) {
Customer customer = customerService.getCustomer();
map.addAttribute("customer", customer);
return "index";
}

}
1
2
3
4
5
6
7
8
9
10
11
12
index.html
<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head lang="en">
<meta charset="UTF-8" />
<title></title>
</head>
<body>
<h1 th:text="${customer.id}">Hello World</h1>
<h2 th:text="${customer.name}">name</h2>
</body>
</html>
1
2
3
4
application.properties

com.example.myproject.customer.id = 001
com.example.myproject.customer.name = jl

运行项目,访问localhost:8080/customer

image

也可以test:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
MyprojectApplicationTests.java

package com.example.myproject;

import com.example.myproject.service.CustomerService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MyprojectApplicationTests {

@Autowired
private CustomerService customerService;

@Test
public void contextLoads() {
}


@Test
public void testCustomer() {
customerService.printCustomer();
System.out.println(customerService.getCustomer());
}
}

输出:

1
2
jl
Customer{id='001', name='jl'}

-------------本文结束 感谢您的阅读-------------