簡體   English   中英

春季4,@ Resource注釋字段為null

[英]Spring 4, @Resource annotated field is null

我有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"));
    }
}

春季配置(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並向其移動了注釋,則我沒有得到任何更改(地圖字段也為null):

private Map<String, String> map;

@Resource(name = "map")
public void setMap(Map<String, String> map) {
    this.map = map;
}

只要文件位於給定的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]
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM