gpt4 book ai didi

java - Spring - 使用声明性列表时出现 404

转载 作者:行者123 更新时间:2023-12-01 14:55:02 24 4
gpt4 key购买 nike

我试图通过 application-context.xml 中的声明在 Spring 中创建一个列表

      <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-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<context:component-scan base-package="com.example.check" />

<bean id="valueOptions">
<property name="valueList">
<list>
<value>Value A</value>
<value>Value B</value>
</list>
</property>

</bean>

当我尝试运行此代码时,我得到了


严重:将上下文初始化事件发送到类 org.springframework.web.context.ContextLoaderListener 的监听器实例时发生异常
java.lang.NullPointerException
在 java.util.concurrent.ConcurrentHashMap.get(来源未知)
atorg.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.defineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:186)

我在尝试运行该应用程序时收到 404 错误。关于我做错了什么有什么想法吗?

更新

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener java.lang.NullPointerException at java.util.concurrent.ConcurrentHashMap.get(Unknown Source) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:186) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:857) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:829) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:440) 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:220) 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:729) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:381) at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4791) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source) at java.util.concurrent.FutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)

我之所以说它与列表创建相关,是因为如果我删除 xhtml 文件中的列表和相应的用法,应用程序就可以正常工作。

最佳答案

您没有为 xml 文件中的 bean 元素提供 class 属性...

<bean id="valueOptions" class="your.package.ObjectName">
<property name="valueList">
<list>
<value>Value A</value>
<value>Value B</value>
</list>
</property>
</bean>

如果您的 bean 是 List 对象的实现,您可以执行如下操作:

<bean id="valueOptions" class="java.util.ArrayList">
<constructor-arg>
<list>
<value>Value A</value>
<value>Value B</value>
</list>
</constructor-arg>
</bean>

或者使用 util 命名空间:

<?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-2.5.xsd">


<util:list id="valueOptions" value-type="java.lang.String">
<value>Value A</value>
<value>Value B</value>
</util:list>

关于java - Spring - 使用声明性列表时出现 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14387397/

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