当前位置: 首页 > 后端技术 > Java

SpringAuthentication-InjectingInnerBeans

时间:2023-04-01 21:41:41 Java

如您所知,Java内部类定义在其他类的范围内,类似地,内部bean是定义在另一个bean范围内的bean。因此,元素内的元素称为内部bean,如下所示。示例让我们使用EclipseIDE并按照步骤创建aSpring应用-步骤说明1创建一个名为SpringExample的项目,并在创建的项目的src文件夹下创建一个包com.tutorialspoint。2使用添加外部JAR选项添加所需的Spring库,如SpringHelloWorld示例章节中所述。3在MainApp下创建Java类TextEdit、SpellCheck和com.tutorialspoint包。4在src文件夹下创建Beans配置文件Beans.xml。5最后一步是创建所有Java文件和bean配置文件的内容,并按如下所述运行应用程序。这是TextEditor.java文件内容包com.tutorialspoint;publicclassTextEditor{privateSpellCheckerspellChecker;//用于注入依赖项的setter方法。publicvoidsetSpellChecker(SpellCheckerspellChecker){System.out.println("InsidesetSpellChecker.");this.spellChecker=spellChecker;}//返回spellChecker的getter方法publicSpellChecker(){System.out.println("InsideSpellCheckerconstructor.");}publicvoidcheckSpelling(){System.out.println("InsidecheckSpelling.");}}例文帐名组合MainApp.java电影图像包com.tutorialspoint;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;publicclassMainApp{publicstaticvoidmain(String[]args){ApplicationContextcontext=newClassPathXmlApplicationContext("Beans.xml");TextEditorte=(TextEditor)context.getBean("textEditor");te.spellCheck();}}下面是配置文件Beans.xml,它具有基于setter的注入配置,但使用内部beans-创建完源文件和bean配置文件后,让我们运行应用程序如果您的应用程序一切正常,它将打印以下消息-InsideSpellCheckerconstructor。在setSpellChecker里面。里面检查拼写。