【问题标题】:debug HTTP Status 500 - Servlet.init() for servlet dispatcher threw exception调试 HTTP 状态 500 - Servlet 调度程序的 Servlet.init() 引发异常
【发布时间】:2017-08-13 14:04:00
【问题描述】:

我正在尝试创建自己的 eshop。我有一些问题。我认为问题出在 xml 文件上,所以我不发布我的类、控制器等。如果你让我发布,我会的。

applicationContext.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:tx="http://www.springframework.org/schema/tx"
    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-4.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">

    <beans profile="dev">

        <context:annotation-config></context:annotation-config>
        <context:component-scan base-package="com.emusicstore.dao, com.emusicstore.impl"></context:component-scan> 


        <context:property-placeholder location="jdbc.properties" />

        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close">

            <property name="driverClassName" value="${jdbc.driver}"></property>
            <property name="url" value="${jdbc.url}"></property>
            <property name="password" value="${jdbc.password}"></property>
            <property name="username" value="${jdbc.username}"></property>
        </bean>

        <!-- MY CHANGES -->
        <!-- LocalSessionFactoryBean for hibernate4 -->

        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

            <property name="dataSource" ref="dataSource"></property>
            <property name="hibernateProperties">
                <props>
                    <!-- CHANGE HERE FOR MySQL -->
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.format_sql">true</prop>
                </props>
            </property>
            <!-- To know where is ProductDao -->
            <property name="packagesToScan">
                <list>
                    <value>com.emusicstore.dao</value>
                    <value>com.emusicstore.impl</value>
                </list>
            </property>
        </bean>

        <bean id="transactionManager"
            class="org.springframework.orm.hibernate4.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
        <tx:annotation-driven transaction-manager="transactionManager" />
    </beans>
</beans>

dispatcher-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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    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/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">


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

<context:annotation-config></context:annotation-config>

<mvc:annotation-driven />

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>

<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />

<tx:annotation-driven />

web.xml(我的代码的一部分)

        <context-param>
       <param-name>contextConfigLocation</param-name>
         <param-value>
          /WEB-INF/applicationContext.xml
        </param-value>
    </context-param>
    <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
      <servlet-name>dispatcher</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param> 
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/dispatcher-servlet.xml
            </param-value>
       </init-param>
       <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
      <servlet-name>dispatcher</servlet-name>
      <url-pattern>/</url-pattern>
    </servlet-mapping>

第一个错误如下

        HTTP Status 500 - Servlet.init() for servlet dispatcher threw exception

    type Exception report

    message Servlet.init() for servlet dispatcher threw exception

    description The server encountered an internal error that prevented it from fulfilling this request.

    exception
    javax.servlet.ServletException: Servlet.init() for servlet dispatcher threw exception
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:474)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
    org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:495)
    org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:767)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1347)
    org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Unknown Source)

那么下面的所有错误都是:

org.springframework.beans.factory.BeanCreationException
org.springframework.beans.factory.NoSuchBeanCreationDefinitionException

感谢任何形式的帮助。 在此先感谢迈克

编辑

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'homeController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private dao.ProductDao controllers.HomeController.productDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [dao.ProductDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

ProductDao

package com.emusicstore.dao;
import java.util.List;
import com.emusicstore.model.Product;
public interface ProductDao 
{
    void addProduct(Product product);
    Product getProductById (String id);
    List<Product> getAllProducts();
    void deleteProduct (String id);
}

ProductDaoImpl

package com.emusicstore.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.emusicstore.dao.ProductDao;
import com.emusicstore.model.Product;

@Component
@Transactional
public class ProductDaoImpl implements ProductDao {

@Autowired
private SessionFactory sessionFactory;

public void addProduct(Product product) {
    Session session = sessionFactory.getCurrentSession();
    session.saveOrUpdate(product);
    session.flush();
}

public Product getProductById(String id) {
    Session session = sessionFactory.getCurrentSession();
    Product product = (Product) session.get(Product.class, id);
    session.flush();

    return product;
}

public List<Product> getAllProducts() {
    Session session = sessionFactory.getCurrentSession();
    Query query = session.createQuery("from Product");
    List<Product> products = query.list();
    session.flush();

    return products;
}

public void deleteProduct(String id) {
    Session session = sessionFactory.getCurrentSession();
    session.delete(getProductById(id));
    session.flush();
}

}

【问题讨论】:

  • 你能发布完整的错误日志吗?
  • 我刚刚更新了我的帖子。
  • ProductDao 怎么样?
  • 在 ProductDao 我定义我的方法。在 ProduDaoImpl 实现 Product 我为所有方法编写代码并使用 @Autowired SessionFactory。
  • 错误提示 - 它无法为 productDao 创建 bean,您可以发布该课程吗?

标签: java xml spring servlets


【解决方案1】:

您可以通过使用@Qualifier 注释来告诉 Spring 使用您的 ProductDaoImpl bean,如下所示。

@Autowire
@Qualifier("productDaoImpl") 
ProductDao productDao;

【讨论】:

  • 不幸的是,同样的错误出现了。 :( 我将检查我的 pom.xml 以查看我是否使用了正确的依赖项。
猜你喜欢
  • 1970-01-01
  • 2018-06-29
  • 1970-01-01
  • 2013-02-04
  • 2015-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多