一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - 在Spring中基于Java類進(jìn)行配置的完整步驟

在Spring中基于Java類進(jìn)行配置的完整步驟

2021-04-29 14:50deniro Java教程

基于Java配置選項(xiàng),可以編寫大多數(shù)的Spring不用配置XML,下面這篇文章主要給大家介紹了關(guān)于在Spring中基于Java類進(jìn)行配置的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言

javaconfig 原來是 spring 的一個(gè)子項(xiàng)目,它通過 java 類的方式提供 bean 的定義信息,在 spring4 的版本, javaconfig 已正式成為 spring4 的核心功能 。

本文將詳細(xì)介紹關(guān)于spring中基于java類進(jìn)行配置的相關(guān)內(nèi)容,下面話不多說了,來一起看看詳細(xì)的介紹吧

1 定義 bean

普通的 pojo 只要標(biāo)注了 @configuration 注解,就可以為 spring 容器提供 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
26
27
28
29
30
@configuration
public class systemconfig {
 /**
  * 定義 bean,并實(shí)例化
  *
  * @return
  */
 @bean
 public userdao userdao() {
  return new userdao();
 }
 
 @bean
 public deptdao deptdao() {
  return new deptdao();
 }
 
 /**
  * 定義 userservice,并把之前定義的 userdao 與 deptdao 注入進(jìn)來
  *
  * @return
  */
 @bean
 public userservice userservice() {
  userservice service = new userservice();
  service.setuserdao(userdao());
  service.setdeptdao(deptdao());
  return service;
 }
}

這個(gè)類的方法標(biāo)注了 @bean 注解,即為定義 bean, bean 的類型由方法返回值的類型決定,名稱默認(rèn)和方法名同名,也可以通過入?yún)@示指定 bean 名稱,比如 @bean(name=”xxx”)。 @bean 所標(biāo)注的方法體提供了 實(shí)例化 bean 的邏輯 。

以上配置和下面的 xml 是等效的:

?
1
2
3
4
<bean id="userdao" class="net.deniro.spring4.conf.userdao"/>
<bean id="deptdao" class="net.deniro.spring4.conf.deptdao"/>
<bean id="userservice" class="net.deniro.spring4.conf.userservice"
p:userdao-ref="userdao" p:deptdao-ref="deptdao"/>

基于 java 類的配置方式和基于 xml 或者基于注解的配置方式相比——

  • java 類的配置方式通過代碼編程的方式,可以更加靈活地實(shí)例化 bean 和裝配 bean 之間的關(guān)系。
  • xml 或者基于注解的方式都是通過聲明來定義配置的,所以靈活性上要遜一些,但在配置上更簡單 。

因?yàn)?@configuration 注解類本身已經(jīng)標(biāo)注了 @component,所以這些類可以像那些普通的 bean 一樣被注入到其他的 bean 中。

?
1
2
3
4
5
6
7
8
9
10
11
12
@configuration
public class applicationconfig {
 @autowired
 private systemconfig systemconfig;
 @bean
 public authorityservice authorityservice() {
  authorityservice service = new authorityservice();
  service.setuserdao(systemconfig.userdao());
  service.setdeptdao(systemconfig.deptdao());
  return service;
 }
}

spring 會對配置類中所有標(biāo)注了 @bean 的方法使用 aop 增強(qiáng),引入 bean 的生命周期管理邏輯。比如上面的 systemconfig.userdao(),它返回的是對應(yīng) bean 的單例。

在 @bean 中,我們還可以通過標(biāo)注 @scope 注解來控制 bean 的作用范圍:

?
1
2
3
4
5
@scope("prototype")
@bean
public deptdao deptdao() {
 return new deptdao();
}

這樣每次調(diào)用 deptdao() 方法都會返回一個(gè)新的實(shí)例:

?
1
2
assertnotsame(authorityservice.getdeptdao().hashcode(),authorityservice
    .getdeptdao().hashcode());

注意: 使用基于 java 類進(jìn)行配置,類路徑下必須有 spring aop 與 cglib 庫。

2 啟動(dòng) spring 容器

2.1 只使用 @configuration 類

可以使用 annotationconfigapplicationcontext 類的構(gòu)造函數(shù)傳入標(biāo)注了 @configuration 的 java 類來啟動(dòng) spring 容器 。

?
1
2
3
4
applicationcontext context=new annotationconfigapplicationcontext(systemconfig
  .class);
userservice userservice= (userservice) context.getbean("userservice");
assertnotnull(userservice);

如果存在多個(gè) @configuration 配置類,那么可以 annotationconfigapplicationcontext 中注冊它們,然后再通過刷新容器應(yīng)用這些配置類:

?
1
2
3
4
5
6
7
8
9
10
11
annotationconfigapplicationcontext context=new annotationconfigapplicationcontext();
 
//注冊多個(gè)配置類
context.register(systemconfig.class);
context.register(applicationconfig.class);
 
