当前位置: 代码迷 >> 综合 >> Spring-IOC-依赖注入-基于XML方式-外部bean、内部bean、级联赋值
  详细解决方案

Spring-IOC-依赖注入-基于XML方式-外部bean、内部bean、级联赋值

热度:46   发布时间:2024-03-06 01:12:43.0

Spring系列文章目录


Spring系列文章目录

前言

一、外部bean、内部bean是什么?

二、使用步骤

1.创建部门类

2.创建员工类

3.外部bean属性注入

4、内部bean属性注入

5、级联赋值

总结



前言

Spring-IOC-依赖注入-基于XML方式-外部bean、内部bean、级联赋值


提示:以下是以公司的部门、员工为例

一、外部bean、内部bean是什么?

外部bean:直接在beans标签内部直接定义的bean对象,外部bean可以被多个bean对象引用

内部bean:在某个bean标签的内部定义的bean对象,内部bean只能被某个对象的某个属性引用。

二、使用步骤

1.创建部门类

package com.orz.spring.bean;
/*** @author orz* @create 2020-10-18 15:08*/
public class Department {private Integer id;private String deptName;public Department() {}public Department(Integer id, String deptName) {this.id = id;this.deptName = deptName;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getDeptName() {return deptName;}public void setDeptName(String deptName) {this.deptName = deptName;}@Overridepublic String toString() {return "Department{" +"id=" + id +", deptName='" + deptName + '\'' +'}';}
}

2.创建员工类

package com.orz.spring.bean;
/*** @author orz* @create 2020-10-18 15:07*/
public class Employee {private String name;private String gender;private Department dept;public Employee() {}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public Department getDept() {return dept;}public void setDept(Department dept) {this.dept = dept;}@Overridepublic String toString() {return "Employee{" +"name='" + name + '\'' +", gender='" + gender + '\'' +", dept=" + dept +'}';}
}

3.外部bean属性注入

    <bean id="employee3" class="com.orz.spring.bean.Employee"><property name="name" value="梅梅"/><property name="gender" value="女"/><property name="dept" ref="department2"/></bean><bean id="department2" class="com.orz.spring.bean.Department"><property name="id" value="3"/><property name="deptName" value="行政部"/></bean>
@Testpublic void test3(){ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml");Employee employee = applicationContext.getBean("employee3", Employee.class);System.out.println(employee);}

 

Employee{name='梅梅', gender='女', dept=Department{id=3, deptName='行政部'}}

4、内部bean属性注入

    <bean id="employee2" class="com.orz.spring.bean.Employee"><property name="name" value="刘大爷"/><property name="gender" value="男"/><property name="dept"><bean class="com.orz.spring.bean.Department"><property name="id" value="2"/><property name="deptName" value="安全部"/></bean></property></bean>
@Testpublic void test2(){ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml");Employee employee = applicationContext.getBean("employee2", Employee.class);System.out.println(employee);}
Employee{name='刘大爷', gender='男', dept=Department{id=2, deptName='安全部'}}

5、级联赋值

    <bean id="employee" class="com.orz.spring.bean.Employee"><property name="name" value="李华"/><property name="gender" value="男"/><property name="dept" ref="department"/><property name="dept.id" value="1"/><property name="dept.deptName" value="技术部"/></bean><bean id="department" class="com.orz.spring.bean.Department"/>
@Testpublic void test1(){ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml");Employee employee = applicationContext.getBean("employee", Employee.class);System.out.println(employee);}
Employee{name='李华', gender='男', dept=Department{id=1, deptName='技术部'}}

总结

内部bean不需要写id 不能给外部访问,级联赋值需要开启getter方法

建议每个类都配上getter、setter方法

  相关解决方案