 
- Spring 核心基础知识
- 春天 - 主页
- 春天 - 概述
- 春天-建筑
- Spring - 环境设置
- Spring - Hello World 示例
- Spring - IoC 容器
- Spring - Bean 定义
- Spring - Bean 范围
- Spring - Bean 生命周期
- Spring - Bean 后处理器
- Spring-Bean定义继承
- Spring - 依赖注入
- Spring - 注入内部 Bean
- Spring - 注入集合
- Spring - Bean 自动装配
- 基于注释的配置
- Spring - 基于Java的配置
- Spring - Spring 中的事件处理
- Spring - Spring 中的自定义事件
- Spring - 使用 Spring 框架的 AOP
- Spring - JDBC 框架
- Spring-事务管理
- Spring - Web MVC 框架
- Spring - 使用 Log4J 进行日志记录
- 春季问答
- 春天 - 问题与解答
- 春季有用资源
- 春天 - 快速指南
- Spring - 有用的资源
- 春天 - 讨论
Spring - 注入内部 Bean
正如您所知,Java 内部类是在其他类的范围内定义的,类似地,内部 bean是在另一个 bean 的范围内定义的 bean。因此,<property/> 或 <constructor-arg/> 元素内的 <bean/> 元素称为内部 bean,如下所示。
<?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-3.0.xsd">
   <bean id = "outerBean" class = "...">
      <property name = "target">
         <bean id = "innerBean" class = "..."/>
      </property>
   </bean>
</beans>
 
例子
让我们准备好可用的 Eclipse IDE,并按照以下步骤创建 Spring 应用程序 -
| 脚步 | 描述 | 
|---|---|
| 1 | 创建一个名为SpringExample的项目,并在创建的项目中的src文件夹下创建一个包com.tutorialspoint 。 | 
| 2 | 使用“添加外部 JAR”选项添加所需的 Spring 库,如Spring Hello World 示例一章中所述。 | 
| 3 | 在com.tutorialspoint包下创建 Java 类TextEditor、SpellChecker和MainApp。 | 
| 4 | 在src文件夹下创建Beans配置文件Beans.xml。 | 
| 5 | 最后一步是创建所有 Java 文件和 Bean 配置文件的内容并运行应用程序,如下所述。 | 
这是TextEditor.java文件的内容-
package com.tutorialspoint;
public class TextEditor {
   private SpellChecker spellChecker;
   
   // a setter method to inject the dependency.
   public void setSpellChecker(SpellChecker spellChecker) {
      System.out.println("Inside setSpellChecker." );
      this.spellChecker = spellChecker;
   }
   
   // a getter method to return spellChecker
   public SpellChecker getSpellChecker() {
      return spellChecker;
   }
   public void spellCheck() {
      spellChecker.checkSpelling();
   }
}
以下是另一个依赖类文件SpellChecker.java的内容-
package com.tutorialspoint;
public class SpellChecker {
   public SpellChecker(){
      System.out.println("Inside SpellChecker constructor." );
   }
   public void checkSpelling(){
      System.out.println("Inside checkSpelling." );
   }
}
以下是MainApp.java文件的内容-
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
      TextEditor te = (TextEditor) context.getBean("textEditor");
      te.spellCheck();
   }
}
 
以下是配置文件Beans.xml,它具有基于 setter 的注入的配置,但使用内部 beans -
<?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-3.0.xsd">
   <!-- Definition for textEditor bean using inner bean -->
   <bean id = "textEditor" class = "com.tutorialspoint.TextEditor">
      <property name = "spellChecker">
         <bean id = "spellChecker" class = "com.tutorialspoint.SpellChecker"/>
      </property>
   </bean>
</beans>
 
创建完源文件和 bean 配置文件后,让我们运行该应用程序。如果您的应用程序一切正常,它将打印以下消息 -
Inside SpellChecker constructor. Inside setSpellChecker. Inside checkSpelling.