IOC操作Bean管理XML方式

什么是Bean管理

Bean管理指的两个条件:

  1. spring创建对象
  2. spring注入属性

Bean管理操作有两种方式

基于xml配置文件方式实现

1、基于xml方式创建对象

  • 在Spring配置文件中,使用bean标签,标签里面添加对应属性,就可以实现对象创建
  • 在bean标签有很多属性:
    • id属性:唯一标识
    • class属性:包类路径
    • name:与id类似,用于早期框架,与id的区别是可以加特殊符号,较少使用
  • 创建对象时,默认执行无参构造方法
    <!--配置User对象创建-->
    <bean id="user" class="com.ln.spring5.User"></bean>

2、基于xml方式注入属性

DI:依赖注入,就是注入属性。(DI是IOC的具体表现,他就是依赖注入,他需要在对象创建基础之上进行完成)

第一种:set方法注入

public class Book {

    private  String bname;

    private String bauthor;

    public void setBname(String bname) {
        this.bname = bname;
    }

    public void setBauthor(String bauthor) {
        this.bauthor = bauthor;
    }
    @Override
    public String toString() {
        return "Book{" +
                "bname='" + bname + '\'' +
                ", bauthor='" + bauthor + '\'' +
                '}';
    }
}
    <!--set方法注入属性-->
    <bean id="book" class="com.ln.spring5.Book">
        <!--使用property完成属性注入
            name:类里面属性名称
            value:向属性注入的值
        -->
        <property name="bname" value="zs"></property>
        <property name="bauthor" value="李四"></property>
    </bean>

测试:

    @Test
    public void testBook1(){
        //1、加载spring配置文件
        ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
        //2、获取配置创建的对象
        Book book=context.getBean("book",Book.class);

        System.out.println("book = " + book);
    }

第二种:有参构造方法注入

public class Book {

    private  String bname;

    private String bauthor;

    public Book(String bname,String bauthor) {
        this.bname = bname;
        this.bauthor = bauthor;
    }
    
    @Override
    public String toString() {
        return "Book{" +
                "bname='" + bname + '\'' +
                ", bauthor='" + bauthor + '\'' +
                '}';
    }
}
    <!--有参构造方法注入属性-->
    <bean id="book" class="com.ln.spring5.Book">
        <constructor-arg name="bname" value="ls"/>
        <constructor-arg index="1" value="张三3"/>
    </bean>

p名称空间注入(简化基于xml配置方式)

底层还是用set方法

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

    <bean id="book" class="com.ln.spring5.Book" p:bname="ls" p:bauthor="张三三">
    </bean>
</beans>

 注入空值 <property name="bname"><null/></property>

注入特殊符号<property name="bname"><![CDATA[<<张三>>]]></property>

 注入属性-外部bean

  1. 创建两个类service类和dao类
  2. 在service调用dao里面的方法
  3. 在spring配置文件中进行配置
public interface UserDao {

    void update();

}

public class UserDaoImpl implements UserDao{
    @Override
    public void update() {
        System.out.println("daoimpl update.......");
    }
}

public class UserService {

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

    public void add(){
        System.out.println("service add....");
        userDao.update();
    }
}
    <!-- service和dao对象创建-->
    <bean id="userService" class="com.ln.spring5.service.UserService">
        <!--注入userDao对象
            name属性:类里面属性名称
            ref属性:创建userDao对象bean标签id值,将外部bean注入进来
         -->
        <property name="userDao" ref="userDaoImpl"></property>
    </bean>
    <bean id="userDaoImpl" class="com.ln.spring5.dao.UserDaoImpl"></bean>
    @Test
    public void testAdd(){
        ApplicationContext context=new ClassPathXmlApplicationContext("bean2.xml");
        UserService userService=context.getBean("userService",UserService.class);
        userService.add();
    }

注入属性-内部bean和级联赋值

    <bean id="emp" class="com.ln.spring5.bean.Emp">
        <property name="ename" value="lisi"></property>
        <property name="gender" value="1"></property>
        <property name="dept">
            <bean id="dept" class="com.ln.spring5.bean.Dept">
                <property name="dname" value="部门"></property>
            </bean>
        </property>
    </bean>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

过街的老鼠

感谢你对诗仙女的打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值