Skip to content

Commit b3a231f

Browse files
committed
Part III.Using Spring boot - 14.Structuring Your code
1 parent 18972b2 commit b3a231f

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# 14. Structuring Your Code
2+
3+
# 14. 组织你的代码
4+
5+
Spring Boot does not require any specific code layout to work. However, there are some best practices that help.
6+
7+
Spring Boot不需要任何代码布局来工作。然而,有很多最佳实践可以提供帮助。
8+
9+
### Using the "default" Package
10+
11+
### 使用“默认”包
12+
13+
When a class does not include a package declaration, it is considered to be in the "default package". The use of the "default package" is generally discouraged and should be avoided. It can cause particular problems for Spring Boot applications that use the `@ComponentScan`, `@EntityScan`, or `@SpringBootApplication` annotations, since every class from every jar is read.
14+
15+
当一个类没有包含任何包声明时,它将使用“默认的包”。通常不建议使用“默认包”。对于使用`@ComponentScan`, `@EntityScan`, or `@SpringBootApplication`注解情况,它可能会导致每个jar的类都会被读取的特殊的问题。
16+
17+
##### Tip
18+
19+
##### 小提示
20+
21+
> We recommend that you follow Java's recommended package naming conventions and use a reversed domain name (for example, com.example.project).
22+
23+
> 我们推荐你遵循Java推荐的包命名约定:使用一个反向域名(比如,com.example.project).
24+
25+
### Location the Main Application Class
26+
27+
### 定位应用程序入口类
28+
29+
We generally recommend that you locate your main application class in a root package above other classes. The `@SpringBootApplication` annotation is often placed on your main class, and it implicitly defines a base "search package" for certain items. For example, if you are writing a JPA application, the package of the `@SpringBootApplication` annotated class is used to search fo `@Entity` items. Using a root package also allows component scan to apply only on your project.
30+
31+
我们通常建议你将饮用程序入口类定位在其他类上方的根节点包中。`@SpringBootApplication`注解放在你的入口类中,它隐式地为某些项目定义了一个基本的“搜索包”。例如,如果你编写一个JAP应用时,则使用`@SpringBootApplication`注解类的包来搜索`@Entity`项目。使用根节点包也允许组建扫描适用于你的项目。
32+
33+
##### Tip
34+
35+
##### 小提示
36+
37+
> If you don't want to use `@SpringBootApplication`, the `@EnableAutoConfiguration` and `@ComponentScan` annotations that it imports defines that behaviour so you can also use that instaead.
38+
39+
> 如果你不想使用`@SpringBootApplication`,则可以导入`@EnableAutoConfiguration``@ComponentScan` 注解定义该行为来代替它。
40+
41+
The following listing shows a typical layout:
42+
43+
下面展示了一个典型的布局:
44+
45+
```
46+
com
47+
+ - example
48+
+ - myapplication
49+
|
50+
+ - customer
51+
| + - Customer.java
52+
| + - CustomerController.java
53+
| + - CustomerService.java
54+
| + - CustomerRepository.java
55+
|
56+
+ - Order
57+
| + - Order.java
58+
| + - OrderController.java
59+
| + - OrderService.java
60+
| + - OrderRepository.java
61+
```
62+
63+
The `Application.java` file would declare the main method, along with the basic `@SpringBootApplication`, as follows:
64+
65+
`Application.java`文件中声明了入口函数和基本的`@SpringBootApplication`,如下所示:
66+
67+
```java
68+
package com.example.myapplication;
69+
70+
import org.springframework.boot.SpringApplication;
71+
import org.springframework.boot.autconfigure.SpringBootApplication;
72+
73+
@SpringBootApplication
74+
public class Application {
75+
public static void main(String[] args) {
76+
SpringApplication.run(Application.class, args);
77+
}
78+
}
79+
```
80+
81+
82+
83+
84+

0 commit comments

Comments
 (0)