我的课程AnnotatedCollectionInjection
带有@Resource
个带注释的字段:
package LearnBook.part0.s9;
import LearnBook.Common;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
@Service("injectedAnnotatedCollection")
public class AnnotatedCollectionInjection {
@Resource(name = "map")
private Map<String, String> map;
@Resource(name = "props")
private Properties props;
@Resource(name = "set")
private Set<Integer> set;
@Resource(name = "list")
private List<Long> list;
@Override
public String toString() {
return "CollectionInjection{\n" +
"map=" + map +
", \nprops=" + props +
", \nset=" + set +
", \nlist=" + list +
"\n}";
}
public static void main(String ...args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"META-INF/spring/learnbook/part0/s9/a1.xml"
);
System.out.println(ctx.getBean("injectedAnnotatedCollection"));
}
}
Spring配置(a1.xml)在这里:
<?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:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<context:annotation-config/>
<context:component-scan base-package="LearnBook.part0.s9"/>
<util:map id="map" map-class="java.util.HashMap" key-type="java.lang.String" value-type="java.lang.String">
<entry key="someValue" value="is value"/>
<entry key-ref="someKeyValue" value-ref="someStringValue"/>
</util:map>
<util:properties id="props">
<prop key="one">two</prop>
<prop key="DA">DO</prop>
</util:properties>
<util:list id="list" value-type="java.lang.Long">
<ref bean="someLongValue"/>
<value>2000</value>
</util:list>
<util:set id="set" value-type="java.lang.Integer">
<ref bean="someIntValue"/>
<value>200</value>
</util:set>
<bean id="someStringValue"
class="java.lang.String"
c:_0="Some string value"/>
<bean id="someKeyValue"
class="java.lang.String"
c:_0="Some key value"/>
<bean id="someLongValue"
class="java.lang.Long"
c:_0="25"/>
<bean id="someIntValue"
class="java.lang.Integer"
c:_0="7"/>
</beans>
但是字段为空,为什么?:
CollectionInjection{
map=null,
props=null,
set=null,
list=null
}
如果我创建了setter并将注释移动到它,我没有得到更改(map字段也为null):
private Map<String, String> map;
@Resource(name = "map")
public void setMap(Map<String, String> map) {
this.map = map;
}
答案 0 :(得分:0)
只要文件出现在给定META-INF/spring/learnbook/part0/s9/a1.xml
的类路径中,您的配置就会很好。
通过将包名称降低为learnBook.part0.s9
,<context:component-scan base-package="learnBook.part0.s9"/>
和FileSystemXmlApplicationContext
而不是ClassPathXmlApplicationContext
public static void main(String ...args) {
ApplicationContext ctx = new FileSystemXmlApplicationContext(
"C://fullyQualifiedPathTo//a1.xml"
);
System.out.println(ctx.getBean("injectedAnnotatedCollection"));
}
成功收到输出。
CollectionInjection{
map={someValue=is value, Some key value=Some string value},
props={one=two, DA=DO},
set=[7, 200],
list=[25, 2000]
}