Spring Boot 2.0.0參考手冊(cè)_中英文對(duì)照_Part III_14-18

文章作者:Tyan
博客:noahsnail.com ?|? CSDN ?|? 簡(jiǎn)書

14. Structuring your code

Spring Boot does not require any specific code layout to work, however, there are some best practices that help.

Spring Boot工作時(shí)不要求任何特定的代碼布局米同,但是有一些最佳實(shí)踐是很有幫助的猫十。

14.1 Using the “default” package

When a class doesn’t 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 @ComponentScan, @EntityScan or @SpringBootApplication annotations, since every class from every jar, will be read.

當(dāng)一個(gè)類沒有包含一個(gè)package聲明時(shí),它當(dāng)做是在default package中。通常情況下不建議使用default package架谎,應(yīng)該避免使用它。當(dāng)Spring Boot應(yīng)用使用@ComponentScan崭篡,@EntityScan@SpringBootApplication它會(huì)引起一些特別的問題淮摔,因?yàn)镾pring Boot會(huì)讀取每個(gè)jar中的每個(gè)類。

We recommend that you follow Java’s recommended package naming conventions and use a reversed domain name (for example, com.example.project).

?

我們建議你遵循Java推薦的包命名規(guī)范移怯,使用一個(gè)反轉(zhuǎn)的域名(例如香璃,com.example.project)。

14.2 Locating the main application class

We generally recommend that you locate your main application class in a root package above other classes. The @EnableAutoConfiguration 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 @EnableAutoConfiguration annotated class will be used to search for @Entity items.

通常我們建議你將你的主應(yīng)用類放在其它類之上的根包中舟误。@EnableAutoConfiguration注解經(jīng)常放在你的主類(main class)中葡秒,對(duì)于某些像它隱式的定義了一個(gè)基search package,例如嵌溢,如果你正在寫一個(gè)JPA應(yīng)用眯牧,@EnableAutoConfiguration注解的類所在的包將被用來搜索@Entity項(xiàng)。

Using a root package also allows the @ComponentScan annotation to be used without needing to specify a basePackage attribute. You can also use the @SpringBootApplication annotation if your main class is in the root package.

根包的應(yīng)用也允許使用@ComponentScan注解而不需要指定basePackage特性赖草。如果你的主類是在根包中学少,你也可以使用@SpringBootApplication注解。

Here is a typical layout:

下面是一個(gè)典型的布局:

com
 +- example
     +- myproject
         +- Application.java
         |
         +- domain
         |   +- Customer.java
         |   +- CustomerRepository.java
         |
         +- service
         |   +- CustomerService.java
         |
         +- web
             +- CustomerController.java

The Application.java file would declare the main method, along with the basic @Configuration.

Application.java文件會(huì)聲明main方法和基本的@Configuration秧骑。

package com.example.myproject;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {

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

}

15. Configuration classes

Spring Boot favors Java-based configuration. Although it is possible to call SpringApplication.run() with an XML source, we generally recommend that your primary source is a @Configuration class. Usually the class that defines the main method is also a good candidate as the primary @Configuration.

Spring Boot支持基于Java的注解版确。盡管可以通過XML源調(diào)用SpringApplication.run()方法扣囊,但我們通常建議你主要的源是一個(gè)@Configuration類。

Many Spring configuration examples have been published on the Internet that use XML configuration. Always try to use the equivalent Java-based configuration if possible. Searching for enable* annotations can be a good starting point.

?

網(wǎng)上已經(jīng)發(fā)布了許多使用XML配置來進(jìn)行Spring配置的例子绒疗。但要盡可能的嘗試使用等價(jià)的Java注解侵歇。搜索enable*注解是一個(gè)好的開端。

15.1 Importing additional configuration classes

You don’t need to put all your @Configuration into a single class. The @Import annotation can be used to import additional configuration classes. Alternatively, you can use @ComponentScan to automatically pick up all Spring components, including @Configuration classes.

你不必將所有的@Configuration放到一個(gè)單獨(dú)的類中吓蘑『兄粒可以使用@Import注解來導(dǎo)入額外的配置類∈啃蓿或者枷遂,你可以使用@ComponentScan來自動(dòng)獲得所有的Spring組件,包括@Configuration類棋嘲。

15.2 Importing XML configuration

If you absolutely must use XML based configuration, we recommend that you still start with a @Configuration class. You can then use an additional @ImportResource annotation to load XML configuration files.

如果你絕對(duì)的必須使用基于XML的配置酒唉,我們推薦你仍然從@Configuration類開始。你可以使用額外的@ImportResource注解來加載XML配置文件沸移。

16. Auto-configuration

