Spring中如何進行搜索Bean類操作,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
一 配置文件
二 接口
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; } // 實現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 "石斧砍柴好慢"; }}
四 測試類
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實例的名 System.out.println("--------------" + java.util.Arrays.toString(ctx.getBeanDefinitionNames())); }}
五 測試結果
--------------[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]
關于Spring中如何進行搜索Bean類操作問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注創(chuàng)新互聯行業(yè)資訊頻道了解更多相關知識。