本篇文章為大家展示了Spring數據怎么在Java項目中進行單元配置,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
創新互聯服務項目包括歷城網站建設、歷城網站制作、歷城網頁制作以及歷城網絡營銷策劃等。多年來,我們專注于互聯網行業,利用自身積累的技術優勢、行業經驗、深度合作伙伴關系等,向廣大中小型企業、政府機構等提供互聯網行業的解決方案,歷城網站推廣取得了明顯的社會效益與經濟效益。目前,我們服務的客戶以成都為中心已經輻射到歷城省份的部分城市,未來相信會繼續擴大服務區域并繼續獲得客戶的支持與信任!1. 配置元數據
基于XML的配置元數據的基本結構:beans.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="/tupian/20230522/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="/tupian/20230522/ /tupian/20230522//spring-beans-2.5.xsd"> <bean id="..." class="..."> <!-- collaborators and configuration for this bean go here --> </bean> <bean id="..." class="..."> <!-- collaborators and configuration for this bean go here --> </bean> <!-- 更多的bean的時候 在引用的xml文件一定是要帶spring dtd頭的文件--> <import resource="services.xml"/> </beans>
services.xml
在配置文件里面命名其實id 和name都是一樣的
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="/tupian/20230522/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="/tupian/20230522/ /tupian/20230522//spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="userService" name="userService" class="com.sun.service.UserService"> <property name="name"> <value>sunxin</value> </property> </bean> </beans>
2. 實例化容器
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"beans.xml"});
3. bean的別名
<!--name指向的是已經存在該id的bean,alias是給給該bean命的別名-->
<alias name="userService" alias="user"/>
調用可以通過:
UserService us = (UserService) app.getBean("user");
上述內容就是Spring數據怎么在Java項目中進行單元配置,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注創新互聯行業資訊頻道。
網站標題:Spring數據怎么在Java項目中進行單元配置-創新互聯
文章出自:http://m.newbst.com/article32/dciosc.html
成都網站建設公司_創新互聯,為您提供Google、小程序開發、定制網站、企業建站、移動網站建設、搜索引擎優化
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