Spring Boot auto-configuration attempts to automatically configure your Spring application based on the jar dependencies that you have added. For example, If HSQLDB is on your classpath, and you have not manually configured any database connection beans, then we will auto-configure an in-memory database.

Spring Boot自動(dòng)配置會(huì)基于你添加的jar依賴試圖自動(dòng)配置你的Spring應(yīng)用痪伦。例如,如果HSQLDB在你的classpath中雹锣,并且你沒有手動(dòng)的配置任何數(shù)據(jù)庫連接beans网沾,我們將會(huì)在自動(dòng)配置一個(gè)內(nèi)存中的數(shù)據(jù)庫。

You need to opt-in to auto-configuration by adding the @EnableAutoConfiguration or @SpringBootApplication annotations to one of your @Configuration classes.

你需要通過添加@EnableAutoConfiguration@SpringBootApplication注解到你的@Configuration類中的一個(gè)來選擇性的加入自動(dòng)配置蕊爵。

You should only ever add one @EnableAutoConfiguration annotation. We generally recommend that you add it to your primary @Configuration class.

?

你應(yīng)該僅添加一個(gè)@EnableAutoConfiguration注解辉哥。我們通常建議你將它添加到你主要的@Configuration類中。

16.1 Gradually replacing auto-configuration

Auto-configuration is noninvasive, at any point you can start to define your own configuration to replace specific parts of the auto-configuration. For example, if you add your own DataSource bean, the default embedded database support will back away.

自動(dòng)配置是非入侵性的攒射,在任何時(shí)候你都可以開始定義你自己的配置來替換自動(dòng)配置的指定部分醋旦。例如,如果你要添加你自己的DataSource bean会放,默認(rèn)嵌入的數(shù)據(jù)庫支持將會(huì)退出饲齐。

If you need to find out what auto-configuration is currently being applied, and why, start your application with the --debug switch. This will enable debug logs for a selection of core loggers and log an auto-configuration report to the console.

如果你需要找出當(dāng)前正在應(yīng)用的自動(dòng)配置和為什么,你可以用--debug開關(guān)來啟動(dòng)你的應(yīng)用咧最。這將會(huì)使核心日志的輸出級(jí)別變?yōu)閐ebug級(jí)別并輸出一個(gè)自動(dòng)配置報(bào)告到控制臺(tái)捂人。

16.2 Disabling specific auto-configuration

If you find that specific auto-configure classes are being applied that you don’t want, you can use the exclude attribute of @EnableAutoConfiguration to disable them.

如果你發(fā)現(xiàn)正在應(yīng)用特定的你不想使用的自動(dòng)配置類,你可以使用@EnableAutoConfiguration注解的exclude特性來禁用它們矢沿。

import org.springframework.boot.autoconfigure.*;
import org.springframework.boot.autoconfigure.jdbc.*;
import org.springframework.context.annotation.*;

@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MyConfiguration {
}

If the class is not on the classpath, you can use the excludeName attribute of the annotation and specify the fully qualified name instead. Finally, you can also control the list of auto-configuration classes to exclude via the spring.autoconfigure.exclude property.

如果這個(gè)類不在classpath中滥搭,你可以使用這個(gè)注解的excludeName特性并指定全限定名來代替。最后咨察,你也可以通過spring.autoconfigure.exclude屬性來排除论熙,從而控制自動(dòng)配置類的列表。

You can define exclusions both at the annotation level and using the property.

?

你也可以在注解級(jí)別或使用屬性來定義排除項(xiàng)摄狱。

17. Spring Beans and dependency injection

You are free to use any of the standard Spring Framework techniques to define your beans and their injected dependencies. For simplicity, we often find that using @ComponentScan to find your beans, in combination with @Autowired constructor injection works well.

你可以自由的使用任何標(biāo)準(zhǔn)的Spring框架技術(shù)來定義你的beans和它們注入的依賴脓诡。為了簡(jiǎn)便,我們經(jīng)常使用@ComponentScan來發(fā)現(xiàn)你的beans媒役,結(jié)合@Autowired構(gòu)造函數(shù)注入也工作的很好祝谚。

If you structure your code as suggested above (locating your application class in a root package), you can add @ComponentScan without any arguments. All of your application components (@Component, @Service, @Repository, @Controller etc.) will be automatically registered as Spring Beans.

如果你根據(jù)上面的建議組織你代碼(將你的應(yīng)用類放在根包中),你可以添加@ComponentScan注解而不需要任何參數(shù)酣衷。你所有的應(yīng)用組件(@Component交惯,@Service@Repository穿仪,@Controller等等)將會(huì)作為Spring bean進(jìn)行自動(dòng)注冊(cè)席爽。

