日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring-注入方式(基于xml方式)

發布時間:2024/10/5 javascript 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring-注入方式(基于xml方式) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.基于xml方式創建對象

<!--配置User類對象的創建 --><bean id="user" class="com.at.spring5.User"></bean>

(1)在spring配置文件中,使用bean標簽,標簽里面添加對應屬性,就可以實現對象創建

(2)在bean標簽有很多屬性
*id屬性:唯一標識
*class屬性:包類路徑

(3)創建對象時,默認執行無參構造方法

2.基于xml方式注入屬性
(1)DI:依賴注入->注入屬性
第一種注入方式:使用set方法注入
創建Book類

package com.at.spring5;public class Book {private String bName;private String author;public void setbName(String bName) {this.bName = bName;}public void setAuthor(String author) {this.author = author;}public void testDemo(){System.out.println(bName+"::"+author);} }

配置bean1.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.xsd"><!--配置User類對象的創建 --><bean id="book" class="com.at.spring5.Book"><property name="bName" value="易筋經"></property><property name="author" value="八段錦"></property></bean> </beans>

創建Test類進行測試

public class TestSpring5 {@Testpublic void testAdd(){//1.加載spring配置文件------》在該步驟創建對象ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");//2.獲取配置創建對象Book book = context.getBean("book", Book.class);book.testDemo();} }

第二種注入方式:使用有參構造注入
創建Orders類

/*** 使用有參構造注入*/ public class Orders {private String name;private String address;public Orders(String name,String address) {this.name = name;this.address=address;}public void orderTest(){System.out.println(name+":"+address);} }

配置bean1.xml文件

<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.xsd"><!--配置User類對象的創建 --><!--使用時set方法注入--> <!-- <bean id="book" class="com.at.spring5.Book">--> <!-- <property name="bName" value="易筋經"></property>--> <!-- <property name="author" value="八段錦"></property>--> <!-- </bean>--><!--使用有參構造注入--><bean id="orders" class="com.at.spring5.Orders"><constructor-arg name="name" value="ssh"></constructor-arg><constructor-arg name="address" value="ssm"></constructor-arg></bean> </beans>

測試類

public class TestSpring5 {@Testpublic void testAdd(){//1.加載spring配置文件------》在該步驟創建對象ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");//2.獲取配置創建對象Orders orders = context.getBean("orders", Orders.class);orders.orderTest();} }

第三種注入方式(不常用,了解即可)
使用名稱空間實現注入

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的Spring-注入方式(基于xml方式)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。