Java 自动连线依赖项的spring注入失败

Java 自动连线依赖项的spring注入失败,java,spring,java-8,spring-rest,Java,Spring,Java 8,Spring Rest,我一直在Java 7(确切版本:jdk1.7.0_60)上运行我的项目,但由于公司范围内的政策更改为升级到Java 8(版本:jdk1.8.0_73),我已将本地机器的JVM升级到jdk1.8.0_73,并在此版本上配置了我的项目 以下是我的项目的事实: Spring框架版本:4.0.3.发布 Java:jdk1.8.0_73 从那时起,我的应用程序引发以下异常: Feb 16, 2016 12:15:14 PM org.apache.catalina.core.StandardContext

我一直在Java 7(确切版本:jdk1.7.0_60)上运行我的项目,但由于公司范围内的政策更改为升级到Java 8(版本:jdk1.8.0_73),我已将本地机器的JVM升级到jdk1.8.0_73,并在此版本上配置了我的项目

以下是我的项目的事实:
Spring框架版本:4.0.3.发布
Java:jdk1.8.0_73

从那时起,我的应用程序引发以下异常:

Feb 16, 2016 12:15:14 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.handler.MappedInterceptor#1': Cannot create inner bean 'com.asi.core.spring.interceptor.ThrottleRequestInterceptor#0' of type [com.asi.core.spring.interceptor.ThrottleRequestInterceptor] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.asi.core.spring.interceptor.ThrottleRequestInterceptor#0': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.asi.v1.ext.api.service.impl.ThrottleService com.asi.core.spring.interceptor.ThrottleRequestInterceptor.throttlerV1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'throttlerV1': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.web.client.RestTemplate com.asi.v1.ext.api.service.impl.ThrottleService.restTemplate; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restTemplate' defined in class path resource [spring/application-config.xml]: Cannot create inner bean 'org.springframework.http.converter.json.MappingJackson2HttpMessageConverter#4dd016c7' of type [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter] while setting bean property 'messageConverters' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.http.converter.json.MappingJackson2HttpMessageConverter#4dd016c7' defined in class path resource [spring/application-config.xml]: Cannot create inner bean 'org.springframework.http.MediaType#3ffc5996' of type [org.springframework.http.MediaType] while setting bean property 'supportedMediaTypes' with key [0]; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.ExpressionInvocationTargetException: A problem occurred when trying to execute method 'forName' on object of type [java.lang.Class]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4797)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5291)
    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.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.handler.MappedInterceptor#1': Cannot create inner bean 'com.asi.core.spring.interceptor.ThrottleRequestInterceptor#0' of type [com.asi.core.spring.interceptor.ThrottleRequestInterceptor] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.asi.core.spring.interceptor.ThrottleRequestInterceptor#0': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.asi.v1.ext.api.service.impl.ThrottleService com.asi.core.spring.interceptor.ThrottleRequestInterceptor.throttlerV1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'throttlerV1': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.web.client.RestTemplate com.asi.v1.ext.api.service.impl.ThrottleService.restTemplate; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restTemplate' defined in class path resource [spring/application-config.xml]: Cannot create inner bean 'org.springframework.http.converter.json.MappingJackson2HttpMessageConverter#4dd016c7' of type [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter] while setting bean property 'messageConverters' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.http.converter.json.MappingJackson2HttpMessageConverter#4dd016c7' defined in class path resource [spring/application-config.xml]: Cannot create inner bean 'org.springframework.http.MediaType#3ffc5996' of type [org.springframework.http.MediaType] while setting bean property 'supportedMediaTypes' with key [0]; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.ExpressionInvocationTargetException: A problem occurred when trying to execute method 'forName' on object of type [java.lang.Class]
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:290)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:122)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:632)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:140)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1114)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1017)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:470)
    at org.springframework.context.support.AbstractApplicationContext.getBeansOfType(AbstractApplicationContext.java:1071)
    at org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors(BeanFactoryUtils.java:277)
    at org.springframework.web.servlet.handler.AbstractHandlerMapping.detectMappedInterceptors(AbstractHandlerMapping.java:221)
    at org.springframework.web.servlet.handler.AbstractHandlerMapping.initApplicationContext(AbstractHandlerMapping.java:196)
    at org.springframework.context.support.ApplicationObjectSupport.initApplicationContext(ApplicationObjectSupport.java:120)
    at org.springframework.web.context.support.WebApplicationObjectSupport.initApplicationContext(WebApplicationObjectSupport.java:73)
    at org.springframework.context.support.ApplicationObjectSupport.setApplicationContext(ApplicationObjectSupport.java:74)
    at org.springframework.context.support.ApplicationContextAwareProcessor.invokeAwareInterfaces(ApplicationContextAwareProcessor.java:119)
    at org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization(ApplicationContextAwareProcessor.java:94)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:407)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    ... 20 more
