Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 不能注射豆子_Java_Spring_Javabeans - Fatal编程技术网

Java 不能注射豆子

Java 不能注射豆子,java,spring,javabeans,Java,Spring,Javabeans,我正在尝试使用Javabean进行注入。这是一个非常基本的实现,但是注入不起作用。你能指引我吗 test.java public class TempClass{ @Autowired HashMap<String,HashMap<String,String>> newMap = new HashMap<String,HashMap<String,String>>(); public void setNewMa

我正在尝试使用Javabean进行注入。这是一个非常基本的实现,但是注入不起作用。你能指引我吗

test.java

 public class TempClass{
@Autowired
        HashMap<String,HashMap<String,String>> newMap = new HashMap<String,HashMap<String,String>>();


        public void setNewMap(HashMap<String, HashMap<String,String>> newMap)
        {
            newMap= newMap;
        }

        public HashMap<String, HashMap<String,String>> getNewMap()
        {
            return newMap;
        }
    }
公共类TempClass{
@自动连线
HashMap newMap=新HashMap();
公共void setNewMap(HashMap newMap)
{
newMap=newMap;
}
公共HashMap getNewMap()
{
返回newMap;
}
}
此外: 对于我的bean config conn.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: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="xyz" map-class="java.util.HashMap">
        <entry key="x" value-ref="x" />
        <entry key="y" value-ref="y" />
        <entry key="z" value-ref="z" />
    </util:map>

    <util:map id="x" map-class="java.util.HashMap">
        <entry key="xx" value="xx" />
        <entry key="xy" value="xy" />
        <entry key="xz" value="xz" />
    </util:map>

    <util:map id="y" map-class="java.util.HashMap">
        <entry key="yx" value="yx" />
        <entry key="yy" value="yy" />
        <entry key="yz" value="yz" />
    </util:map>

    <util:map id="z" map-class="java.util.HashMap">
        <entry key="zx" value="zx" />
        <entry key="zy" value="zy" />
        <entry key="zz" value="zz" />
    </util:map>

 <bean id="bean123" class="reference.to.class" autowire="byName">
      <property name="newMap" ref="xyz" />
   </bean>

</beans>


有人能告诉我怎么回事吗?

自动连线注释应该在要注入的字段上方。并且该字段不应该只被赋值和声明

此外,注入贴图并不是最简单的例子。您应该编写一个更简单的测试用例来掌握它的窍门。比如说,

package test;
public class SpringInjectionTest {
    @Autowired
    private String injectThis;

    public void setInjectThis(String s) {
         injectThis = s;
    }

    public String getInjectThis() {
         return injectThis;
    }
}
下面是applicationContext:

<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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:annotation-config />   

    <bean id="testBean" class="test.SpringInjectionTest" autowire="byName"/>
</beans>

您有两种可能的修复方法:

a。将您的
newMap
声明为类型
Map
而不是具体的Hashmap,这是因为在内部
util:Map
返回对象类型
Map
,自动关联将无法找到合适的候选对象

b。从
newMap
中删除
@Autowired
,而是直接通过xml注入依赖项,方法如下:

<bean id="bean123" class="...">
   <property name="newMap" ref="xyz" />
</bean>


您的
someOtherBean
在同一个文件中声明在哪里。它基本上是一个hashmap,其中一个键引用另一个设置值的映射。我要求您显示它,但也显示初始化过程中出现的错误。您的bean引用
class=“link.to.that.class”
。你有吗?我有更新的代码。无错误,但newMap在程序启动后不包含任何值:(您所说的“该字段不应被分配,仅声明”是什么意思)在注入引用时分配引用。您不需要在类中分配引用(Java代码)它本身。第二部分起到了作用!它至少解决了被调用的bean!现在的问题是,我如何从第一个映射的值引用到另一个映射的键?