欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于Spring p標(biāo)簽和c標(biāo)簽注入方式

 更新時(shí)間:2021年09月26日 09:51:01   作者:迷惑的菜鳥(niǎo)  
這篇文章主要介紹了Spring p標(biāo)簽和c標(biāo)簽注入方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Spring p和c標(biāo)簽注入方式

1.編寫實(shí)體類

package com.ming04.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private int id;
    private String name;
}

2.配置文件(重點(diǎn))

<?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"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- p命名空間注入 ,可以直接注入屬性的值 :property -->
    <bean id="user" class="com.ming04.pojo.User" p:name="秦小東" p:id="18"/>
    <!-- c命名空間注入 可以構(gòu)造器注入屬性的值 :construct-args -->
    <bean id="user2" class="com.qin.pojo.User" c:id="19" c:name="狂神" />
</beans>

3.測(cè)試

  @Test
    public void Text(){
       ApplicationContext context = new ClassPathXmlApplicationContext("Beans04.xml");
        User user = (User) context.getBean("user2");
        System.out.println(user);
    }

spring Bean注入和P標(biāo)簽使用

1.構(gòu)造方法參數(shù)

對(duì)應(yīng) 配置文件 <constructor-arg> 元素

可以index|name|type 三選一 、三選二 ; ref|value 二選一

2.setter方法注入(開(kāi)發(fā)推薦)

為需要注入屬性提供setter方法

配置 每個(gè)注入屬性, 對(duì)應(yīng)<property> 元素

3.p名稱空間的使用

spring2.5以后,為了簡(jiǎn)化setter方法屬性注入,引用p名稱空間的概念,可以將<property> 子元素,簡(jiǎn)化為<bean>元素屬性配置 ??!

a.

在applicationContext.xml 引入p 名稱空間

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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">

b. 簡(jiǎn)化注入的配置

4.集合類型的屬性注入(了解)

Spring 對(duì)每個(gè)常用集合對(duì)象,提供單獨(dú)元素完成注入

  • List 對(duì)象 ---- <list> 元素
  • Set 對(duì)象 ---- <set> 元素
  • Map對(duì)象 ----<map>元素
  • Properties 對(duì)象 ---- <props> 元素

集合屬性的注入,主要用于框架的配置 !

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論