//刷新容器(應(yīng)用這些配置類)
context.refresh();
 
applicationconfig config=context.getbean(applicationconfig.class);
assertnotnull(config);

也可以通過 @import 將多個(gè)配置類組裝到一個(gè)配置類中,然后僅需注冊這個(gè)組裝好的配置類 ,即可啟動(dòng)容器:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
@configuration
@import(systemconfig.class)
public class applicationconfig2 {
 @autowired
 private systemconfig systemconfig;
 @bean
 public authorityservice authorityservice() {
  authorityservice service = new authorityservice();
  service.setuserdao(systemconfig.userdao());
  service.setdeptdao(systemconfig.deptdao());
  return service;
 }
}

單元測試:

?
1
2
3
4
5
6
7
8
9
annotationconfigapplicationcontext context=new annotationconfigapplicationcontext(applicationconfig2.class);
 
applicationconfig2 config=context.getbean(applicationconfig2.class);
assertnotnull(config);
final authorityservice authorityservice = config.authorityservice();
assertnotnull(authorityservice.getdeptdao());
 
assertnotsame(authorityservice.getdeptdao().hashcode(),authorityservice
  .getdeptdao().hashcode());

2.2 使用 xml 文件引用 @configuration 類的配置

標(biāo)注了 @configuration 的配置類也是一個(gè) bean,所以它也可以被 spring 的 <context:component-scan> 掃描到 。 因此如果希望將配置類組裝到 xml 的配置文件中,并通過 xml 的配置文件啟動(dòng) spring,那么僅需要在 xml 中通過 <context:component-scan> 掃描到相應(yīng)的配置類即可 。

?
1
2
3
<context:component-scan base-package="net.deniro.spring4.conf"
  resource-pattern="applicationconfig2.class"
  />

2.3 在 @configuration 類中引用 xml 文件的配置

在 @configuration 配置類中可以直接通過 @importresource 引入 xml 的配置文件,這樣就可以直接通過 @autowired 引用 xml 配置文件中定義的 bean。

配置文件:

?
1
2
<bean id="groupdao" class="net.deniro.spring4.conf.groupdao"/>
<bean id="roledao" class="net.deniro.spring4.conf.roledao"/>

@configuration 類:

?
1
2
3
4
5
6
7
8
9
10
11
12
@importresource("classpath:beans5-11.xml")
@configuration
public class serviceconfig {
 @bean
 @autowired
 public relationservice relationservice(groupdao groupdao,roledao roledao){
  relationservice service=new relationservice();
  service.setgroupdao(groupdao);
  service.setroledao(roledao);
  return service;
 }
}

單元測試:

?
1
2
3
4
5
6
7
8
9
10
annotationconfigapplicationcontext context=new annotationconfigapplicationcontext
  (serviceconfig.class);
serviceconfig config=context.getbean(serviceconfig.class);
assertnotnull(config);
relationservice service=config.relationservice((groupdao) context.getbean
    ("groupdao"),
  (roledao) context
  .getbean
    ("roledao"));
assertnotnull(service.getroledao());

只要這些不同形式 bean 的定義信息能夠加載到 spring 容器中,那么 spring 就可以智能的完成 bean 之間的裝配 。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對服務(wù)器之家的支持。

原文鏈接:https://www.jianshu.com/p/67fa18a9c8b3

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成人久久伊人精品伊人 | 欧美男女爱爱视频 | 亚洲 欧美 中文 日韩欧美 | 色网在线视频 | 福利片福利一区二区三区 | 欧美性黑人巨大gaysex | 亚洲七七久久综合桃花 | 美女沟厕撒尿全过程高清图片 | 日韩在线天堂免费观看 | 日本爽p大片免费观看 | 单亲乱l仑在线观看免费观看 | 91欧洲在线视精品在亚洲 | 亚洲冬月枫中文字幕在线看 | 五月桃花网婷婷亚洲综合 | 亚洲国产在线视频中文字 | 校花被吃奶还摸下面 | 亚洲成人影院在线 | 日韩国产欧美视频 | 国产精品日韩在线观看 | 国产一级片视频 | 亚洲高清在线天堂精品 | 欧美国产日本精品一区二区三区 | 亚洲欧美日韩成人 | 午夜影院网站 | 国产综合久久久久 | 亚洲AV 中文字幕 国产 欧美 | 日本免费在线观看视频 | 2019男人天堂| 91亚洲视频在线观看 | 97色伦图片7778久久 | 草草影院永久在线观看 | 国模大胆一区二区三区 | 亚洲免费国产 | 亚洲国产综合久久久无码色伦 | 免费一级特黄特色大片在线 | 99久久精品在免费线18 | 日韩欧美亚洲一区二区综合 | 亚洲网站在线观看 | 欧美1| 女教师雪白老汉 | 毛片视频在线免费观看 |