最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解spring自動(dòng)掃描包

 更新時(shí)間:2018年06月12日 14:08:38   作者:qq_36098284  
這篇文章主要介紹了spring自動(dòng)掃描包的相關(guān)知識(shí),本文通過實(shí)例相結(jié)合的形式給大家介紹的非常詳細(xì),感興趣的朋友跟隨腳本之家小編一起看看吧

配置文件

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

Spring2.5為我們引入了組件自動(dòng)掃描機(jī)制,它可以在類路徑底下尋找標(biāo)注了@Component、@Service、@Controller、@Repository注解的類,并把這些類納入進(jìn)Spring容器中管理。

它的作用和在XML文件中使用bean節(jié)點(diǎn)配置組件是一樣的。要使用自動(dòng)掃描機(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" />這個(gè)配置隱式注冊了多個(gè)對(duì)注解進(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)組件不好歸類的時(shí)候,我們可以使用這個(gè)注解進(jìn)行標(biāo)注。
本文是建立在@Autowire注解與自動(dòng)裝配的案例基礎(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>

一個(gè)實(shí)例

然后使用@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控制臺(tái)打?。?/p>

這里寫圖片描述

如果我們想使用按指定名稱獲取,可將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控制臺(tái)打?。?/p>

這里寫圖片描述

我們前面學(xué)過Spring管理的bean的作用域,我們就能知道以上Spring管理的兩個(gè)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這個(gè)bean的作用域變成prototype了,這時(shí)我們將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控制臺(tái)打?。?/p>

這里寫圖片描述

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

若是通過在classpath路徑下自動(dòng)掃描方這種式把組件納入Spring容器中管理,如何指定bean的初始化方法和銷毀方法呢?這時(shí)我們就需要用到兩個(gè)注解:@PostConstruct和@PreDestroy。為了試驗(yàn),我們將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控制臺(tái)會(huì)打印:

這里寫圖片描述

如要查看源碼,可點(diǎn)擊讓Spring自動(dòng)掃描和管理Bean進(jìn)行下載

總結(jié)

以上所述是小編給大家介紹的spring自動(dòng)掃描包,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • SpringBoot結(jié)合JWT登錄權(quán)限控制的實(shí)現(xiàn)

    SpringBoot結(jié)合JWT登錄權(quán)限控制的實(shí)現(xiàn)

    本文主要介紹了SpringBoot結(jié)合JWT登錄權(quán)限控制的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • Hibernate映射解析之關(guān)聯(lián)映射詳解

    Hibernate映射解析之關(guān)聯(lián)映射詳解

    所謂關(guān)聯(lián)映射就是將關(guān)聯(lián)關(guān)系映射到數(shù)據(jù)庫里,在對(duì)象模型中就是一個(gè)或多個(gè)引用。下面這篇文章詳細(xì)的給大家介紹了Hibernate映射解析之關(guān)聯(lián)映射的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-02-02
  • java字符串相似度算法

    java字符串相似度算法

    這篇文章主要介紹了java字符串相似度算法,是Java實(shí)現(xiàn)比較典型的算法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-02-02
  • Java 生成二維碼的工具資料整理

    Java 生成二維碼的工具資料整理

    本文主要介紹Java 生成二維碼的幾種方法,這里給大家詳細(xì)介紹了java生成二維碼的三種工具,并附有示例代碼供大家參考,開發(fā)java 二維碼的朋友可以參考下
    2016-08-08
  • Spring核心思想之淺談IoC容器與依賴倒置(DI)

    Spring核心思想之淺談IoC容器與依賴倒置(DI)

    文章介紹了Spring的IoC和DI機(jī)制,以及MyBatis的動(dòng)態(tài)代理,通過注解和反射,Spring能夠自動(dòng)管理對(duì)象的創(chuàng)建和依賴注入,而MyBatis則通過動(dòng)態(tài)代理實(shí)現(xiàn)了接口方法到數(shù)據(jù)庫操作的映射,文章詳細(xì)解釋了Spring和MyBatis的工作原理,并通過示例代碼展示了它們的結(jié)合使用方式
    2025-01-01
  • Java數(shù)據(jù)結(jié)構(gòu)--時(shí)間和空間復(fù)雜度

    Java數(shù)據(jù)結(jié)構(gòu)--時(shí)間和空間復(fù)雜度

    這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)的時(shí)間和空間復(fù)雜度,小編覺得這篇文寫的不錯(cuò),感興趣的朋友可以了解下,希望能夠給你帶來幫助
    2021-08-08
  • SpringBoot整合SpringBoot-Admin實(shí)現(xiàn)監(jiān)控應(yīng)用功能

    SpringBoot整合SpringBoot-Admin實(shí)現(xiàn)監(jiān)控應(yīng)用功能

    本文主要介紹如何整合Spring Boot Admin,以此監(jiān)控Springboot應(yīng)用,文中有相關(guān)的示例代碼供大家參考,需要的朋友可以參考下
    2023-05-05
  • Java獲取環(huán)境變量(System.getenv)的方法

    Java獲取環(huán)境變量(System.getenv)的方法

    本文主要介紹了Java獲取環(huán)境變量(System.getenv)的方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Spring 方法注解@Bean及配置類掃描路徑

    Spring 方法注解@Bean及配置類掃描路徑

    這篇文章主要介紹了Spring 方法注解@Bean及配置類掃描路徑,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧
    2024-12-12
  • IDEA 必要配置設(shè)置方式

    IDEA 必要配置設(shè)置方式

    這篇文章主要介紹了IDEA 必要配置設(shè)置方式,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-04-04

最新評(píng)論

黄浦区| 榆中县| 九龙县| 孝昌县| 娄底市| 彰化市| 大洼县| 林西县| 乌拉特前旗| 中江县| 滨海县| 进贤县| 兴海县| 闻喜县| 靖远县| 博乐市| 安远县| 平乡县| 时尚| 西平县| 常熟市| 富阳市| 广南县| 新泰市| 改则县| 江阴市| 雷州市| 丽水市| 高唐县| 太和县| 同德县| 包头市| 揭西县| 三亚市| 太保市| 大余县| 泸水县| 黄山市| 平昌县| 手机| 怀仁县|