轉自:https://www.hangge.com/blog/cache/detail_2506.html
一、基本用法
1,基本介紹
Spring Boot 推薦使用 java 配置完全代替 XML 配置,java 配置是通過 @Configration 和 @Bean 注解實現的。二者作用如下:
- @Configration 注解:聲明當前類是一個配置類,相當於 Spring 中的一個 XML 文件
- @Bean 注解:作用在方法上,聲明當前方法的返回值是一個 Bean
2,簡單樣例
(1)首先創建一個自定義的配置類 MyConfigration:
- 使用 @Configration 注解將該類聲明為一個配置類。
- 在 hello() 方法上添加 @Bean 注解則會往 Spring 容器中添加一個名為 hello 的 Bean,該 Bean 即為方法的返回值。
1
2
3
4
5
6
7
8
9
10
11
12
|
package
com.example.demo.component;
import
org.springframework.context.annotation.Bean;
import
org.springframework.context.annotation.Configuration;
@Configuration
public
class
MyConfigration {
@Bean
public
String hello() {
return
"welcome to hangge.com"
;
}
}
|
(2)下面我們在一個 Controller 中獲取並使用這個 Bean,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package
com.example.demo.controller;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.web.bind.annotation.GetMapping;
import
org.springframework.web.bind.annotation.RestController;
@RestController
public
class
HelloController {
@Autowired
String hello;
@GetMapping
(
"/test"
)
public
String test() {
return
hello;
}
}
|
二、@Bean 注解詳解
1,使用說明
- @Bean 注解作用在方法上
- @Bean 指示一個方法返回一個 Spring 容器管理的 Bean
- @Bean 方法名與返回類名一致,首字母小寫
- @Bean 一般和 @Component 或者 @Configuration 一起使用
- @Bean 注解默認作用域為單例 singleton 作用域,可通過 @Scope(“prototype”) 設置為原型作用域
2,Bean 名稱
(1)默認情況下 Bean 名稱就是方法名,比如下面 Bean 名稱便是 myBean:
1
2
3
4
|
@Bean
public
MyBean myBean() {
return
new
MyBean();
}
|
(2)@Bean 注解支持設置別名。比如下面除了主名稱 myBean 外,還有個別名 myBean1(兩個都可以使用)
1
2
3
4
|
@Bean
(
"myBean1"
)
public
MyBean myBean() {
return
new
MyBean();
}
|
(3)@Bean 注解可以接受一個 String 數組設置多個別名。比如下面除了主名稱 myBean 外,還有別名 myBean1、myBean2(三個都可以使用)
1
2
3
4
|
@Bean
({
"myBean1"
,
"myBean2"
})
public
MyBean myBean() {
return
new
MyBean();
}
|
3,@Bean 與其他注解一起使用
(1)@Bean 注解常常與 @Scope、@Lazy,@DependsOn 和 @link Primary 注解一起使用:
(2)然后在 Configuration 中進行聲明:
- @Profile 注解:為在不同環境下使用不同的配置提供了支持,如開發環境和生產環境的數據庫配置是不同的
- @Scope 注解:將 Bean 的作用域從單例改變為指定的作用域
- @Lazy 注解:只有在默認單例作用域的情況下才有實際效果
- @DependsOn 注解:表示在當前 Bean 創建之前需要先創建特定的其他 Bean
(2)比如下面樣例,Bean 的作用域默認是單例的,我們配合 @Scope 注解將其改成 prototype 原型模式(每次獲取 Bean 的時候會有一個新的實例)
1
2
3
4
5
|
@Bean
()
@Scope
(
"prototype"
)
public
MyBean myBean() {
return
new
MyBean();
}
|
4,Bean 初始化和銷毀時調用相應的方法
(1)實際開發中,經常會遇到在 Bean 使用之前或使用之后做些必要的操作,Spring 對 Bean 的生命周期的操作提供了支持:我們可以通過 @Bean 注解的 initMethod 和 destrodMethod 進行指定 Bean 在初始化和銷毀時需要調用相應的方法。
(2)下面是一個簡單的樣例:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public
class
MyBean {
public
void
init() {
System.out.println(
"MyBean開始初始化..."
);
}
public
void
destroy() {
System.out.println(
"MyBean銷毀..."
);
}
public
String get() {
return
"MyBean使用..."
;
}
}
|
1
2
3
4
|
@Bean
(initMethod=
"init"
, destroyMethod=
"destroy"
)
public
MyBean myBean() {
return
new
MyBean();
}
|
三、@Configration 注解詳解
1,使用說明
- @Configration 注解作用在類、接口(包含注解)上
- @Configuration 用於定義配置類,可替換 xml 配置文件
- @Configration 注解類中可以聲明一個或多個 @Bean 方法
- @Configration 注解作用的類不能是 final 類型
- 嵌套的 @Configration 類必須是 static 的
2,聲明一個 @Bean 方法
(1)假設我們定義一個如下的 Bean:
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
|
package
com.example.demo.bean;
public
class
MyBean {
private
String port;
public
void
init() {
System.out.println(
"MyBean開始初始化..."
);
}
public
void
destroy() {
System.out.println(
"MyBean銷毀..."
);
}
public
String get() {
return
"端口號: "
+ getPort();
}
public
String getPort() {
return
port;
}
public
void
setPort(String port) {
this
.port = port;
}
}
|
(2)然后在 Configuration 中進行聲明:
1
2
3
4
5
6
7
8
9
|
@Configuration
public
class
MyConfigration {
@Bean
(initMethod=
"init"
, destroyMethod=
"destroy"
)
public
MyBean myBean() {
MyBean myBean =
new
MyBean();
myBean.setPort(
"8080"
);
return
myBean;
}
}
|
(3)最后進行測試,我們獲取這個 Bean 並輸出其內容:
1
2
3
4
5
6
7
8
9
10
|
@SpringBootApplication
public
class
DemoApplication {
public
static
void
main(String[] args) {
ConfigurableApplicationContext context
= SpringApplication.run(DemoApplication.
class
, args);
MyBean myBean = (MyBean) context.getBean(
"myBean"
);
System.out.println(myBean.get());
}
}
|
3,聲明多個 @Bean 方法
(1)@Configration 注解類中可以聲明多個 @Bean 方法,並且 bean 與 bean 之間是可以有依賴關系的。如果一個 bean 的定義依賴其他 bean,則直接調用對應的 JavaConfig 類中依賴 bean 的創建方法就可以了。
(2)下面是一個簡單的樣例,一共聲明了 country 和 userInfo 兩個 Bean。
(2)下面是一個簡單的樣例,一共聲明了 country 和 userInfo 兩個 Bean。
注意:@Configuration 注解的 bean 都已經變成了增強的類。因此上面的 country 這個 Bean 和下面直接調用 country() 方法返回的是同一個實例
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@Configuration
public
class
MyBeanConfig {
@Bean
public
Country country(){
return
new
Country();
}
@Bean
public
UserInfo userInfo(){
return
new
UserInfo(country());
}
}
|
原文出自:www.hangge.com 轉載請保留原文鏈接:https://www.hangge.com/blog/cache/detail_2506.html