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

在Spring自动跟踪多个数据源

郁宾鸿
2023-03-14

我的DAO类(Spring 3.0)中有autowired一个数据源,即:

@Component
public class MyDAO {

    private NamedParameterJdbcTemplate template;

    @Autowired
    @Qualifier("dataSource")
    public void setDataSource(DataSource ds) {
      template = new NamedParameterJdbcTemplate(ds);
    }
}
@Component
public class MyDAO {

    private NamedParameterJdbcTemplate template;

    @Autowired
    @Qualifier("dataSource")
    public void setDataSource(DataSource ds) {
      template = new NamedParameterJdbcTemplate(ds);
    }

    private DataSource dataSource2;

    @Autowired
    @Qualifier("dataSource2")
    public void setDataSource2(DataSource dataSource2) {
      this.dataSource2= dataSource2;
    }
}
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
  <property name="jndiName" value="java:comp/env/jdbc/read" />
</bean>

<bean id="dataSource2" class="org.springframework.jndi.JndiObjectFactoryBean">
  <property name="jndiName" value="java:comp/env/jdbc/update" />
</bean>
SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@78fa39d7] to 

prepare test instance [pkg.namespace.MyServiceImplTest@73eb904d]
java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyDAO': Injection of autowired dependencies failed; nested 

exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void 

pkg.namespace.MyDAO.setDataSource2(javax.sql.DataSource); nested exception is 

org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [javax.sql.DataSource] found for dependency: expected at least 1 bean 

which qualifies as autowire candidate for this dependency. Dependency annotations: {}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues

(AutowiredAnnotationBeanPostProcessor.java:287)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1073)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:516)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:455)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:84)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
    at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:280)
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:304)
    ... 24 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void pkg.namespace.MyDAO.setDataSource2

(javax.sql.DataSource); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [javax.sql.DataSource] found 

for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject

(AutowiredAnnotationBeanPostProcessor.java:593)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues

(AutowiredAnnotationBeanPostProcessor.java:284)
    ... 38 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [javax.sql.DataSource] found for dependency: expected at least 

1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:924)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject

(AutowiredAnnotationBeanPostProcessor.java:551)
    ... 40 more

正如eis所述,我没有更新测试用例的配置文件。

共有1个答案

闻人德庸
2023-03-14

你发布的代码似乎没有问题。

然而,堆栈跟踪似乎表明测试仍然失败(以与发布的代码不一致的方式),所以我猜测测试配置不是最新的。

 类似资料:
  • 1.3.2. 跟踪数据 作为一个有安全意识的开发者,最重要的一件事就是随时跟踪数据。不只是要知道它是什么和它在哪里,还要知道它从哪里来,要到哪里去。有时候要做到这些是困难的,特别是当你对WEB的运做原理没有深入理解时。这也就是为什么尽管有些开发者在其它开发环境中很有经验,但他对WEB不是很有经验时,经常会犯错并制造安全漏洞。 大多数人在读取EMAIL时,一般不会被题为"Re: Hello"之类的垃

  • 我有4个Spring启动应用程序(A,B,C和D)。事务的生命周期如下: 应用程序A是一个kafka流应用程序,它最终产生一个主题,该主题由应用程序B使用。 应用程序B然后使用@KafkaListener从主题中消费,进行一些处理,然后使用Spring的jmsTemboard生成IBMMQ队列。 应用程序C,它是@JMSListener,从上面的队列中消耗并使用Spring的JMST模板生成到另一

  • 我需要设置许多每周重复的警报,当用户在上课时(这是一个学术应用程序)自动将手机调到振动状态,然后在下课时重置音量。 用户可以取消课程和注册课程(但不能直接在应用程序中),这将更新并反映在数据库中,只保留当前的课程。因此,如果我有一个警报设置到一个不再存在的类,我如何取消它,如果我不能复制PendingIntent? 我在这里看到的一个解决方案是创建一个数据库表来跟踪当前的警报,然后我就可以完全控制

  • 我有一些启用了跟踪的Python Lambda函数,它们是这样开始的: 有了这个跟踪,每个Lambda函数本身都可以工作,我可以看到通过boto3对DynamoDB或Kinesis的子服务调用。 但是如何在一个跟踪中将各种Lambda函数连接在一起呢?我正在考虑在第一个函数中生成一个唯一的字符串,并将其写入存储在Kinesis中的消息中。然后,另一个函数将从动觉信息中提取字符串并再次跟踪它。 如何

  • 在微服务环境中,我看到了通过整个业务流程中的所有微服务实例跟踪请求的两个主要好处。 查找服务实例之间或服务实例中的延迟间隙 寻找失败的根源,无论是技术上的还是关于商业案例的 Zipkin提供了一个解决第一个问题的工具。但是,如何使用跟踪来揭示微服务环境中的故障呢?我当然想跟踪所有受错误影响的范围,但不是每个请求,因为它们没有出错。如本文所述,可以使用自定义采样器。 或者,您可以注册自己的采样器be