小編給大家分享一下SpringBean如何獲取Spring容器操作,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

成都創新互聯公司從2013年成立,先為吉木乃等服務建站,吉木乃等地企業,進行企業商務咨詢服務。為吉木乃企業網站制作PC+手機+微官網三網同步一站式服務解決您的所有建站問題。
具體如下:
一 配置
<?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"> <!-- 加載容器國際化所需要的語言資源文件 --> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <list> <value>message</value> </list> </property> </bean> <!-- Spring容器會檢測容器中所有Bean,如果發現某個Bean實現了 ApplicationContextAware接口,Spring容器會在創建該Bean之后, 自動調用該Bean的setApplicationContext()方法,調用該方法時, 會將容器本身作為參數傳給該方法。--> <bean id="person" class="org.crazyit.app.service.Person"/></beans>
二 資源文件
hello=\u6b22\u8fce\u4f60\uff0c{0}now=\u73b0\u5728\u65f6\u95f4\u662f\uff1a{0}
三 Bean
package org.crazyit.app.service;import org.springframework.context.*;import org.springframework.beans.BeansException;import java.util.Locale;public class Person implements ApplicationContextAware{ // 將BeanFactory容器以成員變量保存 private ApplicationContext ctx; /* Spring容器會檢測容器中所有Bean,如果發現某個Bean實現了ApplicationContextAware接口, Spring容器會在創建該Bean之后,自動調用該方法,調用該方法時, 會將容器本身作為參數傳給該方法。*/ public void setApplicationContext(ApplicationContext ctx) throws BeansException { this.ctx = ctx; } public void sayHi(String name) { System.out.println(ctx.getMessage("hello" , new String[]{name} , Locale.getDefault(Locale.Category.FORMAT))); }}
四 測試類
package lee;import java.io.*;import org.springframework.context.*;import org.springframework.context.support.*;import org.crazyit.app.service.*;public class SpringTest{ public static void main(String[] args)throws Exception { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); Person p = ctx.getBean("person" , Person.class); p.sayHi("孫悟空"); }}
以上是“SpringBean如何獲取Spring容器操作”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注創新互聯行業資訊頻道!
當前標題:SpringBean如何獲取Spring容器操作
網頁鏈接:http://www.yijiale78.com/article18/ghdsgp.html
成都網站建設公司_創新互聯,為您提供網站內鏈、企業建站、虛擬主機、網站排名、搜索引擎優化、自適應網站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