配置文件
前面的例子我們都是使用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控制臺打印:
如果我們想使用按指定名稱獲取,可將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管理的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控制臺打印:
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自動掃描和管理bean進行下載。
總結
以上所述是小編給大家介紹的spring自動掃描包,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://blog.csdn.net/qq_36098284/article/details/80663860