1. 程式人生 > 其它 >基於 xml 方式的bean管理

基於 xml 方式的bean管理

技術標籤:Spring

1、基於 xml 方式建立物件

(1)在 spring 配置檔案,使用 bean 標籤,標籤裡面新增對應屬性,就可以實現物件建立
(2)在 bean 標籤有很多屬性,介紹常用的屬性

  • id 屬性:唯一標識
  • class 屬性:類全路徑(包類路徑)

(3)建立物件時候,預設也是執行無引數構造方法完成物件建立

2、基於 xml 方式注入屬性

(1)DI:依賴注入,就是注入屬性

3、第一種注入方式:使用 set 方法進行注入

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

public class Book {
    //建立屬性
    private String bname;
private String bauthor; //建立屬性對應的 set 方法 public void setBname(String bname) { this.bname = bname; } public void setBauthor(String bauthor) { this.bauthor = bauthor; } public String toString() { return "Book{" + "bname='"
+ bname + '\'' + ", bauthor='" + bauthor + '\'' + '}'; } }

(2)在 spring 配置檔案配置物件建立,配置屬性注入

    <!--2set方法注入屬性-->
    <bean id="book" class="com.atguigu.spring5.Book">
        <!--使用property屬性完成屬性注入
            name:類裡面的屬性名稱
            value:向屬性中注入的值
        -->
<property name="bname" value="java學習指南"></property> <property name="bauthor" value="大佬"></property> </bean>

(3)測試

 @Test
    public void testBook(){
        //1載入Spring配置檔案
        ApplicationContext context = new ClassPathXmlApplicationContext ("bean1.xml");
        //2獲取配置建立的物件
        Book book = context.getBean ("book", Book.class);
        System.out.println (book);
    }

(4)結果
在這裡插入圖片描述

4、第二種注入方式:使用有引數構造進行注入

(1)建立類,定義屬性和有引數的構造方法

public class Orders {
    //屬性
    private String oname;
    private String address;

