当前位置: 首页 > 知识库问答 >
问题:

创建名为“accountController”的bean时出错

康文昌
2023-03-14

因此,我们终于设法将我们的Spring框架从版本< code>3.2更新到了< code>4.2.25。经过一些痛苦的过程后,我现在陷入了这个异常:< code >创建名为' accountController '的bean时出错:注入资源依赖关系失败;嵌套异常是org . spring framework . beans . factory . beancreationexception 。< br >

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/servlet-context.xml,
            /WEB-INF/spring/servlet-security.xml
        </param-value>
</context-param>

<servlet>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/spring/servlet-context.xml,
                /WEB-INF/spring/servlet-security.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
</servlet>
</web-app>

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

<context:property-placeholder location="/WEB-INF/spring/spring.properties" />

<!-- Enable annotation style of managing transactions -->
<tx:annotation-driven transaction-manager="transactionManager" />   

<!-- Declare a transaction manager -->
<!-- See http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/transaction.html -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource" />

<!-- Declare a datasource that has pooling capabilities-->   
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
            destroy-method="close"
            p:driverClass="${app.jdbc.driverClassName}"
            p:jdbcUrl="${app.jdbc.url}"
            p:user="${app.jdbc.username}"
            p:password="${app.jdbc.password}"
            p:acquireIncrement="1"
            p:idleConnectionTestPeriod="60"
            p:maxPoolSize="247"
            p:maxStatements="100"
            p:minPoolSize="1" />    

<bean id="dataSourceSlave" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
            destroy-method="close"
            p:driverClass="${app.jdbc.driverClassName}"
            p:jdbcUrl="${app.slave.jdbc.url}"
            p:user="${app.slave.jdbc.username}"
            p:password="${app.slave.jdbc.password}"
            p:acquireIncrement="1"
            p:idleConnectionTestPeriod="60"
            p:maxPoolSize="247"
            p:maxStatements="100"
            p:minPoolSize="1" />            

和一个完整的异常printStack():

SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException:
    Error creating bean with name 'accountController': Injection of resource dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException:
    Error creating bean with name 'accountService': Injection of resource dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException:
    Error creating bean with name 'friendService': Injection of resource dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException:
    Error creating bean with name 'agentProfileService': Injection of resource dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException:
    Error creating bean with name 'screenNameService': Injection of resource dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException:
    Error creating bean with name 'exceptionService': Injection of resource dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException:
    Error creating bean with name 'mailService': Injection of resource dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException:
    Error creating bean with name 'emailNotificationService': Injection of resource dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException:
    Error creating bean with name 'sportProfileService': Injection of resource dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException:
    Error creating bean with name 'countryService': Injection of resource dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException:
    Error creating bean with name 'dataSourceSlave' defined in 
    ServletContext resource [/WEB-INF/spring/jdbc-context.xml]:
        Error setting property values; nested exception is
org.springframework.beans.NotWritablePropertyException:
    Invalid property 'acquireIncrement' of bean class
    [org.springframework.jdbc.datasource.DriverManagerDataSource]:
    Bean property 'acquireIncrement' is not writable or has an invalid setter method.
    Does the parameter type of the setter match the return type of the getter?
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:311)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5068)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5584)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:899)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:875)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1259)
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1998)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)

不确定是否相关:我正在使用javax.servlet-api-3.1

现在是AccountService.java:

@Service("accountService")
@Transactional
public class AccountService
{
protected static Logger log = LogManager.getLogger(AccountService.class);
private NamedParameterJdbcTemplate jdbcTemplate;
private SimpleJdbcInsert jdbcInviteInsert;
private SimpleJdbcInsert jdbcChallengeInsert;
private SimpleJdbcInsert jdbcPostitInsert;
private SimpleJdbcInsert jdbcPreferenceInsert;

@Resource(name = "dataSource")
public void setDataSource(DataSource dataSource)
{
    this.jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
    this.jdbcInviteInsert = new SimpleJdbcInsert(dataSource).withTableName("invite_contact").usingGeneratedKeyColumns("id");
    this.jdbcChallengeInsert = new SimpleJdbcInsert(dataSource).withTableName("challenge").usingGeneratedKeyColumns("id");
    this.jdbcPreferenceInsert = new SimpleJdbcInsert(dataSource).withTableName("preference").usingGeneratedKeyColumns("id");
    this.jdbcPostitInsert = new SimpleJdbcInsert(dataSource).withTableName("account_postit");
}

// super-awesome-revolutionary code

}

如果我需要提供任何其他信息,请告诉我:)

共有3个答案

汲时铭
2023-03-14

检查您的 servlet 上下文.xml以查看您是否正在扫描控制器所在的软件包,然后检查您是否有类似如下的内容:

<context:component-scan base-package="your/package/path/controllers"/>

您的/包/路径/控制器是您的控制器类所在的位置。

蒯宏达
2023-03-14

根本原因是

未定义名为“数据源斯拉夫”的 Bean

似乎在服务内部的某个地方,您使用此名称引用它,而数据源bean是使用名称“dataSource”创建的。

潘飞英
2023-03-14

驱动程序管理器数据修补程序不提供池化功能。它不定义属性获取增量,空闲连接测试周期,最大池大小,最大状态和最小池大小。

请删除这些属性,或者使用支持池的数据源,例如Apache commons-dbcp basic data source。不建议在生产系统中使用DriverManagerDataSurce。这是为了测试目的。

例如

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxActive" value="100"/>
    </bean>
 类似资料:
  • 我有一个实体类InAppNotification。看起来像这样的java: 我使用JPA来处理数据库查询,这就是JPA接口的定义: 这是我application.properties的样子: 但是,当我试图在构建后通过运行 来打包应用程序时,我会遇到以下问题: 尝试调用不存在的方法。尝试从以下位置进行:javax.el.ELManager.getExpress sionWorks(ELManage

  • 在将project从Spring Boot版本从1.2.3.release迁移到1.3.0.release之后,我已经开始得到以下异常。 创建类路径资源[org/springframework/boot/autoconfigure/admin/springapplicationadminjmxautoconfiguration.class]中定义的名为'Spring ApplicationAdmi

  • 我遵循本教程将消息发送到azure服务队列:https://docs.microsoft.com/en-us/azure/java/spring-framework/configure-spring-boot-starter-java-app-with-azure-service-bus 到我现有的spring boot应用程序,但我得到以下错误: 用户类别: 控制器类: pom xml: 添加它

  • 我试图将弹性搜索集成到spring-boot应用程序中,但我得到了这个*创建名为“client”的bean时出错*异常,不确定是什么地方出了问题,因为我之前找不到任何类似的线索...非常感谢你为大家指路。这是mu elasticsearch配置: 这是我的主要应用程序: 我的pom.xml: 这是te异常跟踪:

  • 我有这些错误编译,我不知道我哪里错了,我只有这3个类

  • 我正在尝试在spring boot应用程序中使用SQLite。但是应用程序不能创建下面的bean。 org.springframework.boot.autocigure.orm.jpa.hibernatejaconfiguration 我该怎么办?我在这个站点上搜索了相关的问题,但是找不到一个合适的。 如下所示。 4.0.0 org.springframework.Boot spring-boo