为了账号安全,请及时绑定邮箱和手机立即绑定

Spring 注入内部 Beans

标签:
Spring

注入内部 Beans

正如你所知道的 Java 内部类是在其他类的范围内被定义的,同理,inner beans 是在其他 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使用 Add External JARs 选项,添加所需的 Spring 库,解释见 Spring Hello World Example 章节。 option as explained in the chapter.
3在 com.tutorialspoint 包中创建Java类TextEditorSpellChecker 和 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();    } }

下面是使用内部 bean 为基于 setter 注入进行配置的配置文件 Beans.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-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.


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消