Spring的Bean管理(基于XML)

注入集合类型的属性

注入集合类型:注入数组,LIst,Map集合类型属性。
<1.创建类,定义集合属性

public class Book {
    private Book [] book;
    private List<Book> list;
    private Map<Integer,Book> map;
    private Set<Book> set;

    public Book[] getBook() {
        return book;
    }

    public void setBook(Book[] book) {
        this.book = book;
    }

    public List<Book> getList() {
        return list;
    }

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

    public Map<Integer, Book> getMap() {
        return map;
    }

    public void setMap(Map<Integer, Book> map) {
        this.map = map;
    }

    public Set<Book> getSet() {
        return set;
    }

    public void setSet(Set<Book> set) {
        this.set = set;
    }

    @Override
    public String toString() {
        return "Book{" +
                "book=" + Arrays.toString(book) +
                ", list=" + list +
                ", map=" + map +
                ", set=" + set +
                '}';
    }
}

<2.在Spring配置文件中进行配置

<bean id="book" class="com.gz.pojo.Book">
        <!--Array数组注入-->
        <property name="book">
            <array>
                <value>Java课程</value>
                <value>Spring框架</value>
            </array>
        </property>
        <!--List类型注入-->
        <property name="list">
            <list>
                <value>Mybatis框架</value>
                <value>JDBC</value>
            </list>
        </property>
        <!--map属性注入-->
        <property name="map">
            <map>
                <entry key="1" value="如何找到女朋友"></entry>
                <entry key="2" value="如何找到男朋友"></entry>
            </map>
        </property>
        <!--Set属性注入-->
        <property name="set">
            <set>
                <value>MySql</value>
                <value>Redis</value>
            </set>
        </property>
    </bean>

< 3.把集合注入部分提取出来
在Spring配置文件中引入名称空间Util
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/beans/spring-util.xsd">

使用util标签完成List集合的注入提取

 <!--提取list集合类型属性注入-->
    <util:list id="list">
        <value>葵花宝典</value>
        <value>九阳神功</value>
    </util:list>
    <!--使用提取的list集合-->
    <bean id="book" class="com.gz.pojo.Book">
        <property name="list" ref="list"></property>
    </bean>

Bean的作用域

在创建Spring框架中,默认Bean单实例对象,但可以设置创建Bean实例是单实例还是多实例。通过bean标签中的scope标签设置属性值。

  • 默认值 Singleton,表示单例对象
  • prototype,表示多实例对象
<bean id="user" class="com.gz.pojo.User" scope="singletone">
        <property name="id" value="0"></property>
        <property name="name" value="gz"></property>
    </bean>
<bean id="user" class="com.gz.pojo.User" scope="prototype">
        <property name="id" value="0"></property>
        <property name="name" value="gz"></property>
    </bean>

bean的生命周期
普通的bean生命周期:

  • 通过构造器创建bean实例(无参构造器)
  • 为bean的属性设置值和对其他bean引用(调用set方法)
  • 调用bean的初始方法(需要进行配置初始化的方法)
  • 对象获取到
  • 当容器关闭时,调用备案的销毁方法(需要进行配置的销毁方法)

bean的后置处理器时,生命周期步骤需要增加两步。

  • 通过构造器创建bean实例(无参构造器)
  • 为bean的属性设置值和对其他bean引用(调用set方法)
  • 把bean实例传递到bean后置处理器的方法postProcessBeforeInitialization
  • 调用bean的初始方法(需要进行配置初始化的方法)
  • 把bean实例传递到bean后置处理器的方法postProcessAfterInitialization
  • 对象获取到
  • 当容器关闭时,调用备案的销毁方法(需要进行配置的销毁方法)

使用bean后置处理器需要实现BeanPostProcessor接口。

bean的自动装配

自动装配:根据指定装配规则(属性名称或者属性类型),Spring自动将匹配的属性值进行注入
使用标签autowire配置自动装配,autowrie有两个属性值

  • byName根据属性名称注入 注入值的bean的id和类属性名称一样
  • byType根据属性类型注入

byName注入:

<bean id="emp" class="com.atguigu.spring5.autowire.Emp" autowire="byName"> 
 <! -- <property name="dept" ref="dept"></property> -- > 
 </bean>
 <bean id="dept" class="com.atguigu.spring5.autowire.Dept"></bean> 

byTpye注入:

<bean id="emp" class="com.atguigu.spring5.autowire.Emp" autowire="bytype"> 
 <! -- <property name="dept" ref="dept"></property> -- > 
 </bean>
 <bean id="dept" class="com.atguigu.spring5.autowire.Dept"></bean> 

bean管理外部属性文件

引入外部属性文件配置数据库连接池
1.创建properties文件,配置数据库类型

driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/userDb
userName=root
password=123456

2.把外部properties文件引入Spring配置文件中
<1.引入context命名空间

 xmlns:context="http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

<2.配置Spring文件

<context:property-placeholder location="classpath:jdbc.properties"/> 
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> 
   <property name="driverClassName" value="${prop.driverClass}"></property>
   <property name="url" value="${prop.url}"></property> 
   <property name="username" value="${prop.userName}"></property> 
   <property name="password" value="${prop.password}"></property> 
</bean> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值