Here is an example @Service Bean that uses constructor injection to obtain a required RiskAssessor bean.

下面是一個(gè)@Service Bean的例子,通過使用構(gòu)造函數(shù)注入來獲得RiskAssessor bean啊片。

package com.example.service;

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

@Service
public class DatabaseAccountService implements AccountService {

    private final RiskAssessor riskAssessor;

    @Autowired
    public DatabaseAccountService(RiskAssessor riskAssessor) {
        this.riskAssessor = riskAssessor;
    }

    // ...

}

Notice how using constructor injection allows the riskAssessor field to be marked as final, indicating that it cannot be subsequently changed.

?

注意使用構(gòu)造函數(shù)注入允許riskAssessor字段標(biāo)記為final只锻,意味著它接下來不能被修改。

18. Using the @SpringBootApplication annotation

Many Spring Boot developers always have their main class annotated with @Configuration, @EnableAutoConfiguration and @ComponentScan. Since these annotations are so frequently used together (especially if you follow the best practices above), Spring Boot provides a convenient @SpringBootApplication alternative.

許多Spring Boot的開發(fā)者總是在它們的主類上加上@Configuration紫谷,@EnableAutoConfiguration@ComponentScan注解齐饮。由于這些注解頻繁的在一起使用(尤其是你遵循上面的最佳實(shí)踐時(shí)),Spring Boot提供了一個(gè)方便的@SpringBootApplication注解來代替它們笤昨。

The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan with their default attributes:

@SpringBootApplication注解等價(jià)于使用@Configuration祖驱,@EnableAutoConfiguration@ComponentScan以及它們的默認(rèn)特性:

package com.example.myproject;

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

@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
public class Application {

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

}

@SpringBootApplication also provides aliases to customize the attributes of @EnableAutoConfiguration and @ComponentScan.

?

@SpringBootApplication也提供了別名來定制@EnableAutoConfiguration@ComponentScan的特性。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末瞒窒,一起剝皮案震驚了整個(gè)濱河市捺僻,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌崇裁,老刑警劉巖陵像,帶你破解...
    沈念sama閱讀 211,743評(píng)論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異寇壳,居然都是意外死亡醒颖,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,296評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門壳炎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來泞歉,“玉大人,你說我怎么就攤上這事匿辩⊙遥” “怎么了?”我有些...
    開封第一講書人閱讀 157,285評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵铲球,是天一觀的道長(zhǎng)挺庞。 經(jīng)常有香客問我,道長(zhǎng)稼病,這世上最難降的妖魔是什么选侨? 我笑而不...
    開封第一講書人閱讀 56,485評(píng)論 1 283
  • 正文 為了忘掉前任掖鱼,我火速辦了婚禮,結(jié)果婚禮上援制,老公的妹妹穿的比我還像新娘戏挡。我一直安慰自己,他們只是感情好晨仑,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,581評(píng)論 6 386
  • 文/花漫 我一把揭開白布褐墅。 她就那樣靜靜地躺著,像睡著了一般洪己。 火紅的嫁衣襯著肌膚如雪妥凳。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,821評(píng)論 1 290
  • 那天答捕,我揣著相機(jī)與錄音逝钥,去河邊找鬼。 笑死噪珊,一個(gè)胖子當(dāng)著我的面吹牛晌缘,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播痢站,決...
    沈念sama閱讀 38,960評(píng)論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼磷箕,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了阵难?” 一聲冷哼從身側(cè)響起岳枷,我...
    開封第一講書人閱讀 37,719評(píng)論 0 266
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎呜叫,沒想到半個(gè)月后空繁,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,186評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡朱庆,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,516評(píng)論 2 327
  • 正文 我和宋清朗相戀三年盛泡,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片娱颊。...
    茶點(diǎn)故事閱讀 38,650評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡傲诵,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出箱硕,到底是詐尸還是另有隱情拴竹,我是刑警寧澤,帶...
    沈念sama閱讀 34,329評(píng)論 4 330
  • 正文 年R本政府宣布剧罩,位于F島的核電站栓拜,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜幕与,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,936評(píng)論 3 313
  • 文/蒙蒙 一挑势、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧纽门,春花似錦薛耻、人聲如沸营罢。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,757評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽饲漾。三九已至蝙搔,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間考传,已是汗流浹背吃型。 一陣腳步聲響...
    開封第一講書人閱讀 31,991評(píng)論 1 266
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留僚楞,地道東北人勤晚。 一個(gè)月前我還...
    沈念sama閱讀 46,370評(píng)論 2 360
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像泉褐,于是被迫代替她去往敵國(guó)和親赐写。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,527評(píng)論 2 349

推薦閱讀更多精彩內(nèi)容