Spring

Spring入門(使用spring框架創(chuàng)建對象):

1.導(dǎo)入Spring相關(guān)的包

junit? ? Spring-context? ? Mybatis? Mybatis-spring


2.配置文件(spring.xml) 文件中必須要有xsi官方命名空間

<bean id="接口名" class="實現(xiàn)類全限定名">

bean的子標(biāo)簽property標(biāo)簽(用來在類中注入屬性) property中的屬性:

name:屬性名? ? ?ref:引用對象? ? ?value:普通值 比如double long int string

例如: <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">

3. 在idea中使用 Spring框架

context = new ClassPathXmlApplicationContext("文件名")

context.getBean("bean中id值")? ?獲取接口對應(yīng)的實現(xiàn)類對象

因為在Service中用到dao的對象 所以在spring.xml中的bean標(biāo)簽要使用property標(biāo)簽注入

例如:?<bean id="accountService"class="com.itheima.service.impl.AccountServiceImpl">

? ? ? ? ? ? ? ? ?<property? name="accountDao"? ref="accountDao"/>?

? ? ? ? ? ?</bean>? ? 注入property快捷鍵 Alt+insert

注意: 1.?這樣注入必須要求在 Service類中要有accountDao的set方法

? ? ? ? ? ?2.?name="accountDao" 其中property標(biāo)簽name的屬性值要和類中set方法名一致

? ? ? ? ? ?3.??ref="accountDao"? ref引用

二. 構(gòu)造注入(使用構(gòu)造函數(shù))

格式: <constructor-arg name="字段名" value="賦值"></constructor-arg>

使用場景:使用此類時必須要帶有參數(shù) 不植入?yún)?shù)無法創(chuàng)建對象


三.其他各種注入

依次為: 數(shù)組array? list?集合?set集合? map集合? properties文件 注入


四.創(chuàng)建對象的方式

1.實例工廠方法創(chuàng)建


2.Spring factoryBean 標(biāo)準(zhǔn)化方式創(chuàng)建


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容