Spring 中的内部bean 和集合

在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 id="loginBiz" class="biz.LoginImpl">
  <property name="haha">
   <!--
    对应的haha在biz.LoginImpl类中为数组(注:如果属性为List则在applicationContext.xml文件中也应用list节点)
   -->
   <list>
    <value>aa</value>
    <value>bb</value>
    <value>cc</value>
    <value>dd</value>
   </list>
  </property>
  <property name="h">
   <!--
    此处为内部bean,即h的类型为biz.ha类型的引用
   -->
   <bean class="biz.ha">
    <!--
     biz.ha类中有三个属性,分别为name(String类型),age(int类型)和map(Map类型)
    -->
    <property name="name" value="李四" />
    <property name="age" value="55" />
    <property name="map">
     <!--
      map在biz.ha类中我Map集合
     -->
     <map>
      <!--
       每一个entry为一个Map元素
      -->
      <entry>
       <key>
        <value>hehe</value>
       </key>
       <value>hei</value>
      </entry>
     </map>
    </property>
   </bean>
  </property>
  <!--
   <constructor-arg type="java.lang.String">
   <value>张三</value>
   </constructor-arg>
  -->
  <constructor-arg index="0">
   <value>张三</value>
  </constructor-arg>
  <!--
   <constructor-arg type="int">
   <value>55</value>
   </constructor-arg>
  -->
  <constructor-arg index="1">
   <value>55</value>
  </constructor-arg>
</bean>
</beans>

=================================================

LoginAction类中的代码
=================================================

/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import biz.ILoginBiz;
import form.LoginForm;
public class LoginAction extends Action {

private ILoginBiz loginBiz;

public ILoginBiz getLoginBiz() {
  return loginBiz;
}
public void setLoginBiz(ILoginBiz loginBiz) {
  this.loginBiz = loginBiz;
}
public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
  System.out.println("haha 我己在action中了哈...");
  loginBiz.login();
  return null;
}
}

======================================

业务层(biz)中的ILoginBiz接口
======================================

package biz;
public interface ILoginBiz {
public void login();
}

======================================

业务层(biz)中实同ILoginBiz接口的LoginImpl类
======================================
package biz;
public class LoginImpl implements ILoginBiz {
public LoginImpl() {
}
private ha h;/依赖下一个类呐....
public LoginImpl(String name, int age) {
  System.out.println("我的名称:" + name + "我的年龄:" + age);
}
private String haha[];
public void login() {
  System.out.println("haha me is 在业务层中呐。。。快来找我呐。。。哈哈。。。");
  System.out.println("我的数组中有" + haha.length + "个数....哈哈。。。");
  System.out.println("我是内部bean呐..不错!!!"+"我的名字叫:"+h.getName()+"嘿嘿...不好意思哈....");
  System.out.println("嘿嘿...我是Map呐..你好么?嘿嘿.."+h.getMap().get("hehe"));
}
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;
}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值