国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

15、spring自動掃描組件

2019-11-08 01:38:41
字體:
供稿:網(wǎng)友

15、sPRing自動掃描組件

自動組件掃描

第一步:使用@Component標(biāo)記類,使得spring容器能夠識別為一個組件

@Component public class UserDAO { public void outPut(){ System.out.println("你好,這是一個自動掃描的demo"); }}

第二步:在bean配置文件中啟動掃描

<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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.main.autowrite.autoScanning" /></beans>

第三步:提取bean并調(diào)用

@Test public void test(){ applicationContext context = new ClassPathXmlApplicationContext("com/main/autowrite/autoScanning/bean.xml"); UserDAO dao = (UserDAO)context.getBean("userDAO"); dao.outPut(); }

輸出結(jié)果為:

你好,這是一個自動掃描的demo

自定義組件名稱:使用@Service(“value”)

@Service("student") public class User{...} //調(diào)用方法 User user = (User)context.getBean("student");

實(shí)際上,可以用以下四種注解來標(biāo)識組件

@Component ——表示自動掃描組件@Service ——表示在業(yè)務(wù)層服務(wù)組件@Controller ——表示在表示層控制器組件@Repository ——表示在持久層DAO組件

攔截器

允許特定組件可以通過

<context:component-scan base-package="com.yiibai" > <context:include-filter type="regex" expression="com.yiibai.customer.dao.*DAO.*" /> <context:include-filter type="regex" expression="com.yiibai.customer.services.*Service.*" /> </context:component-scan>

說明:上述例子中將允許含有關(guān)鍵字DAO和Service的組件,在spring容器中注冊,其他類型的組件都會被攔截掉

不允許特定組件通過

<context:component-scan base-package="com.yiibai" > <context:exclude-filter type="regex" expression="com.yiibai.customer.dao.*DAO.*" /> </context:component-scan>

說明:上述例子中將攔截掉含有關(guān)鍵字DAO的組件,其他則pass

<context:component-scan base-package="com.yiibai.customer" > <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan>

說明:不允許含有@Service的組件通過

注意:typeregex是指關(guān)鍵字typeannoation是指特定注解


上一篇:leetcode463

下一篇:c# NPOI EXcel讀取

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 保山市| 印江| 莆田市| 宁安市| 平顶山市| 营山县| 天门市| 明水县| 都昌县| 武鸣县| 湟源县| 方山县| 广丰县| 绥芬河市| 夏河县| 上饶市| 娱乐| 乡城县| 沙田区| 巫溪县| 奉新县| 自贡市| 兴安盟| 睢宁县| 蒙城县| 乐业县| 冷水江市| 阳曲县| 辽中县| 德兴市| 嵊州市| 双鸭山市| 巴彦淖尔市| 新闻| 中超| 上饶县| 沅陵县| 平遥县| 柳江县| 南部县| 江华|