上下文初始化期间遇到异常 - 取消刷新尝试

时间:2017-08-18 07:18:38

标签: java spring

WARNING: Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'registrationController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.concept.UserService com.concept.RegistrationController.userService; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.concept] for bean with name 'UserService' defined in ServletContext resource [/WEB-INF/Spring-servlet.xml]; nested exception is java.lang.ClassNotFoundException: com.concept

您能否帮助解决可能的原因。我是Spring框架的新手

WARNING: Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'registrationController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.concept.UserService com.concept.RegistrationController.userService; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.concept] for bean with name 'UserService' defined in ServletContext resource [/WEB-INF/Spring-servlet.xml]; nested exception is java.lang.ClassNotFoundException: com.concept
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:762)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:663)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:629)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:677)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:548)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:489)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.java:158)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1227)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1140)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1027)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5038)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5348)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

这是我的控制器

    package com.concept;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.*;
    import com.concept.UserService;
    import com.concept.User;

    @Controller
    public class RegistrationController {

     @Autowired  
     UserService userService;  
     @RequestMapping("/insert")         
     public String inserData(@ModelAttribute User user) {  
     if (user != null)  
      userService.insertData(user);  

      return "result" ;  
       }  
      }

这是user.java

    package com.concept;

    public class User {
         private String firstName;
         private String lastName;
         private Integer age;
        private String email;
       public String getFirstName() {
          return firstName;
           }


public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }


    }

这是我的UserDAO.java

package com.concept;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

public class UserDAO implements UserService {
     @Autowired  
     DataSource dataSource;  

     public void insertData(User user) {  

      String sql = "INSERT INTO Enroll(first_name,last_name, age, email) VALUES (?, ?, ?,?)";  

      JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);  

      jdbcTemplate.update(sql, new Object[] { user.getFirstName(), user.getLastName(),  
          user.getAge(),user.getEmail() });  

     }  
}

这是我的UserService.java

package com.concept;


public interface UserService {
     public void insertData(User user);  
}

这是我的Spring-servlet.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:context="http://www.springframework.org/schema/context"  
 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">  

 <context:annotation-config />  

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

 <!-- adding view resolver to show jsp's on browser -->  
 <bean id="viewResolver"  
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  <property name="prefix" value="/WEB-INF/jsp/" />  
  <property name="suffix" value=".jsp" />  
 </bean>  

   <bean id="UserService" class="com.concept"/>


 <!-- declare datasource bean -->  
 <bean id="dataSource"  
  class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  <property name="driverClassName" value="oracle.jdbc.OracleDriver" />  
  <property name="url" value="jdbc:oracle:thin:@localhost:1521:XE" />  
  <property name="username" value="EMPLOYEE" />  
  <property name="password" value="pass1234" />  
 </bean>  
</beans>  

这是我的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_2_5.xsd"  
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
 id="WebApp_ID" version="2.5"> 


 <display-name>Spring MVC Application</display-name>  

 <servlet>  
  <servlet-name>Spring</servlet-name>  
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  <load-on-startup>1</load-on-startup>  
 </servlet>  

 <servlet-mapping>  
  <servlet-name>Spring</servlet-name>  
  <url-pattern>/</url-pattern>  
 </servlet-mapping>  
</web-app>

1 个答案:

答案 0 :(得分:1)

查看根本原因:nested exception is java.lang.ClassNotFoundException: com.concept

删除第<bean id="UserService" class="com.concept"/>

因为com.concept是一个包而不是一个类。你也不应该混合基于XML和注释的配置。

并将@Repository添加到public class UserDAO implements UserService {

但我不明白为什么DAO实现了一个名为Service

的接口