草庐IT

Bean-IOC

全部标签

java - 如果 Spring 中没有 bean,是否有一种简单的方法来 Autowiring 空集合?

如果我有@AutowiredListbeans;没有SomeBeanClass的bean,我得到:Nomatchingbeanoftype[SomeBeanClass]foundfordependency[collectionofSomeBeanClass]:expectedatleast1beanwhichqualifiesasautowirecandidateforthisdependency.Dependencyannotations:{@org.springframework.beans.factory.annotation.Autowired(required=true)}如

java - spring:通过读取另一个 bean 的属性来设置一个 bean 的属性?

是否可以通过读取另一个bean的属性来设置一个bean的属性?例如,假设我有:classA{voidsetList(Listlist);}classB{ListgetList();}我希望Spring实例化这两个类,并调用A的setList方法,传入调用B的getList方法的结果。Spring配置可能类似于:唉,这个虚构的XML不起作用。为什么不直接将B注入(inject)A呢?因为我不想引入额外的依赖。A只是依赖列表,而不是B。 最佳答案 如果您使用的是spring3.0,除了@Kevin的回答之外,还可以使用新的spring表

java - spring:通过读取另一个 bean 的属性来设置一个 bean 的属性?

是否可以通过读取另一个bean的属性来设置一个bean的属性?例如,假设我有:classA{voidsetList(Listlist);}classB{ListgetList();}我希望Spring实例化这两个类,并调用A的setList方法,传入调用B的getList方法的结果。Spring配置可能类似于:唉,这个虚构的XML不起作用。为什么不直接将B注入(inject)A呢?因为我不想引入额外的依赖。A只是依赖列表,而不是B。 最佳答案 如果您使用的是spring3.0,除了@Kevin的回答之外,还可以使用新的spring表

Spring拦截器的Java配置,其中拦截器使用 Autowiring 的spring bean

我想将springmvc拦截器添加为Java配置的一部分。我已经有一个基于xml的配置,但我正在尝试迁移到Java配置。对于拦截器,我知道可以从spring文档中这样完成-@EnableWebMvc@ConfigurationpublicclassWebConfigextendsWebMvcConfigurerAdapter{@OverridepublicvoidaddInterceptors(InterceptorRegistryregistry){registry.addInterceptor(newLocaleInterceptor());}}但是我的拦截器正在使用一个Autow

Spring拦截器的Java配置,其中拦截器使用 Autowiring 的spring bean

我想将springmvc拦截器添加为Java配置的一部分。我已经有一个基于xml的配置,但我正在尝试迁移到Java配置。对于拦截器,我知道可以从spring文档中这样完成-@EnableWebMvc@ConfigurationpublicclassWebConfigextendsWebMvcConfigurerAdapter{@OverridepublicvoidaddInterceptors(InterceptorRegistryregistry){registry.addInterceptor(newLocaleInterceptor());}}但是我的拦截器正在使用一个Autow

spring - 如何使用自定义注释 @Foo 找到所有 bean?

我有这个Spring配置:@Lazy@ConfigurationpublicclassMyAppConfig{@Foo@BeanpublicIFooServiceservice1(){returnnewSpecialFooServiceImpl();}}如何获得所有带有@Foo注释的bean的列表?注意:@Foo是我定义的自定义注解。它不是“官方”的Spring注释之一。[编辑]按照AvinashT.的建议,我编写了这个测试用例:importstaticorg.junit.Assert.*;importjava.lang.annotation.ElementType;importjav

spring - 如何使用自定义注释 @Foo 找到所有 bean?

我有这个Spring配置:@Lazy@ConfigurationpublicclassMyAppConfig{@Foo@BeanpublicIFooServiceservice1(){returnnewSpecialFooServiceImpl();}}如何获得所有带有@Foo注释的bean的列表?注意:@Foo是我定义的自定义注解。它不是“官方”的Spring注释之一。[编辑]按照AvinashT.的建议,我编写了这个测试用例:importstaticorg.junit.Assert.*;importjava.lang.annotation.ElementType;importjav

spring - 在 Spring 中何时使用 <ref bean> 以及何时使用 <ref local>?

何时使用以及何时使用Spring? 最佳答案 Specifyingthetargetbeanbyusingthebeanattributeofthereftagisthemostgeneralform,andwillallowcreatingareferencetoanybeaninthesameBeanFactory/ApplicationContext(whetherornotinthesameXMLfile),orparentBeanFactory/ApplicationContext.Thevalueofthebeanatt

spring - 在 Spring 中何时使用 <ref bean> 以及何时使用 <ref local>?

何时使用以及何时使用Spring? 最佳答案 Specifyingthetargetbeanbyusingthebeanattributeofthereftagisthemostgeneralform,andwillallowcreatingareferencetoanybeaninthesameBeanFactory/ApplicationContext(whetherornotinthesameXMLfile),orparentBeanFactory/ApplicationContext.Thevalueofthebeanatt

java - 使用 Java 8 构造时创建 Bean 时出现 ArrayOutOfBoundsException

当我使用Java8功能时,我在服务启动(创建Bean)时收到ArrayIndexOutOfBoundsException。Java8已经设置好并且一直在工作。代码编译正确。在服务启动时,服务无法监听端口,因为没有创建bean。当我更改代码(删除java8构造)时,服务启动并且一切正常。这是我正在使用的代码(服务启动的工作代码):for(ItemitemObject:response){if(itemObject.hasId()){idList.add(String.valueOf(itemObject.Id());}}使用Java8构造的相同代码:response.parallelSt