Spring无法找到用户创建的类

时间:2015-10-21 23:33:17

标签: java spring

尝试在这里创建一个相当简单的bean来访问数据库是我的bean xml

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

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd


http://www.springframework.org/schema/context


http://www.springframework.org/schema/context/spring-context-3.0.xsd


http://www.springframework.org/schema/tx


http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">



<!-- job DB data source. -->
<bean id="jobViewDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close">
    <property name="driverClass" value="${jdbc.driverClassName}" />
    <property name="jdbcUrl" value="${jdbc.employee_db_url}" />
    <property name="user" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    <property name="maxPoolSize" value="${jdbc.maxPoolSize}" />
    <property name="minPoolSize" value="${jdbc.minPoolSize}" />
    <property name="maxStatements" value="${jdbc.maxStatements}" />
    <property name="testConnectionOnCheckout" value="${jdbc.testConnection}" />
</bean>

<bean id="JobsViewDAO" class="com.api.jobs.daoservice.dao.impl.JobsViewModelServiceDAOImpl;">
    <property name="jobViewDataSource" ref="jobViewDataSource" />
</bean>

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

这是我的bean类

package com.api.jobs.daoservice.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;

import com.api.jobs.daoservice.dao.JobsViewModelServiceDAO;
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class JobsViewModelServiceDAOImpl implements JobsViewModelServiceDAO {


    public ComboPooledDataSource getJobViewDataSource() {
        return jobViewDataSource;
    }

    public void setJobViewDataSource(ComboPooledDataSource jobViewDataSource) {
        this.jobViewDataSource = jobViewDataSource;
    }

    ComboPooledDataSource jobViewDataSource;

    public Long getJobViewCountforPerson(String peopleId) {
        Long startTime = System.currentTimeMillis();
        Long result = 0L;
        Connection conn = null;
        Statement stmt =null;
        try {
            conn = jobViewDataSource.getConnection();
            stmt = conn.createStatement();
            String sql;
            sql = "select count(job_view_id) from job_view where people_id = " +peopleId;
            ResultSet rs = stmt.executeQuery(sql);
            // Get the data from the result set.
            while (rs.next()) {
                // Retrieve two columns.
                result = rs.getLong(1);
                Long endTime = System.currentTimeMillis();
                // System.out.println(result);
                //System.out.println("Total Runtime: " + (endTime - startTime));
            }
            rs.close();
            stmt.close();
            conn.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
               //Finally block to close resources.
               try{
                  if(stmt!=null)
                     stmt.close();
               }catch(Exception ex){
               }// nothing we can do
               try{
                  if(conn!=null)
                     conn.close();
               }catch(Exception ex){
                  ex.printStackTrace();
               }
            }
        return result;
    }

    public Long getJobViewCountforPerson(String peopleId, Date date) {
        Long startTime = System.currentTimeMillis();
        Long result = 0L;
        Connection conn = null;
        Statement stmt =null;
        try {
            conn = jobViewDataSource.getConnection();
            stmt = conn.createStatement();
            String sql;
            sql = "select count(job_view_id) from job_view where people_id = " +peopleId +"and date_created > " +date;
            ResultSet rs = stmt.executeQuery(sql);
            // Get the data from the result set.
            while (rs.next()) {
                // Retrieve two columns.
                result = rs.getLong(1);
                Long endTime = System.currentTimeMillis();
                // System.out.println(result);
                //System.out.println("Total Runtime: " + (endTime - startTime));
            }
            rs.close();
            stmt.close();
            conn.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
               //Finally block to close resources.
               try{
                  if(stmt!=null)
                     stmt.close();
               }catch(Exception ex){
               }// nothing we can do
               try{
                  if(conn!=null)
                     conn.close();
               }catch(Exception ex){
                  ex.printStackTrace();
               }
            }
        return result;
    }

}

我有以下主要课程:

package com.api.jobs;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.api.jobs.daoservice.dao.JobsViewModelServiceDAO;
import com.api.jobs.daoservice.dao.impl.JobsViewModelServiceDAOImpl;

public class PocController {

    public static void main(String[] args){

        JobsViewModelServiceDAOImpl jb = new JobsViewModelServiceDAOImpl();

        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

        JobsViewModelServiceDAO jobViewDAO = (JobsViewModelServiceDAO) context.getBean("JobsViewDAO");

        jobViewDAO.getJobViewCountforPerson("858774");
        jobViewDAO.getJobViewCountforPerson("356071");

    }

}

我有以下错误:

Oct 21, 2015 4:17:47 PM 

org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1739422b: startup date [Wed Oct 21 16:17:47 PDT 2015]; root of context hierarchy
Oct 21, 2015 4:17:47 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring.xml]
Oct 21, 2015 4:17:48 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@212aa789: defining beans [jobViewDataSource,JobsViewDAO,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
Oct 21, 2015 4:17:48 PM com.mchange.v2.log.MLog 
INFO: MLog clients using java 1.4+ standard logging.
Oct 21, 2015 4:17:53 PM com.mchange.v2.c3p0.C3P0Registry 
INFO: Initializing c3p0-0.9.5.1 [built 16-June-2015 00:06:36 -0700; debug? true; trace: 10]
Oct 21, 2015 4:17:54 PM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@212aa789: defining beans [jobViewDataSource,JobsViewDAO,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.api.jobs.daoservice.dao.impl.JobsViewModelServiceDAOImpl;] for bean with name 'JobsViewDAO' defined in class path resource [spring.xml]; nested exception is java.lang.ClassNotFoundException: com.api.jobs.daoservice.dao.impl.JobsViewModelServiceDAOImpl;
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1263)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:581)
    at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1332)
    at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:898)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:588)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.api.jobs.PocController.main(PocController.java:30)
Caused by: java.lang.ClassNotFoundException: com.api.jobs.daoservice.dao.impl.JobsViewModelServiceDAOImpl;
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at org.springframework.util.ClassUtils.forName(ClassUtils.java:258)
    at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:415)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1284)
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1255)
    ... 9 more

但是正如你在我的主要内容中所看到的,我可以使用new在我的主要内容中创建类没有问题,但是当我调用上下文时它会因为无法找到它而失败。

1 个答案:

答案 0 :(得分:1)

你有一个;在bean的bean定义中的class属性的末尾:JobsViewDAO。这很可能是错误发生的地方。