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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

属性依赖注入

發(fā)布時間:2023/12/18 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 属性依赖注入 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.依賴注入方法

  手動裝配和自動裝配

2.手動裝配

2.1 基于xml裝配

2.1.1 構(gòu)造方法

<!-- 構(gòu)造方法注入<constructor-arg>name:參數(shù)名type:類型value: --> <bean id="user" class="g_xml.constructor.User"><constructor-arg name="id" type="java.lang.Integer" value="11"></constructor-arg><constructor-arg name="username" type="java.lang.String" value="張三"></constructor-arg><constructor-arg name="age" type="java.lang.Integer" value="10"></constructor-arg> </bean>

?

2.1.2 setter方法

<bean id="addr" class="g_xml.setter.Address" ><property name="addr" value="上海浦東"></property><property name="tel" value="138234324"></property> </bean> <bean id="user" class="g_xml.setter.User"><property name="id" value="1"></property><property name="username" value="張三"></property><property name="age" value="12"></property><property name="homeAddr" ref="addr"></property><property name="companryAddr" ref="addr"></property> </bean>

2.1.3 p命名空間

  對setter方法注入進行簡化,替換<property>為:

  但是需要加入schame命名空間

2.1.4 spEL

  對<property>進行了統(tǒng)一編程,所有的內(nèi)容都是用value

<property name="" value="#{EL表達(dá)式}">

EL:

  #{123}

  #{"hello"}

  #{beanId} :另一個bean的引用

  #{beanId.propName}

  #{beanId.toString()}

  #{T(類).靜態(tài)方法|字段}

2.1.5 集合注入

?

<bean id="users" class="g_xml.collections.Users"><property name="strArr"><array><value>戰(zhàn)三</value><value>李四</value><value>王五</value></array></property><property name="list"><list><value>111</value><value>222</value><value>333</value></list></property><property name="set"><set><value>set1</value><value>set2</value><value>set3</value></set></property><property name="map"><map><entry key="key1" value="value1"></entry><entry key="key2" value="value2"></entry></map></property><property name="properties"><props><prop key="k1">value1</prop><prop key="k2">value2</prop></props></property> </bean>

?

2.2 基于注解

?  注解就是一個類,使用@注解名稱,取代xml配置

2.2.1 @Component? == <bean id="" class="">

  ? @Component("id")

? 使用注解之前,必須添加命名空間,讓spring掃描帶有注解的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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!-- bean definitions here 配置所需要創(chuàng)建的實例對象,放入spring容器中--><!--<bean id="userService" class="a_IoC.UserServiceImp"></bean>--><context:component-scan base-package="h_annotation"></context:component-scan> </beans>

?

2.2.2 在web開發(fā)中,提供了三個Component衍生出的注解

@Repository? DAO層

@Service? ? ? ?Service層

@Controller? ?Web層

2.2.3 依賴注入? (可以給私有屬性注入,也可以給setter方法注入)

  普通值:@Value()

  引用值:

    方式1:按照類型注入-->@Autowired

    方式2:按照名稱注入 -->

        @Autowired

        @Qualifier("名稱")

    方式2:按照名稱注入 -->

      @Resource("名稱")?

?

轉(zhuǎn)載于:https://www.cnblogs.com/zhuxiang1633/p/8502711.html

總結(jié)

以上是生活随笔為你收集整理的属性依赖注入的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。