我有一个类有一个Daemon类型的对象列表.
class Xyz {
List<Daemon> daemons;
}
Run Code Online (Sandbox Code Playgroud)
我的弹簧配置看起来像这样.
<bean id="xyz" class="package1.Xyz">
<property name="daemons" ref="daemonsList">
</bean>
<bean id="daemon1" class="package1.DaemonImpl1"/>
<bean id="daemon2" class="package1.DaemonImpl2"/>
<bean id="daemonsList" class="java.util.ArrayList">
<constructor-arg>
<list>
<ref bean="daemon1" />
<ref bean="daemon2" />
</list>
</constructor-arg>
</bean>
Run Code Online (Sandbox Code Playgroud)
现在,不是在列表中显式连接每个守护进程实现,而是可以Daemon在列表中自动自动装配所有类型的bean .我试图解决的问题是,如果有人创建了一个新的Daemon类实现的bean 并忘记将其连接到列表中.
我在stackoverflow上的某个地方看到过这个问题,但是无法再找到它.为此道歉.
我在春天定义了一张地图:
<util:map id="AdditionalParams" scope="prototype" map-class="java.util.HashMap"
key-type="java.lang.String" value-type="java.lang.String">
<entry key="Start" value="12345" />
<entry key="Finish" value="12365" />
</util:map>
Run Code Online (Sandbox Code Playgroud)
然后我将这个bean自动装配到一个定义为:
private @Autowired @Qualifier(value = "AdditionalParams") Map<String, String> additionalParams;
Run Code Online (Sandbox Code Playgroud)
执行此操作时,会抛出一个异常,说:
引起:org.springframework.beans.factory.BeanCreationException:创建名为'DutyCreator'的bean时出错:注入自动连接的依赖项失败; 嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private java.util.Map DutyCreator.additionalParams; 嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:[java.lang.String中]找到的依赖性[地图值java.lang.String类型]无类型的匹配豆:预期至少1豆,其有资格作为候选自动装配对于这种依赖.依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true),@ org.springframework.beans.factory.annotation.Qualifier(value = AdditionalParams)}
引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有找到类型为[java.lang.String]的匹配bean为依赖[map with value type java.lang.String]:期望至少有一个bean有资格作为autowire候选者对于这种依赖.依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true),@ org.springframework.beans.factory.annotation.Qualifier(value = AdditionalParams)}
有任何想法吗?
干杯.
是否可以使用@Autowired列表?
就像我有mimetypes属性文件一样,在我的类文件中我有这样的东西
@Autowired
private List<String> mimeTypes = new ArrayList<String>();
Run Code Online (Sandbox Code Playgroud) 这是地图
@Autowired
private Map<String, ISendableConverter> converters;
Run Code Online (Sandbox Code Playgroud)
和 ISendableConverter
public interface ISendableConverter {
ISendableMsg convert(BaseMessage baseMessage);
String getType();
}
Run Code Online (Sandbox Code Playgroud)
有几个类实现 ISendableConverter
我想converters通过使用spring Autowried 将它们注入变量.
类的实例作为值,方法的结果@Autowried作为键.
像这个
@Component
public class SendableVoiceMsgConverter implements ISendableConverter {
@Override
public ISendableMsg convert(BaseMessage baseMessage) {
// TODO Auto-generated method stub
return null;
}
@Override
public String getType() {
return "VOICE";
}
}
Run Code Online (Sandbox Code Playgroud)
这可能吗?如何?
我有一个Spring地图问题让我流泪.
我的春天看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
">
<util:map id="mockMap">
<entry key="userTest1" value="test1"/>
<entry key="userTest2" value="test2"/>
<entry key="userTest3" value="test6"/>
</util:map>
</beans>
Run Code Online (Sandbox Code Playgroud)
然后我自动连接的代码如下(省略了相关部分):
@Autowired
@Resource(name="mockMap")
Map<String, String> testMap;
@Test
public void testGetGearListActivityOK() {
for (String key : testMap.keySet()) {
System.out.println("key = " + key);
}
}
Run Code Online (Sandbox Code Playgroud)
令人惊讶的是,这实际上会给我一个错误的自动装配步骤,说没有匹配类型字符串的bean.但是,如果我将单元测试中的地图更改为Map,那么我得到以下输出:
Run Code Online (Sandbox Code Playgroud)[junit] key = mockMap [junit] key = org.springframework.context.annotation.internalConfigurationAnnotationProcessor [junit] key = org.springframework.context.annotation.internalAutowiredAnnotationProcessor [junit] key = org.springframework.context.annotation.internalRequiredAnnotationProcessor [junit] key = org.springframework.context.annotation.internalCommonAnnotationProcessor [junit] key = systemProperties [junit] key = …
我有一个Spring bean,在Spring Bean中我依赖于其他bean的列表.我的问题是:如何将bean的通用列表注入该bean的依赖项?
例如,一些代码:
public interface Color { }
public class Red implements Color { }
public class Blue implements Color { }
Run Code Online (Sandbox Code Playgroud)
我的豆子:
public class Painter {
private List<Color> colors;
@Resource
public void setColors(List<Color> colors) {
this.colors = colors;
}
}
@Configuration
public class MyConfiguration {
@Bean
public Red red() {
return new Red();
}
@Bean
public Blue blue() {
return new Blue();
}
@Bean
public Painter painter() {
return new Painter();
}
}
Run Code Online (Sandbox Code Playgroud)
问题是; 如何获取Painter中的颜色列表?另外,在旁注:我应该让@Configuration返回接口类型,还是类?
谢谢您的帮助!