gpt4 book ai didi

java - 在 Spring 中注入(inject) Value 始终为 null

转载 作者:行者123 更新时间:2023-11-30 03:49:21 25 4
gpt4 key购买 nike

我正在关注这个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 注入(inject)值

spring-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>

客户.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 + "]";
}

}

项目.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;
}
}

最终是我的主课

应用程序.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);
}
}

最佳答案

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

<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 没有被放置在类路径中。因此,当 Spring IOC 容器开始读取 spring-beans.xml 配置文件时,它不会解析表达式 #{itemBean} 并将表达式视为 String文字。这会导致问题,因为 customerBean bean 上的 item 属性需要 SpringEl.Item 类型,而不是 String.

修复 pom.xml 文件中的依赖关系会将 Spring EL 置于类路径中,并允许正确解析表达式,最终使用正确类型的 bean 来满足依赖关系。

关于java - 在 Spring 中注入(inject) Value 始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24815517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com