在Spring中注入值总是为null

时间:2014-07-18 00:11:11

标签: java spring spring-el

我正在关注此tutorial,但在尝试运行应用程序时,它运行此错误堆栈

    Jul 18, 2014 3:45:23 AM org.springframework.context.support.FileSystemXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@1f2f0ce9: display name [org.springframework.context.support.FileSystemXmlApplicationContext@1f2f0ce9]; startup date [Fri Jul 18 03:45:23 EEST 2014]; root of context hierarchy
Jul 18, 2014 3:45:23 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from file [C:\Users\Yasha\workspace3\Test\spring-beans.xml]
Jul 18, 2014 3:45:23 AM org.springframework.context.support.FileSystemXmlApplicationContext obtainFreshBeanFactory
INFO: Bean factory for application context [org.springframework.context.support.FileSystemXmlApplicationContext@1f2f0ce9]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1ba22e94
Jul 18, 2014 3:45:23 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1ba22e94: defining beans [itemBean,customerBean]; root of factory hierarchy
Jul 18, 2014 3:45:24 AM org.springframework.beans.factory.support.DefaultListableBeanFactory destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1ba22e94: defining beans [itemBean,customerBean]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerBean' defined in file [C:\Users\Yasha\workspace3\Test\spring-beans.xml]: Cannot resolve reference to bean '#{itemBean}' while setting bean property 'item'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named '#{itemBean}' is defined
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:275)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:104)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1245)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
    at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:140)
    at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:84)
    at SpringEl.app.main(app.java:8)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named '#{itemBean}' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:387)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:971)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:246)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:269)
    ... 18 more

我在这里做错了什么?

我只是按照本教程的第一步,即通过XML注入值

弹簧-beans.xml文件:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="itemBean" class="SpringEl.Item">
        <property name="name" value="itemA" />
        <property name="qty" value="10" />
    </bean>

    <bean id="customerBean" class="SpringEl.Customer">
        <property name="item" value="#{itemBean}" />
        <property name="itemName" value="#{itemBean.name}" />
    </bean>

</beans>

Customer.java:

打包SpringEl;

public class Customer {

    private Item item;

    private String itemName;

    public Item getItem() {
        return item;
    }

    public void setItem(Item item) {
        this.item = item;
    }

    public String getItemName() {
        return itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    @Override
    public String toString() {
        return "Customer [item=" + item + ", itemName=" + itemName + "]";
    }

}

Item.java:

打包SpringEl;

public class Item {

    private String name;

    private int qty;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getQty() {
        return qty;
    }

    public void setQty(int qty) {
        this.qty = qty;
    }
}

最终是我的主要课程

app.java

   public class app {
        public static void main(String[] args) {
            ApplicationContext context = new FileSystemXmlApplicationContext("spring-beans.xml");


        Customer obj = (Customer) context.getBean("customerBean");
        System.out.println(obj);
    }
}

1 个答案:

答案 0 :(得分:2)

确保在spring-context文件中正确指定了pom.xml依赖项。这是一个例子:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.stack.overflow</groupId>
    <artifactId>example</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>example</name>
    <description>example</description>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.0.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.0.6.RELEASE</version>
        </dependency>
    </dependencies>
</project>

spring-context依赖项对spring-expression工件有自己的依赖关系,基本上是Spring EL。

我的预感是Spring EL没有放在classpath上。因此,当Spring IOC容器开始读取spring-beans.xml配置文件时,它不解析表达式#{itemBean},并将表达式视为字符串文字。这会导致问题,因为item bean上的customerBean属性需要SpringEl.Item类型而不是String

修复pom.xml文件中的依赖项会将Spring EL放在类路径上,并允许正确解析表达式,最终满足与正确类型的bean的依赖关系。