找不到Bean错误

时间:2018-05-25 12:47:31

标签: spring

以下是代码段

@Component()
public class DbConn {

    @Resource(name="vDataSource")
    protected DataSource dataSource;

    public  void doCall () {
    SimpleJdbcCall jdbcCall = new SimpleJdbcCall(dataSource)
//          .withCatalogName(config.getPackageName())
            .withProcedureName("TEST_PROC")
            .withoutProcedureColumnMetaDataAccess();
    }

这是我的主要课程

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/" + "v-test.xml") ;
DbConn d1= context.getBean("dbConn",DbConn.class);

当我试图运行main时,它会抛出一个错误: 没有定义名为'dbConn'的bean

我该如何解决问题

我的应用程序上下文v-test.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:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util.xsd">

    <context:annotation-config/>
    <context:property-placeholder/>
    <context:component-scan base-package="com.sgcib.v.conso" />

    <import resource="classpath:spring/v-common-applicationcontext.xml"/>

    <bean id="vltDataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.v.driverClassName}"/>
        <property name="url" value="${jdbc.v.url}"/>
        <property name="username" value="${jdbc.v.username}"/>
        <property name="password" value="${jdbc.v.password}"/>
        <property name="testOnBorrow" value="true" />
        <property name="validationQuery" value="SELECT 1 FROM DUAL" />
    </bean>

</beans>

错误堆栈低于

18:31:31.048 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
18:31:31.078 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@77c2494c]
18:31:31.079 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
18:31:31.081 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dbConn' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:677)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1180)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1082)
    at test.MainClass.main(MainClass.java:16)

1 个答案:

答案 0 :(得分:0)

尝试命名您的组件:

  

@Component(&#34; dbConn&#34)