自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Spring 注解@Bean使用方式,你都知道嗎?

開發(fā) 架構(gòu)
要聲明Bean,可以使用@Bean注釋對(duì)方法進(jìn)行注釋??梢允褂么朔椒ㄔ贏pplicationContext中注冊(cè)Bean定義,其類型指定為該方法的返回值。默認(rèn)情況下,Bean名與方法名相同。

@Bean是方法級(jí)注釋,是XML <bean/>元素的直接類比。注解支持<bean/>提供的一些屬性,例如:

  • init-method
  • destroy-method
  • autowiring
  • name.

你可以在@Configuration或@Component類中使用@Bean注釋。

聲明Bean

要聲明bean,可以使用@Bean注釋對(duì)方法進(jìn)行注釋??梢允褂么朔椒ㄔ贏pplicationContext中注冊(cè)bean定義,其類型指定為該方法的返回值。默認(rèn)情況下,bean名與方法名相同。下面的例子展示了一個(gè)@Bean方法聲明:

@Configuration
public class AppConfig {

@Bean
public TransferServiceImpl transferService() {
return new TransferServiceImpl();
}
}

前面的配置與下面的Spring XML完全等效:

<beans>
<bean id="transferService" class="com.acme.TransferServiceImpl"/>
</beans>

這兩個(gè)聲明都使一個(gè)名為transferService的bean在ApplicationContext中可用,綁定到類型為TransferServiceImpl的對(duì)象實(shí)例,如下圖所示:

transferService -> com.acme.TransferServiceImpl

你也可以使用默認(rèn)方法來定義bean。這允許通過在默認(rèn)方法上實(shí)現(xiàn)帶有bean定義的接口來組合bean配置。

public interface BaseConfig {

@Bean
default TransferServiceImpl transferService() {
return new TransferServiceImpl();
}
}

@Configuration
public class AppConfig implements BaseConfig {
}

你還可以用接口(或基類)返回類型聲明@Bean方法,如下例所示:

@Configuration
public class AppConfig {

@Bean
public TransferService transferService() {
return new TransferServiceImpl();
}
}

然而,這將高級(jí)類型預(yù)測(cè)的可見性限制為指定的接口類型(TransferService)。然后,只有在實(shí)例化了受影響的單例bean之后,容器才知道完整類型(TransferServiceImpl)。非惰性單例bean根據(jù)其聲明順序進(jìn)行實(shí)例化,因此你可能會(huì)看到不同的類型匹配結(jié)果,這取決于另一個(gè)組件何時(shí)嘗試通過非聲明類型進(jìn)行匹配(例如@Autowired TransferServiceImpl,它只在transferServicebean實(shí)例化后才解析)。

Bean依賴

一個(gè)@Bean注釋的方法可以有任意數(shù)量的參數(shù)來描述構(gòu)建該Bean所需的依賴關(guān)系。例如,如果我們的TransferService需要一個(gè)AccountRepository,我們可以用一個(gè)方法參數(shù)來實(shí)現(xiàn)該依賴關(guān)系,如下例所示:

@Configuration
public class AppConfig {

@Bean
public TransferService transferService(AccountRepository accountRepository) {
return new TransferServiceImpl(accountRepository);
}
}

解析機(jī)制與基于構(gòu)造函數(shù)的依賴注入非常相似。

Bean生命周期

使用@Bean注釋定義的任何類都支持常規(guī)的生命周期回調(diào),并且可以使用JSR-250中的@PostConstruct@PreDestroy注釋。

也完全支持常規(guī)的Spring生命周期回調(diào)。如果bean實(shí)現(xiàn)InitializingBean、DisposableBean或Lifecycle,那么容器將調(diào)用它們各自的方法。

還完全支持一組標(biāo)準(zhǔn)的*Aware接口(如BeanFactoryAware、BeanNameAware、MessageSourceAware、ApplicationContextAware等)。

@Bean注釋支持指定任意的初始化和銷毀回調(diào)方法,就像Spring XML在Bean元素上的init方法和destroy方法屬性一樣,如下例所示:

public class BeanOne {

public void init() {
// initialization logic
}
}

public class BeanTwo {

public void cleanup() {
// destruction logic
}
}

@Configuration
public class AppConfig {

@Bean(initMethod = "init")
public BeanOne beanOne() {
return new BeanOne();
}

@Bean(destroyMethod = "cleanup")
public BeanTwo beanTwo() {
return new BeanTwo();
}
}

默認(rèn)情況下,使用Java配置定義的具有公共close或shutdown方法的bean會(huì)自動(dòng)使用銷毀回調(diào)進(jìn)行登記。如果你有一個(gè)公共的close或shutdown方法,并且不希望在容器關(guān)閉時(shí)調(diào)用它,那么可以將@Bean(destroyMethod="")添加到Bean定義中,以禁用默認(rèn)(推斷)模式。

public class Main {

static class Person {
public void close() {
System.out.println("close") ;
}
public void shutdown() {
System.out.println("shutdown") ;
}
}
@Configuration
static class AppConfig {
@Bean
// @Bean(destroyMethod = "") 這樣就禁用了關(guān)閉的操作(closeshutdown
public Person person() {
return new Person() ;
}
}

public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class) ;
context.close() ;
}

}

