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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

[Spring5]IOC容器_Bean管理XML方式_创建对象_set注入属性and有参构造注入属性

發布時間:2023/12/4 asp.net 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Spring5]IOC容器_Bean管理XML方式_创建对象_set注入属性and有参构造注入属性 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

IOC操作 Bean管理

什么是Bean管理

1.Bean管理指的是兩個操作:

a.Spring創建對象

b.Spring注入屬性

2.Bean管理操作有兩種方式

a.基于xml配置文件方式實現

b.基于注解方式實現

IOC操作Bean管理(基于xml方式)

1.基于xml方式創建對象

<!--配置User對象創建--><bean id = "user" class = "com.atguigu.spring.User"></bean>

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

b.在bean標簽有很多屬性,介紹常用的屬性

i.id屬性:唯一標識

ii.class屬性:類全路徑(包類路徑)

2.基于xml方式注入屬性

3.創建對象的時候,默認執行無參構造方法完成對象創建

2.基于xml方式注入屬性

a.DI:依賴注入,就是注入屬性

i.第一種注入方式:使用set方法進行注入

(1)創建類,定義屬性和對應的set方法

package com.atguigu.spring;/*** 演示使用set方法進行注入屬性*/ public class Book {private String bname;private String bauthor;public String getBname() {return bname;}public void setBname(String bname) {this.bname = bname;}public String getBauthor() {return bauthor;}public void setBauthor(String bauthor) {this.bauthor = bauthor;}public void testDemo(){System.out.println(bname + "::" + bauthor);} }

(2)在spring配置文件配置對象創建,配置屬性注入

<?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"><!-- &lt;!&ndash;配置User對象創建&ndash;&gt;--> <!-- <bean id = "user" class = "com.atguigu.spring.User"></bean>--><!--set方法注入屬性--><bean id = "book" class = "com.atguigu.spring.Book"><!--使用property完成屬性注入--><property name="bname" value="易筋經"></property><property name="bauthor" value="達摩老祖"></property></bean> </beans>

(3)測試

public class TestSpring05 {@Testpublic void testAdd(){ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");Book book = context.getBean("book", Book.class);System.out.println(book);book.testDemo();}}
ii.第二種注入方式:使用有參構造方法進行注入

(1)創建類,定義屬性,創建屬性對應有參數構造方法

package com.atguigu.spring;/*** 使用有參數構造注入*/ 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);}}

(2)在spring配置文件中進行配置

<bean id = "order" class = "com.atguigu.spring.Orders"><constructor-arg name = "name" value = "電腦"></constructor-arg><constructor-arg name = "address" value = "China"></constructor-arg></bean>

(3)測試

package com.atguigu.test;import com.atguigu.spring.User; import javafx.application.Application; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.atguigu.spring.Orders;public class TestSprindAdd {@Testpublic void testOrder(){//1.加載spring配置文件ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");//2.獲取配置創建的對象Orders orders = context.getBean("order", Orders.class);System.out.println(orders);orders.orderTest();}}

總結

以上是生活随笔為你收集整理的[Spring5]IOC容器_Bean管理XML方式_创建对象_set注入属性and有参构造注入属性的全部內容,希望文章能夠幫你解決所遇到的問題。

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