Spring5的学习笔记__02

外部bean与内部bean的注入属性

1、注入对象类型的属性时可以选择外部bean注入,或是内部bean的注入

1.1、外部bean注入对象属性:

service类:

package com.at.spring5.service;

import com.at.spring5.dao.UserDao;
import com.at.spring5.dao.UserDaoImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author tao
 * @version 1.0
 * @date 2020-06-24 15:02
 * @description  使用ref代替value注入属性值:为对象类型的属性注入
 */
public class UserService {

    private  UserDao userDao;

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

    @Test
    public void add(){
        System.out.println("service add..........");

        userDao.update();
    }
}

Dao类:



package com.at.spring5.dao;

/**
 * @author tao
 * @version 1.0
 * @date 2020-06-24 14:59
 * @description
 */
public interface  UserDao {
    public void update();
}

package com.at.spring5.dao;

/**
 * @author tao
 * @version 1.0
 * @date 2020-06-24 15:00
 * @description
 */
public class UserDaoImpl implements UserDao{
    @Override
    public void update() {
        System.out.println("UserDao...........");
    }
}

测试类:

@Test
    public void testbean2(){
        //使用xml配置文件方式----在xml中创建UserDao实现类的对象,并赋值给UserService类中的属性userDao

        //加载配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
        //获取配置创建的对象
        UserService userService = context.getBean("userService", UserService.class);
        userService.add();
    }

在这里插入图片描述

1.2、内部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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <!--使用内部bean的方式注入对象类型的属性-->
    <bean id="emp" class="com.at.spring5.beans.Employee">
        <property name="ename" value="Tom"></property>
        <property name="gender" value="男"></property>
        <property name="dname">
            <bean id="dep" class="com.at.spring5.beans.Department">
                <property name="dname" value="安保部"></property>
            </bean>
        </property>
    </bean>
</beans>
package com.at.spring5.beans;

/**
 * @author tao
 * @version 1.0
 * @date 2020-06-24 21:54
 * @description 部门类
 */
public class Department {
    private String dname;

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

    @Override
    public String toString() {
        return "Department{" +
                "dname='" + dname + '\'' +
                '}';
    }
}

package com.at.spring5.beans;

/**
 * @author tao
 * @version 1.0
 * @date 2020-06-24 21:53
 * @description 员工类
 */
public class Employee {
    private String ename;
    private String gender;
    private Department dname;
    public void setEname(String ename) {
        this.ename = ename;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

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

    public Department getDname() {
        return dname;
    }

    public void add() {
        System.out.println(ename+"::"+gender+"::"+dname);
    }
}

@Test
    public void testBean3(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
        Employee emp = context.getBean("emp", Employee.class);
        emp.add();
    }

在这里插入图片描述
2、级联赋值

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">


    <!--级联赋值-->
    <bean id="emp" class="com.at.spring5.beans.Employee">
        <property name="ename" value="Tom"></property>
        <property name="gender" value="男"></property>
        <property name="dname" ref="dept"></property>
        <!--技术部将覆盖财务部的值:级联赋值二-->
        <property name="dname.dname" value="技术部"></property>
    </bean>
    <bean id="dept" class="com.at.spring5.beans.Department">
        <property name="dname" value="财务部"></property>
    </bean>
</beans>

3、p 名称空间注入属性

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <!--使用p 名称空间注入属性-->
    <bean id="emp" class="com.at.spring5.beans.Employee" p:ename="lucky" p:gender="女" p:dname-ref="dept">
    </bean>
    <bean id="dept" class="com.at.spring5.beans.Department">
        <property name="dname" value="人事部"></property>
    </bean>

</beans>

4、特殊值的注入

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">


    <!--级联赋值-->
    <bean id="emp" class="com.at.spring5.beans.Employee">
        <property name="ename" value="Tom"></property>
        <property name="gender" value="男"></property>
        <property name="dname" ref="dept"></property>
        <!--技术部将覆盖财务部的值:级联赋值二-->
        <property name="dname.dname" >
            <!--特殊值的注入:带特殊符号内容写到 CDATA-->
            <value><![CDATA[<<技术部>>]]></value>
        </property>
    </bean>
    <bean id="dept" class="com.at.spring5.beans.Department">
        <property name="dname" value="财务部"></property>
    </bean>
</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值