课程表

Spring 入门

Spring IoC 容器

Spring 依赖注入

Spring Beans 自动装配

Spring 基于注解的配置

Spring 框架

Spring 事务管理

Spring Web MVC 框架

工具箱
速查手册

Spring 注入内部 Beans

当前位置:免费教程 » Java相关 » Spring

正如你所知道的 Java 内部类是在其他类的范围内被定义的,同理,inner beans 是在其他 bean 的范围内定义的 bean。因此在 或 元素内 元素被称为内部bean,如下所示。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  6. <bean id="outerBean" class="...">
  7. <property name="target">
  8. <bean id="innerBean" class="..."/>
  9. </property>
  10. </bean>
  11. </beans>

例子

我们在适当的位置使用 Eclipse IDE,然后按照下面的步骤来创建一个 Spring 应用程序:

步骤描述
1创建一个名称为 SpringExample 的项目,并且在创建项目的 src 文件夹中创建一个包 com.tutorialspoint
2使用 Add External JARs 选项,添加所需的 Spring 库,解释见 Spring Hello World Example 章节。 option as explained in the chapter.
3com.tutorialspoint 包中创建Java类TextEditorSpellCheckerMainApp
4src 文件夹中创建 Beans 配置文件 Beans.xml
5最后一步是创建的所有Java文件和Bean配置文件的内容,并运行应用程序,解释如下所示。

这里是 TextEditor.java 文件的内容:

  1. package com.tutorialspoint;
  2. public class TextEditor {
  3. private SpellChecker spellChecker;
  4. // a setter method to inject the dependency.
  5. public void setSpellChecker(SpellChecker spellChecker) {
  6. System.out.println("Inside setSpellChecker." );
  7. this.spellChecker = spellChecker;
  8. }
  9. // a getter method to return spellChecker
  10. public SpellChecker getSpellChecker() {
  11. return spellChecker;
  12. }
  13. public void spellCheck() {
  14. spellChecker.checkSpelling();
  15. }
  16. }

下面是另一个依赖的类文件 SpellChecker.java 内容:

  1. package com.tutorialspoint;
  2. public class SpellChecker {
  3. public SpellChecker(){
  4. System.out.println("Inside SpellChecker constructor." );
  5. }
  6. public void checkSpelling(){
  7. System.out.println("Inside checkSpelling." );
  8. }
  9. }

下面是 MainApp.java 文件的内容:

  1. package com.tutorialspoint;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class MainApp {
  5. public static void main(String[] args) {
  6. ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
  7. TextEditor te = (TextEditor) context.getBean("textEditor");
  8. te.spellCheck();
  9. }
  10. }

下面是使用内部 bean 为基于 setter 注入进行配置的配置文件 Beans.xml 文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  6. <!-- Definition for textEditor bean using inner bean -->
  7. <bean id="textEditor" class="com.tutorialspoint.TextEditor">
  8. <property name="spellChecker">
  9. <bean id="spellChecker" class="com.tutorialspoint.SpellChecker"/>
  10. </property>
  11. </bean>
  12. </beans>

一旦你创建源代码和 bean 配置文件完成后,我们就可以运行该应用程序。如果你的应用程序一切都正常,将输出以下信息:

  1. Inside SpellChecker constructor.
  2. Inside setSpellChecker.
  3. Inside checkSpelling.
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号