配置文件
在永仁等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站建設(shè)、成都做網(wǎng)站 網(wǎng)站設(shè)計制作按需網(wǎng)站策劃,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站建設(shè),全網(wǎng)整合營銷推廣,成都外貿(mào)網(wǎng)站制作,永仁網(wǎng)站建設(shè)費用合理。
前面的例子我們都是使用XML的bean定義來配置組件。在一個稍大的項目中,通常會有上百個組件,如果這些組件采用XML的bean定義來配置,顯然會增加配置文件的體積,查找及維護(hù)起來也不太方便。
Spring2.5為我們引入了組件自動掃描機(jī)制,它可以在類路徑底下尋找標(biāo)注了@Component、@Service、@Controller、@Repository注解的類,并把這些類納入進(jìn)Spring容器中管理。
它的作用和在XML文件中使用bean節(jié)點配置組件是一樣的。要使用自動掃描機(jī)制,我們需要打開以下配置信息:
<?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" />
這個配置隱式注冊了多個對注解進(jìn)行解析處理的處理器,包括<context:annotation-config/>該配置注冊的處理器,也就是說寫了<context:component-scan base-package="cn.itcast" />
配置,就不用寫<context:annotation-config/>
配置了,此外base-package為需要掃描的包(含子包)。
注解
@Service用于標(biāo)注業(yè)務(wù)層組件、 @Controller用于標(biāo)注控制層組件(如Struts2中的action)、@Repository用于標(biāo)注數(shù)據(jù)訪問組件,即DAO組件。而@Component泛指組件,當(dāng)組件不好歸類的時候,我們可以使用這個注解進(jìn)行標(biāo)注。
本文是建立在@Autowire注解與自動裝配的案例基礎(chǔ)上的。
我們首先將Spring的配置文件改為:
<?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注解標(biāo)注PersonServiceBean類,如下:
@Service public class PersonServiceBean implements PersonService { private PersonDao personDao; public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } @Override public void save() { personDao.add(); } }
使用@Repository注解標(biāo)注PersonDaoBean類,如下:
@Repository public class PersonDaoBean implements PersonDao { @Override public void add() { System.out.println("執(zhí)行PersonDaoBean中的add()方法"); } }
最后,我們修改SpringTest類的代碼為:
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類的代碼修改為:
@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類的代碼應(yīng)改為:
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控制臺打印:
我們前面學(xué)過Spring管理的bean的作用域,我們就能知道以上Spring管理的兩個bean的作用域默認(rèn)是singleton。當(dāng)然了,我們也可以更改Spring管理的bean的作用域,如將PersonServiceBean類的代碼改為:
@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類的代碼修改為:
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控制臺打?。?/p>
prototype作用域本來就意味著每次從Spring容器獲取bean都是新的對象嘛。
若是通過在classpath路徑下自動掃描方這種式把組件納入Spring容器中管理,如何指定bean的初始化方法和銷毀方法呢?這時我們就需要用到兩個注解:@PostConstruct和@PreDestroy。為了試驗,我們將PersonServiceBean類的代碼修改為:
@Service("personService") public class PersonServiceBean implements PersonService { private PersonDao personDao; @PostConstruct public void init() { System.out.println("初始化資源"); } @PreDestroy public void destroy() { System.out.println("銷毀、關(guān)閉資源"); } public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } @Override public void save() { personDao.add(); } }
接下來還要將SpringTest類的代碼修改為:
public class SpringTest { @Test public void instanceSpring() { AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); PersonService personService = (PersonService) ctx.getBean("personService"); ctx.close(); } }
這樣,測試instanceSpring()方法,Eclipse控制臺會打?。?/p>
如要查看源碼,可點擊讓Spring自動掃描和管理Bean進(jìn)行下載。
總結(jié)
以上所述是小編給大家介紹的spring自動掃描包,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網(wǎng)站的支持!
分享標(biāo)題:詳解spring自動掃描包
文章網(wǎng)址:http://chinadenli.net/article28/jhhjcp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、網(wǎng)站排名、網(wǎng)站維護(hù)、品牌網(wǎng)站設(shè)計、軟件開發(fā)、網(wǎng)頁設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)