以下是我的spring配置:

<?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:p="http://www.springframework.org/schema/p"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:web-services="http://www.springframework.org/schema/web-services"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
        http://www.springframework.org/schema/web-services http://www.springframework.org/schema/web-services/web-services.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

        <mvc:annotation-driven/>
        <web-services:annotation-driven/>
        <web-services:dynamic-wsdl id="productSearch" portTypeName="ProductSearch" 
        targetNamespace="http://www.asicentral.com/schema/product" locationUri="/productSearchService">
            <web-services:xsd location="/WEB-INF/product.xsd"/>
        </web-services:dynamic-wsdl>

        <web-services:dynamic-wsdl id="loginAPI" portTypeName="LoginAPI" 
        targetNamespace="http://www.asicentral.com/schema/login" locationUri="/loginAPI">
            <web-services:xsd location="/WEB-INF/login.xsd"/>
        </web-services:dynamic-wsdl>

        <web-services:dynamic-wsdl id="batchAPI" portTypeName="BatchAPI" 
        targetNamespace="http://www.asicentral.com/schema/batch" locationUri="/batchAPI">
            <web-services:xsd location="/WEB-INF/batch.xsd"/>
        </web-services:dynamic-wsdl>

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

        <mvc:interceptors>
            <bean class="com.asi.core.spring.interceptor.ThrottleRequestInterceptor" />
        </mvc:interceptors>

        <cache:annotation-driven />

        <import resource="v1.xml"/>
        <!-- When more versions are listed load version specific xml config -->
        <import resource="v2.xml"/>
        <import resource="v3.xml"/>

        <bean id="headerHandler" class="com.asi.v1.service.resource.util.ExternalApiHeaderHandler" />
        <bean id="ExternalApiApplicationContextProvider" class="com.asi.core.spring.config.provider.ExternalApiApplicationContextProvider"></bean>
        <bean id="environmentConfigurator" class="com.asi.ext.api.config.EnvironmentConfigurator">
            <property name="env" value="${Ext.env}"></property>
            <property name="lookupRestTemplate" ref="restTemplate"></property>
        </bean>


        <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:config-location="classpath:ehcache.xml"/> 
        <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache"/>   

        <context:property-placeholder  location="classpath:${Ext.env}-environment.properties, classpath:velocity-import*.properties, classpath:radar-version.properties" order="1"/>

             <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
                <property name="mediaTypes">
                    <map>
                        <entry key="xml" value="application/xml"/>
                        <entry key="html" value="text/html"/>
                        <entry key="json" value="application/json"/>
                    </map>
                </property>
                <property name="viewResolvers">
                    <list>
                        <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
                        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                            <property name="prefix" value="/WEB-INF/jsp/"/>
                            <property name="suffix" value=".jsp"/>
                        </bean>
                    </list>
                </property>
                <property name="defaultViews">
                    <list>
                        <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
                    </list>
                </property>
            </bean>     

            <bean id="dataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
                <property name="driverClass" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
                <property name="jdbcUrl" value="jdbc:sqlserver://${db.velocity.host}:${db.velocity.port};databaseName=${db.velocity.database}" />
                <property name="user" value="${db.velocity.username}" />
                <property name="password" value="${db.velocity.password}" /> 

                <!-- these are C3P0 properties -->
                <property name="acquireIncrement" value="1" />
                <property name="minPoolSize" value="5" />
                <property name="maxPoolSize" value="20" />
                <property name="maxIdleTime" value="300" />
            </bean>

            <bean id="hibernate3AnnotatedSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
                <property name="dataSource" ref="dataSource" />
                <property name="annotatedClasses">
                    <list>
                        <value>com.asi.ext.api.dao.ExternalAPILogger</value>
                    </list>
                </property>
                <property name="hibernateProperties">
                    <props>
                        <prop key="hibernate.show_sql">false</prop>
                        <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
                        <prop key="hibernate.current_session_context_class">thread</prop>
                    </props>
                </property>
            </bean>

            <bean id="logDAO" class="com.asi.ext.api.dao.ExternalApiLoggerImpl">
                <property name="sessionFactory" ref="hibernate3AnnotatedSessionFactory" />
            </bean>

            <bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
                <property name="messageConverters">
                    <list>
                        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                            <property name="supportedMediaTypes">
                                <list>
                                    <bean class="org.springframework.http.MediaType">
                                        <constructor-arg value="application" />
                                        <constructor-arg value="json" />
                                        <constructor-arg value="#{T(java.nio.charset.Charset).forName('UTF-8')}"/>
                                    </bean>
                                </list>
                            </property>
                        </bean>
                        <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/> 
                        <bean class="org.springframework.http.converter.StringHttpMessageConverter ">
                            <constructor-arg value="#{T(java.nio.charset.Charset).forName('UTF-8')}"/>
                        </bean> 
                    </list>
                </property>
            </bean>
            <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
                <property name="basename">
                    <value>error</value>
                </property>
            </bean>
