Spring實(shí)戰(zhàn)之搜索Bean類操作示例
本文實(shí)例講述了Spring實(shí)戰(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"> <!-- 自動(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.*; @Component public 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.*; @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 "石斧砍柴好慢"; } }
四 測(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)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
- Spring boot將配置屬性注入到bean類中
- springboot 獲取工具類bean過(guò)程詳解
- Java類獲取Spring中bean的5種方式
- spring boot中的條件裝配bean的實(shí)現(xiàn)
- Spring實(shí)戰(zhàn)之Bean的后處理器操作示例
- Spring實(shí)戰(zhàn)之Bean定義中的SpEL表達(dá)式語(yǔ)言支持操作示例
- Spring實(shí)戰(zhàn)之獲取其他Bean的屬性值操作示例
- Spring實(shí)戰(zhàn)之協(xié)調(diào)作用域不同步的Bean操作示例
- Spring實(shí)戰(zhàn)之Bean銷毀之前的行為操作示例
- Spring的自動(dòng)裝配Bean的三種方式
- Spring中多配置文件及引用其他bean的方式
相關(guān)文章
使用@PropertySource讀取配置文件通過(guò)@Value進(jìn)行參數(shù)注入
這篇文章主要介紹了使用@PropertySource讀取配置文件通過(guò)@Value進(jìn)行參數(shù)注入,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03SQLSyntaxErrorException-ExecutorException報(bào)錯(cuò)解決分析
這篇文章主要為大家介紹了SQLSyntaxErrorException-ExecutorException報(bào)錯(cuò)解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Springboot動(dòng)態(tài)配置AOP切點(diǎn)詳解
這篇文章主要介紹了Springboot動(dòng)態(tài)配置AOP切點(diǎn)詳解,Springboot 可以定義注解切點(diǎn)去攔截注解修飾的類方法以及execution(xxxx)切點(diǎn)去攔截具體的類方法,默認(rèn)情況下我們都會(huì)使用注解@PointCut去定義切點(diǎn),然后定義切面攔截切點(diǎn),需要的朋友可以參考下2023-09-09Java設(shè)計(jì)模式之迭代模式(Iterator模式)介紹
這篇文章主要介紹了Java設(shè)計(jì)模式之迭代模式(Iterator模式)介紹,本文用一個(gè)老師點(diǎn)名的現(xiàn)象描述了迭代模式的使用,需要的朋友可以參考下2015-03-03Spring Boot 中實(shí)現(xiàn)跨域的多種方式小結(jié)
Spring Boot提供了多種方式來(lái)實(shí)現(xiàn)跨域請(qǐng)求,開(kāi)發(fā)者可以根據(jù)具體需求選擇適合的方法,在配置時(shí),要確保不僅考慮安全性,還要兼顧應(yīng)用的靈活性和性能,本文給大家介紹Spring Boot 中實(shí)現(xiàn)跨域的多種方式,感興趣的朋友一起看看吧2024-01-01Java Excel透視表相關(guān)操作實(shí)現(xiàn)代碼
這篇文章主要介紹了Java Excel透視表相關(guān)操作實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08解決springboot3:mybatis-plus依賴錯(cuò)誤:org.springframework.beans.fac
這篇文章主要介紹了解決springboot3:mybatis-plus依賴錯(cuò)誤:org.springframework.beans.factory.UnsatisfiedDependencyException問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07