debug HTTP Status 500 - Servlet.init() for servlet dispatche



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

应用程序上下文.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)}

产品道

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);
}

产品展示

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();
}
}

你可以告诉Spring使用你的ProductDaoImplbean,方法是使用@Qualifier注释,如下所示。

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

最新更新