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

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

服務器之家 - 編程語言 - Java教程 - 詳解spring自動掃描包

詳解spring自動掃描包

2021-05-08 12:30qq_36098284 Java教程

這篇文章主要介紹了spring自動掃描包的相關知識,本文通過實例相結合的形式給大家介紹的非常詳細,感興趣的朋友跟隨腳本之家小編一起看看吧

配置文件

前面的例子我們都是使用xml的bean定義來配置組件。在一個稍大的項目中,通常會有上百個組件,如果這些組件采用xml的bean定義來配置,顯然會增加配置文件的體積,查找及維護起來也不太方便。

spring2.5為我們引入了組件自動掃描機制,它可以在類路徑底下尋找標注了@component、@service、@controller、@repository注解的類,并把這些類納入進spring容器中管理。

它的作用和在xml文件中使用bean節點配置組件是一樣的。要使用自動掃描機制,我們需要打開以下配置信息:

?
1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemalocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-4.2.xsd">
 <context:component-scan base-package="cn.itcast" />
</beans>

其中<context:component-scan base-package="cn.itcast" />這個配置隱式注冊了多個對注解進行解析處理的處理器,包括<context:annotation-config/>該配置注冊的處理器,也就是說寫了<context:component-scan base-package="cn.itcast" />配置,就不用寫<context:annotation-config/>配置了,此外base-package為需要掃描的包(含子包)。

注解

@service用于標注業務層組件、 @controller用于標注控制層組件(如struts2中的action)、@repository用于標注數據訪問組件,即dao組件。而@component泛指組件,當組件不好歸類的時候,我們可以使用這個注解進行標注。
本文是建立在@autowire注解與自動裝配的案例基礎上的。

我們首先將spring的配置文件改為:

?
1
2
3
4
5
6
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
 xmlns:context="http://www.springframework.org/schema/context" xsi:schemalocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans.xsd  http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-4.2.xsd">
 <context:component-scan base-package="cn.itcast" />
</beans>

一個實例

然后使用@service注解標注personservicebean類,如下:

?
1
2
3
4
5
6
7
8
9
10
11
@service
public class personservicebean implements personservice {
 private persondao persondao;
 public void setpersondao(persondao persondao) {
  this.persondao = persondao;
 }
 @override
 public void save() {
  persondao.add();
 }
}

使用@repository注解標注persondaobean類,如下:

?
1
2
3
4
5
6
7
@repository
public class persondaobean implements persondao {
 @override
 public void add() {
  system.out.println("執行persondaobean中的add()方法");
 }
}

最后,我們修改springtest類的代碼為:

?
1
2
3
4
5
6
7
8
9
10
11
public class springtest {
 @test
 public void instancespring() {
  abstractapplicationcontext ctx = new classpathxmlapplicationcontext("beans.xml");
  personservice personservice = (personservice) ctx.getbean("personservicebean");
  persondao persondao = (persondao) ctx.getbean("persondaobean");
  system.out.println(personservice);
  system.out.println(persondao);
  ctx.close();
 }
}

測試instancespring()方法,可看到eclipse控制臺打印:

詳解spring自動掃描包

如果我們想使用按指定名稱獲取,可將personservicebean類的代碼修改為:

?
1
2
3
4
5
6
7
8
9
10
11
@service("personservice")
public class personservicebean implements personservice {
 private persondao persondao;
 public void setpersondao(persondao persondao) {
  this.persondao = persondao;
 }
 @override
 public void save() {
  persondao.add();
 }
}

這樣,springtest類的代碼應改為:

?
1
2
3
4
5
6
7
8
9
public class springtest {
 @test
 public void instancespring() {
  abstractapplicationcontext ctx = new classpathxmlapplicationcontext("beans.xml");
  personservice personservice = (personservice) ctx.getbean("personservice");
  system.out.println(personservice);
  ctx.close();
 }
}

測試instancespring()方法,可看到eclipse控制臺打印:

詳解spring自動掃描包

