Spring實戰(zhàn)之搜索Bean類操作示例
本文實例講述了Spring實戰(zhàn)之搜索Bean類操作。分享給大家供大家參考,具體如下:
一 配置文件
<?xml version="1.0" encoding="GBK"?> <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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 自動掃描指定包及其子包下的所有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.*; @Component public class Chinese implements Person { private Axe axe; // axe的setter方法 public void setAxe(Axe axe) { this.axe = axe; } // 實現(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.*; @Component public class SteelAxe implements Axe { public String chop() { return "鋼斧砍柴真快"; } }
StoneAxe
package org.crazyit.app.service.impl; import org.springframework.stereotype.*; import org.crazyit.app.service.*; @Component public 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]
更多關于java相關內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進階教程》、《Java數(shù)據(jù)結構與算法教程》、《Java操作DOM節(jié)點技巧總結》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設計有所幫助。
- Spring boot將配置屬性注入到bean類中
- springboot 獲取工具類bean過程詳解
- Java類獲取Spring中bean的5種方式
- spring boot中的條件裝配bean的實現(xiàn)
- Spring實戰(zhàn)之Bean的后處理器操作示例
- Spring實戰(zhàn)之Bean定義中的SpEL表達式語言支持操作示例
- Spring實戰(zhàn)之獲取其他Bean的屬性值操作示例
- Spring實戰(zhàn)之協(xié)調(diào)作用域不同步的Bean操作示例
- Spring實戰(zhàn)之Bean銷毀之前的行為操作示例
- Spring的自動裝配Bean的三種方式
- Spring中多配置文件及引用其他bean的方式
相關文章
使用@PropertySource讀取配置文件通過@Value進行參數(shù)注入
這篇文章主要介紹了使用@PropertySource讀取配置文件通過@Value進行參數(shù)注入,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03SQLSyntaxErrorException-ExecutorException報錯解決分析
這篇文章主要為大家介紹了SQLSyntaxErrorException-ExecutorException報錯解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08Spring Boot 中實現(xiàn)跨域的多種方式小結
Spring Boot提供了多種方式來實現(xiàn)跨域請求,開發(fā)者可以根據(jù)具體需求選擇適合的方法,在配置時,要確保不僅考慮安全性,還要兼顧應用的靈活性和性能,本文給大家介紹Spring Boot 中實現(xiàn)跨域的多種方式,感興趣的朋友一起看看吧2024-01-01解決springboot3:mybatis-plus依賴錯誤:org.springframework.beans.fac
這篇文章主要介紹了解決springboot3:mybatis-plus依賴錯誤:org.springframework.beans.factory.UnsatisfiedDependencyException問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07