spring学习笔记p7-p12

IOC操作 Bean管理

1.什么是Bean管理

(0)Bean管理指的是两个操作
(1)Spring创建对象
(2)Spring注入属性

2.Bean管理操作有两种方式

(1)基于xml配置文件方式实现
(2)基于注解方式实现

IOC操作Bean管理(基于xml方式)

1.基于xml方式创建对象

<!--配置User类对象创建-->
    <bean id="user" class="com.atguigu.spring5.User"></bean>

(1)在spring配置文件中,使用bean标签,标签里面添加对应属性,就可以实现对象创建
(2)在bean标签有很多属性,介绍常用的属性

  • id属性:唯一标识
  • class属性:类全部路径(包类路径)

(3)创建对象时候,默认也是执行无参数构造方法完成对象创建

2.基于xml方式注入属性
(1)DI:依赖注入,就是注入属性
第一种注入方式:使用set方法进行注入

  • 1.创建类,定义属性和对应的set方法
public class Book {
    //创建属性
    private String bname;
    private String bauthor;
//set方法注入
    //创建属性对应的set方法
    public void setBname(String bname) {
        this.bname = bname;
    }

    public void setBauthor(String bauthor) {
        this.bauthor = bauthor;
    }
  • 2.在spring配置文件配置对象创建,配置属性注入
 <!--2 set方法注入属性-->
    <bean id="book" class="com.atguigu.spring5.Book">
    <!--使用property完成属性注入
    name:类里面属性名称
    value:向属性注入的值
    -->
    <property name="bname" value="易筋经"></property>
        <property name="bauthor" value="达摩老祖"></property>
    </bean>

第二种注入方式:使用有参数构造进行注入
(1)创建类,定义属性,创建属性对应的有参数构造方法。

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

    //有参数构造
    public Orders(String oname, String address) {
        this.oname = oname;
        this.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>

p名称空间注入(了解)

(1)使用p名称空间注入,可以简化基于xml配置方式
第一步:添加p名称空间在配置文件中

<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">

第二步 进行属性注入,在bean标签里面进行操作

    <!--2 set方法注入属性-->
    <bean id="book" class="com.atguigu.spring5.Book" p:bname="九阳神功" p:bauthor="无名氏">
    </bean>

IOC操作Bean管理(xml注入其他类型属性)

1.字面量
(1)null值

<!--null值-->
<property name="address">
<null/>
</property>

(2)属性值包含特殊符号

<!--属性值包含特殊符号
1<>进行转义
2 把带特殊符号内容写到CDATA
-->
<property name="address">
<value>
<![CDATA[<<南京>>]]></value>
</property>
</bean>

2.注入属性-外部bean
(1)创建两个类service类和dao类
(2)在service调用dao里面的方法
(3)在spring配置文件中进行配置

 private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void add(){
        System.out.println("service add.....");
        userDao.update();
<!--1 service和dao对象创建-->
    <bean id="userService" class="com.atguigu.spring5.service.UserService">
        <!--注入userDao对象
            name属性:类里面属性名称
            ref属性:创建userDao对象bean标签id值
            -->
        <property name="userDao" ref="userDaoImpl"></property>
    </bean>
    <bean id="userDaoImpl" class="com.atguigu.spring5.dao.UserDaoImpl"></bean>

3.注入属性-内部bean和级联赋值
(1)一对多关系:部门和员工
一个部门有多个员工,一个员工属于一个部门
部门是一,员工是多
(2)在实体类之间表示一对多关系

//部门类
public class Dept {
    private String dname;
    public void setDname(String dname) {
        this.dname = dname;
    }
}
//员工类
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;
    }
}

(3)在spring配置文件中进行配置

<!--内部bean-->
    <bean id="emp" class="com.atguigu.spring5.bean.Emp">
        <!--设置两个普通属性-->
        <property name="ename" value="lucy"></property>
        <property name="gender" value="女"></property>
        <!--对象类型-->
        <property name="dept" >
            <bean id="dept" class="com.atguigu.spring5.bean.Dept">
                <property name="dname" value="安保部"></property>
            </bean>
        </property>
    </bean>

4.注入属性-级联赋值
(1)第一种写法

 <!--级联赋值-->
    <bean id="emp" class="com.atguigu.spring5.bean.Emp">
        <!--设置两个普通属性-->
        <property name="ename" value="lucy"></property>
        <property name="gender" value="女"></property>
        <!--级联赋值-->
        <property name="dept" ref="dept"></property>
    </bean>
    <bean id="dept" class="com.atguigu.spring5.bean.Dept">
        <property name="dname" value="财务部"></property>
    </bean>

(2)第二种写法

 <!--级联赋值-->
    <bean id="emp" class="com.atguigu.spring5.bean.Emp">
        <!--设置两个普通属性-->
        <property name="ename" value="lucy"></property>
        <property name="gender" value="女"></property>
        <!--级联赋值-->
        <property name="dept" ref="dept"></property>
        <property name="dept.dname" value="技术部"></property>
    </bean>
    <bean id="dept" class="com.atguigu.spring5.bean.Dept">
        <property name="dname" value="财务部"></property>
    </bean>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值