在Spring框架中,一个bean仅用于一个特定的属性,这是提醒其声明为一个内部bean。内部bean支持setter注入“property”和构造器注入"constructor-arg“。
下面来看看一个详细的例子,演示使用 Spring 内部 bean 。
package com.yiibai.common;

public class Customer
{
private Person person; public Customer(Person person) {
this.person = person;
} public void setPerson(Person person) {
this.person = person;
} @Override
public String toString() {
return "Customer [person=" + person + "]";
}
}
package com.yiibai.common;

public class Person
{
private String name;
private String address;
private int age; //getter and setter methods @Override
public String toString() {
return "Person [address=" + address + ",
age=" + age + ", name=" + name + "]";
}
}
很多时候,可以使用 'ref' 属性来引用“Person” bean到“Customer” Bean,person的属性如下:
<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.5.xsd"> <bean id="CustomerBean" class="com.yiibai.common.Customer">
<property name="person" ref="PersonBean" />
</bean> <bean id="PersonBean" class="com.yiibai.common.Person">
<property name="name" value="yiibai" />
<property name="address" value="address1" />
<property name="age" value="28" />
</bean> </beans>
在一般情况下,引用这样也没有问题,但由于“yiibai” persion bean 只用于Customer bean,这是更好地声明 “yiibai” person 作为一个内部 bean,如下:
<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.5.xsd"> <bean id="CustomerBean" class="com.yiibai.common.Customer">
<property name="person">
<bean class="com.yiibai.common.Person">
<property name="name" value="yiibai" />
<property name="address" value="address1" />
<property name="age" value="28" />
</bean>
</property>
</bean>
</beans>
内部 bean 也支持构造器注入如下:
<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.5.xsd"> <bean id="CustomerBean" class="com.yiibai.common.Customer">
<constructor-arg>
<bean class="com.yiibai.common.Person">
<property name="name" value="yiibai" />
<property name="address" value="address1" />
<property name="age" value="28" />
</bean>
</constructor-arg>
</bean>
</beans>
注意:

id 或 name 值在bean类是没有必要以一个内部 bean 呈现,它会简单地忽略Spring容器。

执行结果:

package com.yiibai.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class App
{
public static void main( String[] args )
{
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"}); Customer cust = (Customer)context.getBean("CustomerBean");
System.out.println(cust); }
}

输出结果:

Customer [person=Person [address=address1, age=28, name=yiibai]]

Spring内部bean实例的更多相关文章

  1. Spring内部bean无法通过id获取

    内部Bean注入正常,但是直接在context中getBean是得不到的: <?xml version="1.0" encoding="UTF-8"?&g ...

  2. Spring 自定义Bean 实例获取

    一.通过指定配置文件获取, 对于Web程序而言,我们启动spring容器是通过在web.xml文件中配置,这样相当于加载了两次spring容器 ApplicationContext ac = new ...

  3. Spring中Bean实例的生命周期及其行为

  4. spring获取bean 实例

    ApplicationContext ctx = new ClassPathXmlApplication("applicationContext.xml"); DataSource ...

  5. Spring三 Bean的三种创建方式

    创建Bean的三种方式在大多数情况下,Spring容器直接通过new关键字调用构造器来创建Bean实例,而class属性指定Bean实例的实现类,但这不是实例化Bean的唯一方法.实际上,Spring ...

  6. Spring工厂方式创建Bean实例

    创建Bean实例的方式: 1) 通过构造器(有参或无参) 方式: <bean id="" class=""/> 2) 通过静态工厂方法 方式: &l ...

  7. 【Spring实战】—— 6 内部Bean

    本篇文章讲解了Spring的通过内部Bean设置Bean的属性. 类似内部类,内部Bean与普通的Bean关联不同的是: 1 普通的Bean,在其他的Bean实例引用时,都引用同一个实例. 2 内部B ...

  8. 品Spring:真没想到,三十步才能完成一个bean实例的创建

    在容器启动快完成时,会把所有的单例bean进行实例化,也可以叫做预先实例化. 这样做的好处之一是,可以及早地发现问题,及早的抛出异常,及早地解决掉. 本文就来看下整个的实例化过程.其实还是比较繁琐的. ...

  9. 什么是 spring 的内部 bean?

    只有将 bean 用作另一个 bean 的属性时,才能将 bean 声明为内部 bean. 为了定义 bean,Spring 的基于 XML 的配置元数据在 <property> 或 &l ...

随机推荐

  1. Math类的数学计算功能

    //Math类的数学计算功能 public class MathTest { public static void main(String[] args) { /*----------下面是三角运算- ...

  2. POJ 2230 Watchcow(欧拉回路:输出点路径)

    题目链接:http://poj.org/problem?id=2230 题目大意:给你n个点m条边,Bessie希望能走过每条边两次,且两次的方向相反,让你输出以点的形式输出路径. 解题思路:其实就是 ...

  3. kNN算法笔记

    kNN算法笔记 标签(空格分隔): 机器学习 kNN是什么 kNN算法是k-NearestNeighbor算法,也就是k邻近算法.是监督学习的一种.所谓监督学习就是有训练数据,训练数据有label标好 ...

  4. MINIBASE源代码阅读笔记之buffer manager

    BufDesc frame 们的 descriptor(见BufHashTbl注释),包括 pageNo: 这个 frame 在文件里的id,page number prevframe: -1 表示此 ...

  5. js复制文字

    一.原理分析 浏览器提供了 copy 命令 ,可以复制选中的内容 document.execCommand("copy") 如果是输入框,可以通过 select() 方法,选中输入 ...

  6. markdown 测试代码

    这是 H1 这是 H2 这是 H3 这是 H4 这是 H5 这是 H6 A First Level Header A Second Level Header Now is the time for a ...

  7. Codeforces Round #361 (Div. 2) D - Friends and Subsequences

    题目大意:给你两个长度为n的数组a, b,问你有多少个问你有多少个区间满足 a中最大值等于b中最小值. 思路:我本来的想法是用单调栈求出每个点的管辖区间,然后问题就变成了巨麻烦的线段覆盖问题,就爆炸写 ...

  8. TestDirector自定义管理:工程配置

    一.工程配置 1.进入工程配置界面 2.点击“customize project entities”弹出自定义字段对话框.(这里有六个表,每个表中的字段分为系统字段(system fields)和用户 ...

  9. Java8所有的包介绍(由英文文档翻译而来)

    转载: Java8所有的包介绍(由英文文档翻译而来)

  10. CodeForces 785B Anton and Classes

    简单判断. 找第一类区间中$R$最大的,以及第二类区间中$L$最小的,判断距离. 找第二类区间中$R$最大的,以及第一类区间中$L$最小的,判断距离. 两种情况取个最大值即可. #include &l ...