在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. 8.Python3标准库--数据持久存储与交换

    ''' 持久存储数据以便长期使用包括两个方面:在对象的内存中表示和存储格式之间来回转换数据,以及处理转换后数据的存储区. 标准库包含很多模块可以处理不同情况下的这两个方面 有两个模块可以将对象转换为一 ...

  2. Mybatis的初步使用

    MyBatis 是当下最流行的持久层框架,也是ORM框架,本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google ...

  3. lumen 框架的特殊使用

    1. 配置代码格式管理工具: composer require squizlabs/php_codesniffer, 使用 php artisan cs 检查代码格式: 2.单元测试用例编写 1./d ...

  4. Mathtype公式位置偏上

    Mathtype公式位置偏上 部分Mathtype公式与文档文字没有很好的对齐,而是浮起来了,也就是说Mathtype公式的位置比正常文字稍高,这是我写论文时碰到的一个很麻烦的问题.然后就是行距稍微大 ...

  5. js中startWith、endWith 函数不能在任何浏览器兼容的问题

    在做js测试的时候用到了startsWith函数,但是他并不是每个浏览器都有的,所以我们一般要重写一下这个函数,具体的用法可以稍微总结一下 在有些浏览器中他是undefined 所以我们可以这样的处理 ...

  6. C# 解决窗体闪烁

    C# 解决窗体闪烁 在Windows窗体上造成“闪烁”的窗体上有很多控制.造成这种闪烁的原因有两个: 1.当控件需要被绘制时,Windows发送一个控件两个消息.第一个(WM_ERASEBKGND)导 ...

  7. Codeforces 821C Okabe and Boxes(模拟)

    题目大意:给你编号为1-n的箱子,放的顺序不定,有n条add指令将箱子放入栈中,有n条remove指令将箱子移除栈,移出去的顺序是从1-n的,至少需要对箱子重新排序几次. 解题思路:可以通过把栈清空表 ...

  8. 三十分钟理解计算图上的微积分:Backpropagation,反向微分

    神经网络的训练算法,目前基本上是以Backpropagation (BP) 反向传播为主(加上一些变化),NN的训练是在1986年被提出,但实际上,BP 已经在不同领域中被重复发明了数十次了(参见 G ...

  9. 一道关于数据库(经典父子级 ID 关联)更新题,大家帮忙想想还有其它解决思路没有?

    昨天,一同事发过来的一道数据库题目,就是哪种经典的父子级 ID 在同一数据库表中设计类型.需要在原表中添加一个字段,同时,将该节点的父子级详细信息插入到原表新增的一字段中,具体效果如下图. AreaC ...

  10. [实战]MVC5+EF6+MySql企业网盘实战(19)——BJUI和ztree

    写在前面 上周在博客园看到一篇通用权限系统的文章,看到他那个UI不错,这里就研究了一下,将网盘的UI修改为他的那个,感兴趣的可以参考:http://b-jui.com/ 系列文章 [EF]vs15+e ...