我們前面學過spring管理的bean的作用域,我們就能知道以上spring管理的兩個bean的作用域默認是singleton。當然了,我們也可以更改spring管理的bean的作用域,如將personservicebean類的代碼改為:

?
1
2
3
4
5
6
7
8
9
10
11
@service("personservice") @scope("prototype")
public class personservicebean implements personservice {
 private persondao persondao;
 public void setpersondao(persondao persondao) {
  this.persondao = persondao;
 }
 @override
 public void save() {
  persondao.add();
 }
}

意味著spring管理的personservicebean這個bean的作用域變成prototype了,這時我們將springtest類的代碼修改為:

?
1
2
3
4
5
6
7
8
9
10
public class springtest {
 @test
 public void instancespring() {
  abstractapplicationcontext ctx = new classpathxmlapplicationcontext("beans.xml");
  personservice personservice1 = (personservice) ctx.getbean("personservice");
  personservice personservice2 = (personservice) ctx.getbean("personservice");
  system.out.println(personservice1 == personservice2);
  ctx.close();
 }
}

測試instancespring()方法,可看到eclipse控制臺打印:

詳解spring自動掃描包

prototype作用域本來就意味著每次從spring容器獲取bean都是新的對象嘛。

若是通過在classpath路徑下自動掃描方這種式把組件納入spring容器中管理,如何指定bean的初始化方法和銷毀方法呢?這時我們就需要用到兩個注解:@postconstruct和@predestroy。為了試驗,我們將personservicebean類的代碼修改為:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@service("personservice")
public class personservicebean implements personservice {
 private persondao persondao;
 @postconstruct
 public void init() {
  system.out.println("初始化資源");
 }
 @predestroy
 public void destroy() {
  system.out.println("銷毀、關閉資源");
 }
 public void setpersondao(persondao persondao) {
  this.persondao = persondao;
 }
 @override
 public void save() {
  persondao.add();
 }
}

接下來還要將springtest類的代碼修改為:

?
1
2
3
4
5
6
7
8
public class springtest {
 @test
 public void instancespring() {
  abstractapplicationcontext ctx = new classpathxmlapplicationcontext("beans.xml");
  personservice personservice = (personservice) ctx.getbean("personservice");
  ctx.close();
 }
}

這樣,測試instancespring()方法,eclipse控制臺會打印:

詳解spring自動掃描包

如要查看源碼,可點擊讓spring自動掃描和管理bean進行下載

總結

以上所述是小編給大家介紹的spring自動掃描包,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

原文鏈接:https://blog.csdn.net/qq_36098284/article/details/80663860

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品欧美亚洲韩国日本 | 2021久久 | 日韩在线免费 | 国产特黄a级在线视频 | 久久两性视频 | chinese男男gay| 欧美最猛性xxxxx69交 | fuqer日本老师 | 我们中文在线观看免费完整版 | 国产成人综合亚洲亚洲欧美 | 波多野结衣在线中文字幕 | 男人天堂资源 | 99久女女精品视频在线观看 | 色哟哟在线观看 | 精品在线免费播放 | 91精品国产99久久 | 亲爱的客栈第二季免费观看完整版 | 99在线播放 | 亚洲无线一二三四区 | 国产人妖xxxxx免费看 | 国产成人精品.一二区 | 四虎影视库永久在线地址 | 亚洲精品在线网址 | 麻豆视频免费在线观看 | 波多野结衣教师未删减版 | 青草国内精品视频在线观看 | 国产成人精品免费视频大全五级 | 日本高清中文字幕一区二区三区 | 国产色综合久久五月色婷婷中文 | 色综合网天天综合色中文男男 | 亚洲欧美精品久久 | 亚洲 欧美 偷自乱 图片 | 亚洲成人一区在线 | 亚洲国产精品久久丫 | 小伙无套内射老女人 | 国产免费色视频 | 小小水蜜桃视频高清在线播放 | 亚洲羞羞裸色私人影院 | 国产精品久久99 | 乌克兰xxxxx| 亚洲国产成人久久综合一区 |