Java Spring數(shù)據(jù)單元配置過程解析
基本原理 - 容器和bean
在Spring中,那些組成你應(yīng)用程序的主體(backbone)及由Spring IoC容器所管理的對象,被稱之為bean。 簡單地講,bean就是由Spring容器初始化、裝配及管理的對象,除此之外,bean就與應(yīng)用程序中的其他對象沒有什么區(qū)別了。
也就是說,其實(shí)spring 就是在加載配置文件beans.xml的時(shí)候,通過反射機(jī)制,去實(shí)例化<bean>標(biāo)簽里面的類的過程。這里可以通過在類的默認(rèn)無參構(gòu)造方法中寫點(diǎn)東西判斷出來。
1. 配置元數(shù)據(jù)
基于XML的配置元數(shù)據(jù)的基本結(jié)構(gòu):beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/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的時(shí)候 在引用的xml文件一定是要帶spring dtd頭的文件-->
<import resource="services.xml"/>
</beans>
services.xml
在配置文件里面命名其實(shí)id 和name都是一樣的
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/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. 實(shí)例化容器
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"beans.xml"});
3. bean的別名
<!--name指向的是已經(jīng)存在該id的bean,alias是給給該bean命的別名-->
<alias name="userService" alias="user"/>
調(diào)用可以通過:
UserService us = (UserService) app.getBean("user");
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java基礎(chǔ)之spring5新功能學(xué)習(xí)
- Java Spring5學(xué)習(xí)之JdbcTemplate詳解
- Java基礎(chǔ)之Spring5的核心之一IOC容器
- SpringBoot+JavaMailSender實(shí)現(xiàn)騰訊企業(yè)郵箱配置
- Spring通過Java配置集成Tomcat的方法
- java、spring、springboot中整合Redis的詳細(xì)講解
- Java中Spring Boot支付寶掃碼支付及支付回調(diào)的實(shí)現(xiàn)代碼
- Spring的@Validation和javax包下的@Valid區(qū)別以及自定義校驗(yàn)注解
- 最優(yōu)雅地整合 Spring & Spring MVC & MyBatis 搭建 Java 企業(yè)級應(yīng)用(附源碼)
- Java Spring WEB應(yīng)用實(shí)例化如何實(shí)現(xiàn)
- Java Spring事務(wù)使用及驗(yàn)證過程詳解
- Spring5學(xué)習(xí)之基礎(chǔ)知識總結(jié)
相關(guān)文章
SpringBoot mybatis 實(shí)現(xiàn)多級樹形菜單的示例代碼
這篇文章主要介紹了SpringBoot mybatis 實(shí)現(xiàn)多級樹形菜單的示例代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-05-05
Mybatis事務(wù)如何跟Spring結(jié)合(數(shù)據(jù)庫事務(wù)特性和Spring事務(wù)管理源碼)
MyBatis與Spring的事務(wù)結(jié)合主要是通過Spring的事務(wù)管理和MyBatis的數(shù)據(jù)庫操作來實(shí)現(xiàn)的,在本文中,我們將從數(shù)據(jù)庫事務(wù)特性和Spring事務(wù)管理源碼兩個(gè)角度來分析MyBatis事務(wù)如何與Spring結(jié)合到一起的原理,感興趣的朋友一起看看吧2024-01-01
springboot使用com.github.binarywang包實(shí)現(xiàn)微信網(wǎng)頁上的支付和退款
最近做項(xiàng)目需要實(shí)現(xiàn)在pc端需要實(shí)現(xiàn)微信的支付,本文主要介紹了springboot使用com.github.binarywang包實(shí)現(xiàn)微信網(wǎng)頁上的支付和退款,具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05
簡單了解springboot中的配置文件相關(guān)知識
這篇文章主要介紹了簡單了解springboot中的配置文件相關(guān)知識,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
Java利用MYSQL LOAD DATA LOCAL INFILE實(shí)現(xiàn)大批量導(dǎo)入數(shù)據(jù)到MySQL
Mysql load data的使用,MySQL的LOAD DATAINFILE語句用于高速地從一個(gè)文本文件中讀取行,并裝入一個(gè)表中2018-03-03
解決Spring Data Jpa 實(shí)體類自動創(chuàng)建數(shù)據(jù)庫表失敗問題
這篇文章主要介紹了解決Spring Data Jpa 實(shí)體類自動創(chuàng)建數(shù)據(jù)庫表失敗問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
基于springboot activiti 配置項(xiàng)解析
這篇文章主要介紹了springboot activiti 配置項(xiàng)解析,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
Mybatis-plus基于redis實(shí)現(xiàn)二級緩存過程解析
這篇文章主要介紹了Mybatis-plus基于redis實(shí)現(xiàn)二級緩存過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08

