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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

依赖注入_引用其他的bean

發布時間:2024/4/13 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 依赖注入_引用其他的bean 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

引用其它 Bean

組成應用程序的 Bean 經常需要相互協作以完成應用程序的功能. 要使 Bean 能夠相互訪問, 就必須在 Bean 配置文件中指定對 Bean 的引用

在 Bean 的配置文件中, 可以通過 <ref> 元素或 ref ?屬性為 Bean 的屬性或構造器參數指定對 Bean 的引用.?

也可以在屬性或構造器里包含 Bean 的聲明, 這樣的 Bean 稱為內部 Bean

<?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:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 配置Car, 屬性注入(set方法注入) --><bean id="car" class="com.learn.spring.beans.Car"><property name="brand" ><value>Audi</value></property><property name="crop" value="yiqi"></property><property name="price" value="400000"></property></bean><bean id="person" class="com.learn.spring.beans.Person"><property name="name" value="莫小貝"></property><property name="age" value="12"></property><!-- 引用其他的bean ref:指定要引用的bean --><property name="car" ref="car"><!-- <ref bean="car"/> --></property><!-- <constructor-arg value="" ref=""></constructor-arg> --></bean></beans> package com.learn.spring.test;import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;import com.learn.spring.beans.Book; import com.learn.spring.beans.Car; import com.learn.spring.beans.HelloWorld; import com.learn.spring.beans.Person; import com.learn.spring.beans.PersonList; import com.learn.spring.beans.PersonMap;public class Main {public static void main(String[] args) {Person person = (Person) ctx.getBean("person");System.out.println(person); } }

?

總結

以上是生活随笔為你收集整理的依赖注入_引用其他的bean的全部內容,希望文章能夠幫你解決所遇到的問題。

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