Spring中如何進(jìn)行搜索Bean類操作,針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。

一 配置文件
<?xml version="1.0" encoding="GBK"?><beans xmlns="/tupian/20230522/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="/tupian/20230522/" xsi:schemaLocation="/tupian/20230522/ /tupian/20230522//spring-beans-4.0.xsd /tupian/20230522/ /tupian/20230522//spring-context-4.0.xsd"> <!-- 自動(dòng)掃描指定包及其子包下的所有Bean類 --> <context:component-scan base-package="org.crazyit.app.service"/></beans>
二 接口
Axe
package org.crazyit.app.service;public interface Axe{ public String chop();}
Person
package org.crazyit.app.service;public interface Person{ public void useAxe();}
三 Bean
Chinese
package org.crazyit.app.service.impl;import org.springframework.stereotype.*;import org.crazyit.app.service.*;@Componentpublic class Chinese implements Person{ private Axe axe; // axe的setter方法 public void setAxe(Axe axe) { this.axe = axe; } // 實(shí)現(xiàn)Person接口的useAxe()方法 public void useAxe() { System.out.println(axe.chop()); }}
SteelAxe
package org.crazyit.app.service.impl;import org.springframework.stereotype.*;import org.crazyit.app.service.*;@Componentpublic class SteelAxe implements Axe{ public String chop() { return "鋼斧砍柴真快"; }}
StoneAxe
package org.crazyit.app.service.impl;import org.springframework.stereotype.*;import org.crazyit.app.service.*;@Componentpublic class StoneAxe implements Axe{ public String chop() { return "石斧砍柴好慢"; }}
四 測(cè)試類
package lee;import org.springframework.context.*;import org.springframework.context.support.*;public class BeanTest{ public static void main(String[] args) { // 創(chuàng)建Spring容器 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 獲取Spring容器中的所有Bean實(shí)例的名 System.out.println("--------------" + java.util.Arrays.toString(ctx.getBeanDefinitionNames())); }}
五 測(cè)試結(jié)果
--------------[chinese, steelAxe, stoneAxe, org.springframework.context.annotation.internalConfigurationAnnotationProcessor, org.springframework.context.annotation.internalAutowiredAnnotationProcessor, org.springframework.context.annotation.internalRequiredAnnotationProcessor, org.springframework.context.annotation.internalCommonAnnotationProcessor, org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor, org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor]
關(guān)于Spring中如何進(jìn)行搜索Bean類操作問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
新聞名稱:Spring中如何進(jìn)行搜索Bean類操作-創(chuàng)新互聯(lián)
分享地址:http://chinadenli.net/article46/digdhg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、企業(yè)網(wǎng)站制作、網(wǎng)站收錄、網(wǎng)頁(yè)設(shè)計(jì)公司、網(wǎng)站營(yíng)銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容