在Spring中所谓的内部bean(inner bean)是指一个bean 的<property/>或<constructor-arg/>元素中使用<bean/>元素定义的bean.内部bean 定义不需要有id或name属性,即使指定id或name属性值也将会被容器忽略.

在Spring中通过<list/>,<set/>, <map/>及<props/>元素可以定义和设置与Java Collection 类型对应List, Set ,Map 及Properties的值.

以下是个关于内部bean和集合的例子.

applicationContext.xml文件:
======================================================<?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-2.0.xsd">
<!--
  注:如果此处是Action的对象,则只能用name而不能用id
-->
<bean name="/login" class="action.LoginAction">
  <property name="loginBiz">
   <ref local="loginBiz" />
  </property>
</bean>
<bean ));
}
public String[] getHaha() {
  return haha;
}
public void setHaha(String[] haha) {
  this.haha = haha;
}
public ha getH() {
  return h;
}
public void setH(ha h) {
  this.h = h;
}
}
=================================
LoginImpl类中h属性所依赖的类
=================================
package biz;
import java.util.Map;
public class ha {
private String name;
private int age;
private Map map;
public int getAge() {
  return age;
}
public void setAge(int age) {
  this.age = age;
}
public String getName() {
  return name;
}
public void setName(String name) {
  this.name = name;
}
public Map getMap() {
  return map;
}
public void setMap(Map map) {
  this.map = map;
}
}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-02-20
  • 2022-12-23
  • 2021-10-20
  • 2021-09-30
  • 2021-11-21
  • 2021-10-08
猜你喜欢
  • 2021-11-29
  • 2021-05-31
  • 2021-06-11
  • 2021-09-11
  • 2022-12-23
  • 2021-07-02
  • 2021-12-28
相关资源
相似解决方案