在本文中,我们将详细介绍如何在SpringXML配置中收集和注入给定类型的所有bean的各个方面,并为您提供关于spring基于xml注入bean的几种方式的相关解答,同时,我们也将为您带来关于jav
在本文中,我们将详细介绍如何在Spring XML配置中收集和注入给定类型的所有bean的各个方面,并为您提供关于spring基于xml注入bean的几种方式的相关解答,同时,我们也将为您带来关于java – Spring:如何在Spring配置中注入ENUM?、java – Spring:引用Foo类型的所有bean、java – 如何在Spring XML配置文件类中指定Map属性?、java – 我们可以覆盖另一个配置中一个xml配置中声明的spring bean吗?的有用知识。
本文目录一览:- 如何在Spring XML配置中收集和注入给定类型的所有bean(spring基于xml注入bean的几种方式)
- java – Spring:如何在Spring配置中注入ENUM?
- java – Spring:引用Foo类型的所有bean
- java – 如何在Spring XML配置文件类中指定Map属性?
- java – 我们可以覆盖另一个配置中一个xml配置中声明的spring bean吗?
如何在Spring XML配置中收集和注入给定类型的所有bean(spring基于xml注入bean的几种方式)
Spring框架最重要的特征之一就是依赖注入概念。我理解背后的建议之一是将一般的高级机制与低级细节分开(如Dependency Inversion Principle所宣布)。
从技术上讲,这可以归结为让Bean实现尽可能少地了解作为依赖项注入的Bean,例如
public class PrintOutBean { private LogicBean logicBean; public void action() { System.out.println(logicBean.humanReadableDetails()); } //...}<bean> <property name="loginBean" ref="ShoppingCartBean"/></bean>
但是,如果我想拥有一种在多个从属bean上运行的高级机制,该怎么办?
public class MenuManagementBean { private Collection<Option> options; public void printOut() { for (Option option:options) { // do something for option } //... } }
我知道一种解决方案是@Autowired
在单例bean中使用批注,即…
@Autowired private Collection<Option> options;
但这不违反分离原则吗?为什么我必须指定要在使用它们的相同位置(即MenuManagementBean
示例中的类)使用哪些依赖项?有没有办法像这样在XML配置中注入bean的集合(在MMB
类中没有任何注释)?
<bean> <property name="options"> <xxx:autowire by-type="MyOptionImpl"/> </property> </bean>
答案1
小编典典如果你想要一种无需使用@Autowired列表即可将给定类型的所有bean收集到一个集合中的方法,那么很容易编写一个自定义的方法FactoryBean
来为你做这件事:
public class BeanListFactoryBean<T> extends AbstractFactoryBean<Collection<T>> { private Class<T> beanType; private @Autowired ListableBeanFactory beanFactory; @Required public void setBeanType(Class<T> beanType) { this.beanType = beanType; } @Override protected Collection<T> createInstance() throws Exception { return beanFactory.getBeansOfType(beanType).values(); } @Override public Class<?> getObjectType() { return Collection.class; } }
然后
<bean> <property name="options"> <bean> <property name="beanType"/> </bean> </property> </bean>
但是,这一切似乎都需要花很多精力来避免@Autowired
进入你的原始课堂。根本就没有违反SoC的情况-没有编译时依赖性,也不知道SoC的options
来源。
答案2
小编典典在Spring 3.1中,可能的是:
public class PluginPrototypeTest extends ASpringWebTest { @Autowired Collection<IDummyRepo> repos; @Test public void cacheTest() { assertNotNull(repos); assertEquals(2, repos.size()); for(IDummyRepo r: repos){ System.out.println(r.getName()); } }}@Repositorypublic class DummyRepo implements IDummyRepo { @Override public String getName(){ return "DummyRepo"; }}@Repositorypublic class DummyRepo2 implements IDummyRepo { @Override public String getName(){ return "DummyRepo2"; }}
java – Spring:如何在Spring配置中注入ENUM?
package com.myorg.sparrow.s3Environment; import javax.annotation.Nonnull; public enum DocumentType { Document("document/",".xml.gz","binary/octet-stream","gzip",true); private final String path; private final String suffix; private final String contentType; private final String contentEncoding; private final Boolean compress; private DocumentType(@Nonnull final String path,@Nonnull final String suffix,@Nonnull final String contentType,@Nonnull final String contentEncoding,@Nonnull final Boolean compress) { this.path = path; this.suffix = suffix; this.contentType = contentType; this.contentEncoding = contentEncoding; this.compress = compress; } @Nonnull public String getPath() { return path; } @Nonnull public String getSuffix() { return suffix; } @Nonnull public String getContentType() { return contentType; } @Nonnull public String getContentEncoding() { return contentEncoding; } @Nonnull public Boolean isCompress() { return compress; } }
我想在Spring配置文件中注入DocumentType.Document的这个值
<bean id="s3Service"https://www.jb51.cc/tag/nes/" target="_blank">ness.xml.persist.S3Service"> <constructor-arg ref="awsCredentials" /> <constructor-arg value="**DocumentType.DOCUMENT**" /> // how do I inject it here? <constructor-arg value="${com.myorg.sparrow.s3EnvironmentConfiguration.S3EnvironmentConfigurator-destBucketName}" /> </bean>
我如何注入此值
<constructor-arg value="**DocumentType.DOCUMENT**" /> // how do I inject it here?
我是Spring框架的新手,不知道如何实现这一点
谢谢
解决方法
<bean id="s3Service"https://www.jb51.cc/tag/nes/" target="_blank">ness.xml.persist.S3Service"> <constructor-arg ref="awsCredentials" /> <constructor-arg value="Document" /> // We love Spring because it is simpler than we expect <constructor-arg value="${com.myorg.sparrow.s3EnvironmentConfiguration.S3EnvironmentConfigurator-destBucketName}" /> </bean>
java – Spring:引用Foo类型的所有bean
ClassPathResource res = new ClassPathResource("spring_foo.xml"); Xmlbeanfactory factory = new Xmlbeanfactory(res); Map<String,Foo> beans = factory.getBeansOfType(Foo.class);
我怎么能用XML做到这一点?
例如.就像是:
<bean id="fooHandler"> <property name="foos"> <map beanType="com.mycompany.Foo" / > </property> </bean>
或者更好的是列表而不是地图?
解决方法
java – 如何在Spring XML配置文件类中指定Map属性?
这就是我的意思,请参阅以下spring XML文件:
我的班级如下:
class A {
HashMap mapProperty
}
如何在spring XML文件中告诉要注入的Map是java.util.HashMap类型?或者一般来说我可以为地图提供课程名称吗?
请注意,我无法更改A类以使用Map而不是HashMap
提前致谢 !!
最佳答案
你可以使用util:map
不要忘记添加util命名空间.
java – 我们可以覆盖另一个配置中一个xml配置中声明的spring bean吗?
我是Spring的新手,所以这个问题可能有一个简单的答案,所以如果我问一些愚蠢的事情,请耐心等待.
我有一个全局弹簧配置文件,我想在许多应用程序中使用,我想在不同的应用程序中以不同的方式覆盖几个bean的一些属性.你能建议最好的方法吗?
例如,我在global.xml中有类似的东西.
现在,我想从不同的spring配置中引用testInstance,并在覆盖mem1后使用它.春天有可能吗?我需要使用自定义标签吗?
最佳答案
您可以在另一个xml配置中覆盖xml中定义的bean.但是如果你想利用@Configuration来做同样的话,那你就不走运了. Spring不允许这样做.
https://jira.springsource.org/browse/SPR-9567
我们今天的关于如何在Spring XML配置中收集和注入给定类型的所有bean和spring基于xml注入bean的几种方式的分享已经告一段落,感谢您的关注,如果您想了解更多关于java – Spring:如何在Spring配置中注入ENUM?、java – Spring:引用Foo类型的所有bean、java – 如何在Spring XML配置文件类中指定Map属性?、java – 我们可以覆盖另一个配置中一个xml配置中声明的spring bean吗?的相关信息,请在本站查询。
本文标签: