我有一个有多个屏幕的应用程序,每个屏幕都是通过一个按钮选择的。每个屏幕都包含相当重量级的组件,因此只有激活屏幕在内存中很重要-所有其他屏幕都应该可用于垃圾回收。该应用程序使用Spring作为胶水,目前它使用getBean()切换屏幕://eventhandlerforaspecificbuttonpublicvoidactionPerformed(Evente){setScreen((Screen)applicationContext.getBean("screen1"));}“screen1”是一个原型(prototype)bean,因此当按下按钮时会创建一个新的屏幕实例。此外,se
考虑这样一行代码AutomobileDriverad=(AutomobileDriver)appContext.getBean("increaseSpeed");假设有一个IncreaseSpeed类继承自AutomobileDriver类getBean有什么作用?这个的主要作用是什么? 最佳答案 用“简单的外行术语”:这假设Spring被告知创建一个对象实例(可能在XML配置文件中),该对象实例(由Spring标识为)id为“increaseSpeed”并且具有的类或父类汽车司机。您要求Spring上下文提供对(a)默认情况下先前
我需要通过类类型查找bean。当beans被代理包装时(一些方法是@Transactional)——ApplicatoinContext找不到它们。我发现如果我通过接口(interface)查找它们,它就可以工作,但在这种情况下,我使用的是具体类类型。我知道这个bean是我正在寻找的类型,但是getBean()方法失败了。我可以调试(并修复)Spring的AbstractBeanFactory代码中的问题。问题是它根据我请求的类型检查beanInstance的类型,但beanInstance.getClass()是一个代理。AbstractBeanFactory应该对此进行补偿并将类型
Java8的::允许仅通过方法名称引用方法。protectedObjectloadBeanController(Stringurl)throwsIOException{loader=newFXMLLoader(getClass().getResource(url));ApplicationContextcontext=MyProjectClass.getApplicationContext();loader.setControllerFactory(context::getBean);returnloader.getController();}但是,根据BeanFactoryInter
Java8的::允许仅通过方法名称引用方法。protectedObjectloadBeanController(Stringurl)throwsIOException{loader=newFXMLLoader(getClass().getResource(url));ApplicationContextcontext=MyProjectClass.getApplicationContext();loader.setControllerFactory(context::getBean);returnloader.getController();}但是,根据BeanFactoryInter
当我使用getBean("test")我有一门课@ComponentpublicclassTEST{}这个bean可以加载吗? 最佳答案 getBean()是区分大小写的,但是Spring对@Component和@Bean使用自定义bean命名策略>类(class)。见4.10.5Namingautodetectedcomponents:Whenacomponentisautodetectedaspartofthescanningprocess,itsbeannameisgeneratedbytheBeanNameGenerator
当我使用getBean("test")我有一门课@ComponentpublicclassTEST{}这个bean可以加载吗? 最佳答案 getBean()是区分大小写的,但是Spring对@Component和@Bean使用自定义bean命名策略>类(class)。见4.10.5Namingautodetectedcomponents:Whenacomponentisautodetectedaspartofthescanningprocess,itsbeannameisgeneratedbytheBeanNameGenerator
我正在使用方法ApplicationContext.getBean(Stringname,ClassrequiredType)。bean的类型为util:set。我的代码如下:SetmySet=context.getBean("myBean",Set.class);我想知道如何做这样的事情来避免类型转换警告:SetmySet=context.getBean("myBean",Set.class);我不确定是否可以以这种方式定义类的类型。我是在做梦还是有办法做到这一点?谢谢。 最佳答案 并非如此,但有一个运行时解决方法至少可以消除对@
我刚刚开始了解SpringIOC概念。我经常看到网上找到的大部分例子都是用代码来获取对象的。ApplicationContextappContext=newClassPathXmlApplicationContext("applicationContext.xml");Hellohello=(Hello)appContext.getBean("hello");作为这些问题的引用1和2在堆栈溢出中。我推断,没有必要在代码中使用appContext.getBean("hello"),这被认为是不好的做法。另外,不推荐了。在这里纠正我,如果我的推断是错误的。考虑到这一点,我对我的项目进行了相
我问了一个一般的Spring问题:Auto-castSpringBeans并且有多个人回应说应该尽可能避免调用Spring的ApplicationContext.getBean()。这是为什么?我还应该如何访问我配置Spring来创建的bean?我在非Web应用程序中使用Spring,并计划访问共享ApplicationContext对象asdescribedbyLiorH.修正我接受下面的答案,但这里是MartinFowler的另一种看法,他discussesthemeritsofDependencyInjectionvs.usingaServiceLocator(这本质上与调用包装