注意:如果一個(gè)類中既有close方法又有shutdown方法,那么只有close方法生效。

以前面示例中的BeanOne為例,在構(gòu)造過程中直接調(diào)用init()方法同樣有效,如下面的例子所示:

@Configuration
public class AppConfig {

@Bean
public BeanOne beanOne() {
BeanOne beanOne = new BeanOne();
beanOne.init();
return beanOne;
}
}

當(dāng)你直接在Java中工作時(shí),你可以對(duì)對(duì)象執(zhí)行任何你喜歡的操作,而不必總是依賴于容器生命周期。

Bean作用域

Spring包含@Scope注釋,以便您可以指定bean的范圍。

你可以指定使用@Bean注釋定義的Bean應(yīng)該具有特定的作用域??梢允褂肂ean scopes部分中指定的任何標(biāo)準(zhǔn)作用域。

默認(rèn)作用域是singleton,但您可以使用@Scope注釋來覆蓋它,如下例所示:

@Configuration
public class MyConfiguration {

@Bean
@Scope("prototype")
public Encryptor encryptor() {
// ...
}
}

@Scope and scoped-proxy

Spring通過作用域代理提供了一種方便的方式來處理作用域依賴。在使用XML配置時(shí),創(chuàng)建這樣一個(gè)代理的最簡(jiǎn)單方法是<aop:scope -proxy/>元素。在Java中配置bean時(shí),使用@Scope注解可以為proxyMode屬性提供類似的支持。默認(rèn)值是ScopedProxyMode.DEFAULT,它通常表示不應(yīng)該創(chuàng)建scoped proxy,除非在組件掃描指令級(jí)別配置了不同的DEFAULT。你可以指定
ScopedProxyMode.TARGET_CLASS ScopedProxyMode。接口或ScopedProxyMode.NO。

如果你使用Java將XML參考文檔(參見“限定范圍的代理”)中的限定范圍代理示例移植到我們的@Bean,它類似于以下內(nèi)容:

// an HTTP Session-scoped bean exposed as a proxy
@Bean
@SessionScope
public UserPreferences userPreferences() {
return new UserPreferences();
}

@Bean
public Service userService() {
UserService service = new SimpleUserService();
// a reference to the proxied userPreferences bean
service.setUserPreferences(userPreferences());
return service;
}

自定義Bean命名

默認(rèn)情況下,配置類使用@Bean方法的名稱作為生成的Bean的名稱。但是,可以使用name屬性覆蓋此功能,如以下示例所示:

@Configuration
public class AppConfig {

@Bean("myThing")
public Thing thing() {
return new Thing();
}
}

Bean別名

正如Naming Beans中所討論的,有時(shí)需要為單個(gè)bean提供多個(gè)名稱,也稱為bean別名。@Bean注釋的name屬性接受用于此目的的String數(shù)組。以下示例顯示了如何為bean設(shè)置多個(gè)別名:

@Configuration
public class AppConfig {

@Bean({"dataSource", "subsystemA-dataSource", "subsystemB-dataSource"})
public DataSource dataSource() {
// instantiate, configure and return DataSource bean...
}
}

Bean描述

有時(shí),提供一個(gè)bean的更詳細(xì)的文本描述是有幫助的。當(dāng)bean被公開(可能是通過JMX)用于監(jiān)視目的時(shí),這可能特別有用。

要向@Bean添加描述,可以使用@Description注釋,如下例所示:

@Configuration
public class AppConfig {

@Bean
@Description("Provides a basic example of a bean")
public Thing thing() {
return new Thing();
}
}


責(zé)任編輯:姜華 來源: 今日頭條
相關(guān)推薦

2023-04-28 12:37:59

Spring@Bean使用方式

2023-08-29 09:31:01

Scrapy網(wǎng)頁(yè)爬蟲

2019-02-12 11:15:15

Spring設(shè)計(jì)模式Java

2020-02-20 08:30:49

OSPF網(wǎng)絡(luò)協(xié)議路由協(xié)議

2024-04-28 08:20:52

Controller接口URL

2023-02-15 08:12:19

http超時(shí)過濾器

2021-07-29 06:55:03

Spring@AutowriedbyType注入

2023-08-30 07:39:16

PawSQL數(shù)據(jù)庫(kù)

2020-09-11 06:39:29

ThreadLocal線程

2020-09-28 11:14:57

線程數(shù)據(jù)語(yǔ)言

2024-01-18 07:46:53

HookReact回調(diào)函數(shù)

2021-11-17 11:03:14

Python代碼語(yǔ)法

2021-08-05 18:21:29

Autowired代碼spring

2016-01-11 09:48:07

2024-02-05 12:08:07

線程方式管理

2023-02-13 08:10:40

Gateway網(wǎng)關(guān)Spring

2016-03-18 19:03:35

認(rèn)知計(jì)算IBM

2018-07-04 11:02:23

無線傳輸模式

2023-02-01 08:31:36

JavaScript循環(huán)遍歷

2022-11-10 09:00:41

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)