    //有引數構造
    public Orders(String oname, String address) {
        this.oname = oname;
        this.address = address;
    }
     public String toString() {
        return "Orders{" +
                "oname='" + oname + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

(2)在 spring 配置檔案中進行配置

  <!--3使用構造方法進行熟屬性注入-->
    <bean id="orders" class="com.atguigu.spring5.Orders">
        <constructor-arg name="oname" value="電腦"></constructor-arg>
        <constructor-arg name="address" value="china"></constructor-arg>
    </bean>

(3)測試

 @Test
    public void testOrders(){
        //1載入Spring配置檔案
        ApplicationContext context = new ClassPathXmlApplicationContext ("bean1.xml");

        //2獲取配置建立的物件
        Orders orders = context.getBean ("orders", Orders.class);
        System.out.println (orders);
    }

(4)結果
在這裡插入圖片描述

第三種注入方式: p 名稱空間注入

(1)新增 p 名稱空間在配置檔案中

在約束中增加:xmlns:p="http://www.springframework.org/schema/p"

(2)進行屬性注入,在 bean 標籤裡面進行操作

    <!--4p名稱空間注入-->
<bean id="book1" class="com.atguigu.spring5.Book" p:bauthor="絕世神功" p:bname="帶佬"></bean>

(3)測試

   @Test
    public void testp(){
        //1載入Spring配置檔案
        ApplicationContext context = new ClassPathXmlApplicationContext ("bean1.xml");

        //2獲取配置建立的物件
        Book book1 = context.getBean ("book1", Book.class);
        System.out.println (book1);
    }

(4)結果
在這裡插入圖片描述

xml 注入其他型別屬性

1、字面量null 值

(1)null 值在xml中配置

 <!--5字面量-->
    <bean id="book1" class="com.atguigu.spring5.Book">
        <property name="bname" value="字面量null"></property>
        <property name="bauthor">
            <null></null>
        </property>
    </bean>

(2)測試

 @Test
    public void testnull(){
        //1載入Spring配置檔案
        ApplicationContext context = new ClassPathXmlApplicationContext ("bean2.xml");

        //2獲取配置建立的物件
        Book book1 = context.getBean ("book1",Book.class);
        System.out.println (book1);
    }

(3)結果
在這裡插入圖片描述
2、字面量,屬性值包含特殊符號

    <!--5字面量,屬性值包含特殊屬性-->
    <bean id="book2" class="com.atguigu.spring5.Book">
        <property name="bname" value="字面量包含特殊值"></property>
        <property name="bauthor">
            <value><![CDATA[<<哈哈哈>>]]></value>
        </property>
    </bean>

(2)測試

    @Test
    public void testteshu(){
        //1載入Spring配置檔案
        ApplicationContext context = new ClassPathXmlApplicationContext ("bean2.xml");

        //2獲取配置建立的物件
        Book book2 = context.getBean ("book2",Book.class);
        System.out.println (book2);
    }

(3)結果
在這裡插入圖片描述

2、注入屬性-外部 bean

(1)建立兩個類 service 類和 dao 類

package com.atguigu.spring5.dao;
public interface UserDao {
    public void update();
}
package com.atguigu.spring5.dao;
public class UserDaoImp implements UserDao {
    @Override
    public void update() {
        System.out.println ("update...");
    }
}

(2)在 service 呼叫 dao 裡面的方法

package com.atguigu.spring5.service;
import com.atguigu.spring5.dao.UserDao;
public class UserService {
    private UserDao userDao;
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    public void add(){
        //1建立UserDao屬性,生成set方法
        System.out.println ("add...");
        userDao.update ();
    }
}

(3)在 spring 配置檔案中進行配置

<bean id="userService" class="com.atguigu.spring5.service.UserService">
    <property name="userDao" ref="userDaoImp"></property>
</bean>
    <bean id="userDaoImp" class="com.atguigu.spring5.dao.UserDaoImp"></bean>

(4)測試

 @Test
    public void test1(){
        //1載入Spring配置檔案
        ApplicationContext context = new ClassPathXmlApplicationContext ("bean3.xml");
        //2獲取配置建立的物件
        UserService bean = context.getBean ("userService", UserService.class);
        System.out.println (bean);
        bean.add ();
    }

(5)結果
在這裡插入圖片描述

3、注入屬性-內部 bean

(1)一對多關係:部門和員工
一個部門有多個員工,一個員工屬於一個部門,部門是一,員工是多

package com.atguigu.spring5;

public class Emp {
 private String ename;
 private String gender;
 //員工屬於某一個部門,使用物件形式表示
 private Dept dept;
 public void setDept(Dept dept) {
 this.dept = dept;
 }
 public void setEname(String ename) {
 this.ename = ename;
 }
 public void setGender(String gender) {
 this.gender = gender;
 } }
package com.atguigu.spring5;

public class Dept {
    private String dname;

    public void setDname(String dname) {
        this.dname = dname;
    }
}

(2)在實體類之間表示一對多關係,員工表示所屬部門,使用物件型別屬性進行表示
(3)在 spring 配置檔案中進行配置

<bean id="userDaoImp" class="com.atguigu.spring5.dao.UserDaoImp"></bean>
    <bean id="emp" class="com.atguigu.spring5.Emp">
        <property name="ename" value="dd"></property>
        <property name="gender" value=""></property>
        <property name="dept">
            <bean id="dept" class="com.atguigu.spring5.Dept">
                <property name="dname" value="保安部門"></property>
            </bean>
        </property>
    </bean>

(4)測試

  @Test
    public void test2(){
        //1載入Spring配置檔案
        ApplicationContext context = new ClassPathXmlApplicationContext ("bean3.xml");

        //2獲取配置建立的物件
        Emp emp = context.getBean ("emp", Emp.class);
        System.out.println (emp);
    }

(5)結果
在這裡插入圖片描述

4、xml 注入集合屬性

1、注入陣列型別屬性
2、注入 List 集合型別屬性
3、注入 Map 集合型別屬性
(1)建立類,定義陣列、list、map、set 型別屬性,生成對應 set 方法

package com.atguigu.spring5;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class Stu {
    //1 陣列型別屬性
    private String[] courses;
    //2 list 集合型別屬性
    private List<String> list;
    //3 map 集合型別屬性
    private Map<String, String> maps;
    //4 set 集合型別屬性
    private Set<String> sets;

    public void setSets(Set<String> sets) {
        this.sets = sets;
    }

    public void setCourses(String[] courses) {
        this.courses = courses;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    public void setMaps(Map<String, String> maps) {
        this.maps = maps;
    }
}

(2)在 spring 配置檔案進行配置

    <!--注入集合型別-->
    <bean id="stu" class="com.atguigu.spring5.Stu">
        <property name="courses">
            <array>
                <value>java</value>
                <value>php</value>
            </array>
        </property>
        <property name="list">
            <list>
                <value>張三</value>
                <value>王五</value>
            </list>
        </property>
        <property name="maps">
            <map>
                <entry key="java" value="JAVA"></entry>
                <entry key="K1" value="V1"></entry>
            </map>
        </property>
        <property name="sets">
            <set>
                <value>1</value>
                <value>2</value>
            </set>
        </property>
    </bean>

(3)測試

@Test
    public void test3(){
        //1載入Spring配置檔案
        ApplicationContext context = new ClassPathXmlApplicationContext ("bean3.xml");

        //2獲取配置建立的物件
        Stu stu = context.getBean ("stu", Stu.class);
        System.out.println (stu);
    }

(4)結果
在這裡插入圖片描述
4、在集合裡面設定物件型別值
(1)建立課程和課程List

package com.atguigu.spring5;

public class Course {
    private String cname;

    public void setCname(String cname) {
        this.cname = cname;
    }

    @Override
    public String toString() {
        return "Course{" +
                "cname='" + cname + '\'' +
                '}';
    }
}
package com.atguigu.spring5;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Stu {

    //1 陣列型別屬性
    private String[] courses;
    //2 list 集合型別屬性
    private List<String> list;
    //3 map 集合型別屬性
    private Map<String, String> maps;
    //4 set 集合型別屬性
    private Set<String> sets;

    //5學生學的多門課程
    private List<Course> courseList;

    @Override
    public String toString() {
        return "Stu{" +
                "courses=" + Arrays.toString (courses) +
                ", list=" + list +
                ", maps=" + maps +
                ", sets=" + sets +
                ", courseList=" + courseList +
                '}';
    }

    public void setCourseList(List<Course> courseList) {
        this.courseList = courseList;
    }

    public void setSets(Set<String> sets) {
        this.sets = sets;
    }

    public void setCourses(String[] courses) {
        this.courses = courses;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    public void setMaps(Map<String, String> maps) {
        this.maps = maps;
    }
}

(2)配置xml檔案

  <!--注入集合型別-->
    <bean id="stu" class="com.atguigu.spring5.Stu">
        <property name="courses">
            <array>
                <value>java</value>
                <value>php</value>
            </array>
        </property>
        <property name="list">
            <list>
                <value>張三</value>
                <value>王五</value>
            </list>
        </property>
        <property name="maps">
            <map>
                <entry key="java" value="JAVA"></entry>
                <entry key="K1" value="V1"></entry>
            </map>
        </property>
        <property name="sets">
            <set>
                <value>1</value>
                <value>2</value>
            </set>
        </property>
        <!--集合中的物件注入-->
        <property name="courseList">
            <list>
               <ref bean="c1"></ref>
                <ref bean="c2"></ref>
            </list>
        </property>
    </bean>
    <bean id="c1" class="com.atguigu.spring5.Course">
        <property name="cname" value="資料庫課程"></property>
    </bean>
    <bean id="c2" class="com.atguigu.spring5.Course">
        <property name="cname" value="java課程"></property>
    </bean>

(3)測試

@Test
    public void test3(){
        //1載入Spring配置檔案
        ApplicationContext context = new ClassPathXmlApplicationContext ("bean3.xml");

        //2獲取配置建立的物件
        Stu stu = context.getBean ("stu", Stu.class);
        System.out.println (stu);
    }

(4)結果
在這裡插入圖片描述
5、把集合注入部分提取出來
(1)在 spring 配置檔案中引入名稱空間 util

<?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:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                          http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!--util標籤抽取list集合型別的注入-->
    <util:list id="bookList">
        <value>易筋經</value>
        <value>九陰真經</value>
        <value>九陽神功</value>
    </util:list>
    <!--2 提取 list 集合型別屬性注入使用-->
    <bean id="b" class="com.atguigu.spring5.util.Books">
    <property name="list" ref="bookList"></property>
</bean>

(2) Books類,與前面的book不一樣。

package com.atguigu.spring5.util;

import java.util.List;

public class Books {
    private List<String> list;

    public void setList(List<String> list) {
        this.list = list;
    }
}

(3)測試

  @Test
    public void test4(){
        //1載入Spring配置檔案
        ApplicationContext context = new ClassPathXmlApplicationContext ("bean3.xml");

        //2獲取配置建立的物件
         Books b = context.getBean ("b", Books.class);
         System.out.println (b);
    }

(4)結果
在這裡插入圖片描述