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過程詳解
- Java類獲取Spring中bean的5種方式
- spring boot中的條件裝配bean的實(shí)現(xiàn)
- Spring實(shí)戰(zhàn)之Bean的后處理器操作示例
- Spring實(shí)戰(zhàn)之Bean定義中的SpEL表達(dá)式語言支持操作示例
- 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)文章
springBoot整合shiro如何解決讀取不到@value值問題
這篇文章主要介紹了springBoot整合shiro如何解決讀取不到@value值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,2023-08-08
Mybatis關(guān)聯(lián)查詢結(jié)果集對(duì)象嵌套的具體使用
在查詢時(shí)經(jīng)常出現(xiàn)一對(duì)多”的關(guān)系,所有會(huì)出現(xiàn)嵌套對(duì)象的情況,本文主要介紹了Mybatis關(guān)聯(lián)查詢結(jié)果集對(duì)象嵌套的具體使用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
解決springboot整合cxf-jaxrs中json轉(zhuǎn)換的問題
這篇文章主要介紹了解決springboot整合cxf-jaxrs中json轉(zhuǎn)換的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
JAVA對(duì)象和字節(jié)數(shù)組互轉(zhuǎn)操作
這篇文章主要介紹了JAVA對(duì)象和字節(jié)數(shù)組互轉(zhuǎn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Java利用Geotools實(shí)現(xiàn)不同坐標(biāo)系之間坐標(biāo)轉(zhuǎn)換
GeoTools 是一個(gè)開源的 Java GIS 工具包,可利用它來開發(fā)符合標(biāo)準(zhǔn)的地理信息系統(tǒng)。本文將利用工具包Geotools實(shí)現(xiàn)不同坐標(biāo)系之間坐標(biāo)轉(zhuǎn)換,感興趣的可以了解一下2022-08-08
Java8中stream流的collectingAndThen方法應(yīng)用實(shí)例詳解
Java8中的Stream流提供了collectingAndThen方法,用于對(duì)歸納結(jié)果進(jìn)行二次處理,文章通過User類的數(shù)據(jù)填充,演示了如何使用該方法進(jìn)行集合去重、查找最高工資員工、計(jì)算平均工資等操作,感興趣的朋友跟隨小編一起看看吧2025-03-03
Springboot集成Kafka進(jìn)行批量消費(fèi)及踩坑點(diǎn)
本文主要介紹了Springboot集成Kafka進(jìn)行批量消費(fèi)及踩坑點(diǎn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12