</beans>

com.asi.ext.api.dao.ExternalAPILogger
假的
org.hibernate.dialogue.sqlserverdialogue
线
错误
然而,v1、v2和v3.xml导入用于不同版本的bean。例如,下面是v1.xml:


这个问题从来没有出现过,直到我使用Java7版本时,我把它切换到Java8,然后我看到了这个异常。由于该编辑器的限制,我无法粘贴整个异常日志。如果需要,将粘贴注释

有人能帮我解决这个问题吗?

试试
value=“java.nio.charset.StandardCharsets.UTF_8”
而不是
value=“{T(java.nio.charset.charset).forName('UTF-8')}”
。您还需要命名参数,否则Spring将无法实例化MediaType,因为存在多个具有3个参数的构造函数

您可以尝试以下代码:

<bean class="org.springframework.http.MediaType" >
        <constructor-arg name="type" value="application" />
        <constructor-arg  name="subtype" value="json" />
        <constructor-arg name="charset" value="#{T(java.nio.charset.StandardCharsets).UTF_8}"/>
    </bean>

感谢@louis-f指出问题并提供解决方案。我试图用上述建议来解决这个问题,但它仍然给出了同样的例外。 然后我尝试了以下方法,它解决了我的问题:

        <bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
            <property name="messageConverters">
                <list>
                    <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                        <property name="supportedMediaTypes">
                            <list>
                                <bean class="org.springframework.http.MediaType">
                                    <constructor-arg name="type" value="application"/>
                                    <constructor-arg name="subtype" value="json"/>
                                    <constructor-arg name="charset" value="UTF-8" />
                                </bean>
                            </list>
                        </property>
                    </bean>
                    <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/> 
                    <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                        <constructor-arg name="defaultCharset" value="UTF-8" />
                    </bean> 
                </list>
            </property>
        </bean>

所以,我实际上只是将charset构造函数参数改为字符串值,即“UTF-8”,它确实对我有用。 在应用程序调试中,我在inspect元素中检查了对象restTemplate,现在messageConverters列表的所有内容都正常了


再次感谢。

“#{T(java.nio.charset.charset).forName('UTF-8')}”似乎是PBN,那么我该如何解决它呢?感谢您指出这个问题。你能给我一个完整的例子,在我的情况下,如何设置它准确?那将是一个很大的帮助!
        <bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
            <property name="messageConverters">
                <list>
                    <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                        <property name="supportedMediaTypes">
                            <list>
                                <bean class="org.springframework.http.MediaType">
                                    <constructor-arg name="type" value="application"/>
                                    <constructor-arg name="subtype" value="json"/>
                                    <constructor-arg name="charset" value="UTF-8" />
                                </bean>
                            </list>
                        </property>
                    </bean>
                    <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/> 
                    <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                        <constructor-arg name="defaultCharset" value="UTF-8" />
                    </bean> 
                </list>
            </property>
        </bean>