Spring實(shí)戰(zhàn)之容器中的工程Bean用法示例
本文實(shí)例講述了Spring容器中的工程Bean用法。分享給大家供大家參考,具體如下:
一 配置
<?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <!-- 下面配置相當(dāng)于如下代碼: FactoryBean factory = new org.crazyit.app.factory.GetFieldFactoryBean(); factory.setTargetClass("java.awt.BorderLayout"); factory.setTargetField("NORTH"); north = factory.getObject(); --> <bean id="north" class="org.crazyit.app.factory.GetFieldFactoryBean"> <property name="targetClass" value="java.awt.BorderLayout"/> <property name="targetField" value="NORTH"/> </bean> <!-- 下面配置相當(dāng)于如下代碼: FactoryBean factory = new org.crazyit.app.factory.GetFieldFactoryBean(); factory.setTargetClass("java.sql.ResultSet"); factory.setTargetField("TYPE_SCROLL_SENSITIVE"); north = factory.getObject(); --> <bean id="theValue" class="org.crazyit.app.factory.GetFieldFactoryBean"> <property name="targetClass" value="java.sql.ResultSet"/> <property name="targetField" value="TYPE_SCROLL_SENSITIVE"/> </bean> </beans>
二 Bean工廠
package org.crazyit.app.factory; import java.lang.reflect.*; import org.springframework.beans.factory.FactoryBean; public class GetFieldFactoryBean implements FactoryBean<Object> { private String targetClass; private String targetField; // targetClass的setter方法 public void setTargetClass(String targetClass) { this.targetClass = targetClass; } // targetField的setter方法 public void setTargetField(String targetField) { this.targetField = targetField; } // 返回工廠Bean所生產(chǎn)的產(chǎn)品 public Object getObject() throws Exception { Class<?> clazz = Class.forName(targetClass); Field field = clazz.getField(targetField); return field.get(null); } // 獲取工廠Bean所生產(chǎn)的產(chǎn)品的類型 public Class<? extends Object> getObjectType() { return Object.class; } // 返回該工廠Bean所生成的產(chǎn)品是否為單例 public boolean isSingleton() { return false; } }
三 測試類
package lee; import org.springframework.context.*; import org.springframework.context.support.*; public class SpringTest { public static void main(String[] args)throws Exception { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 下面2行代碼獲取的FactoryBean的產(chǎn)品 System.out.println(ctx.getBean("north")); System.out.println(ctx.getBean("theValue")); // 下面代碼可獲取的FactoryBean本身 System.out.println(ctx.getBean("&theValue")); } }
四 測試結(jié)果
North
1005
org.crazyit.app.factory.GetFieldFactoryBean@56ac3a89
五 說明
java.awt.BorderLayout定義如下
public class BorderLayout implements LayoutManager2, java.io.Serializable { /** * The north layout constraint (top of container). */ public static final String NORTH = "North"; }
java.sql.ResultSet定義如下
public interface ResultSet extends Wrapper, AutoCloseable { int TYPE_SCROLL_SENSITIVE = 1005; }
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計(jì)有所幫助。
- Spring實(shí)戰(zhàn)之讓Bean獲取Spring容器操作示例
- Spring容器添加組件方式實(shí)現(xiàn)
- springboot關(guān)于容器啟動事件總結(jié)
- Spring創(chuàng)建IOC容器的方式解析
- Spring IoC容器知識點(diǎn)詳解
- 基于SpringMVC的全局異常處理器介紹
- 解析Java的Spring框架的BeanPostProcessor發(fā)布處理器
- Spring中的后置處理器BeanPostProcessor詳解
- Spring Validator接口校驗(yàn)與全局異常處理器
- Spring實(shí)戰(zhàn)之Bean的后處理器操作示例
- Spring實(shí)戰(zhàn)之容器后處理器操作示例
相關(guān)文章
SpringBoot項(xiàng)目整合MybatisPlus并使用SQLite作為數(shù)據(jù)庫的過程
SQLite是一個緊湊的庫,啟用所有功能后,庫大小可以小于 750KiB, 具體取決于目標(biāo)平臺和編譯器優(yōu)化設(shè)置, 內(nèi)存使用量和速度之間需要權(quán)衡,這篇文章主要介紹了SpringBoot項(xiàng)目整合MybatisPlus并使用SQLite作為數(shù)據(jù)庫,需要的朋友可以參考下2024-07-07Java實(shí)現(xiàn)將PDF轉(zhuǎn)為PDF/A
通過將PDF格式轉(zhuǎn)換為PDF/A格式,可保護(hù)文檔布局、格式、字體、大小等不受更改,從而實(shí)現(xiàn)文檔安全保護(hù)的目的,同時又能保證文檔可讀、可訪問。本文將為大家介紹如何實(shí)現(xiàn)這一轉(zhuǎn)換,需要的可以參考一下2022-01-01SpringBoot?@GroupSequenceProvider注解實(shí)現(xiàn)bean多屬性聯(lián)合校驗(yàn)的示例代碼
這篇文章主要介紹了SpringBoot?@GroupSequenceProvider注解實(shí)現(xiàn)bean多屬性聯(lián)合校驗(yàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08AQS加鎖機(jī)制Synchronized相似點(diǎn)詳解
這篇文章主要為大家介紹了AQS加鎖機(jī)制Synchronized相似點(diǎn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10springboot使用logback文件查看錯誤日志過程詳解
這篇文章主要介紹了springboot使用logback文件查看錯誤日志過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09Java面試題沖刺第三十天--數(shù)據(jù)庫(6)
這篇文章主要為大家分享了最有價(jià)值的三道關(guān)于數(shù)據(jù)庫的面試題,涵蓋內(nèi)容全面,包括數(shù)據(jù)結(jié)構(gòu)和算法相關(guān)的題目、經(jīng)典面試編程題等,感興趣的小伙伴們可以參考一下2021-09-09Mybatis-Plus中的selectByMap使用實(shí)例
Mybatis-Plus來對數(shù)據(jù)庫進(jìn)行增刪改查時,將里面的函數(shù)試了個遍,接下來我就將使用selectByMap函數(shù)的簡單測試實(shí)例寫出來,方便沒有使用過的朋友們快速上手,感興趣的可以了解一下2021-11-11