2017-03-16 138 views
1

我在這裏是新的,我試圖創建一個使用Spring的電子商店,但我有一個連接到數據庫的問題。我搜索了一下,發現其他用戶提出了同樣的問題,但我找不到問題。我缺乏想法,這裏有什麼問題。自動佈線依賴項的注入失敗;嵌套的異常是org.springframework.beans.factory.BeanCreationException:

堆棧跟蹤:

org.springframework.beans.factory.BeanCreationException:錯誤 創建名爲 'HomeController的' 豆:自動裝配 依賴注入失敗;嵌套的異常是 org.springframework.beans.factory.BeanCreationException:不能 autowire字段:private dao.ProductDao controllers.HomeController.productDao;嵌套異常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:否 找到依賴關係的[dao.ProductDao]類型符合條件: 預計至少有1個bean符合自動連接候選符合 此依賴關係。 相關注解:{@ org.springframework.beans.factory.annotation.Autowired(所需=真)}

源代碼:

控制器

package controllers; 

import java.io.IOException; 
import java.util.List; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 

import dao.ProductDao; 
import model.Product; 

@Controller 
public class HomeController { 

@Autowired 
private ProductDao productDao; 

@RequestMapping("/") 
public String home(){ 
    return "home"; 
} 

@RequestMapping("/productList") 
public String getProducts(Model model) { 
    List<Product> products = productDao.getAllProducts(); 
    model.addAttribute("products", products); 

    return "productList"; 
} 

@RequestMapping("/productList/viewProduct/{productId}") 
public String viewProduct(@PathVariable String productId, Model model) throws IOException{ 

    Product product = productDao.getProductById(productId); 
    model.addAttribute(product); 
    return "viewProduct"; 
} 

}

ProductDao

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

ProductdaoImpl

package dao.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.Repository; 
    import org.springframework.transaction.annotation.Transactional; 

    import dao.ProductDao; 
    import model.Product; 

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

實體(產品)

package model; 

    import javax.persistence.Entity; 
    import javax.persistence.GeneratedValue; 
    import javax.persistence.GenerationType; 
    import javax.persistence.Id; 

    @Entity 
    public class Product { 

     @Id 
     @GeneratedValue(strategy = GenerationType.AUTO) 
     private String productId; 
     private String productName; 
     private String productCategory; 
     private String productDescription; 
     private double productPrice; 
     private String productCondition; 
     private String productStatus; 
     private int unitInStock; 
     private String productManufacture; 

     public String getProductId() { 
      return productId; 
     } 

     public void setProductId(String productId) { 
      this.productId = productId; 
     } 

     public String getProductName() { 
      return productName; 
     } 

     public void setProductName(String productName) { 
      this.productName = productName; 
     } 

     public String getProductCategory() { 
      return productCategory; 
     } 

     public void setProductCategory(String productCategory) { 
      this.productCategory = productCategory; 
     } 

     public String getProductDescription() { 
      return productDescription; 
     } 

     public void setProductDescription(String productDescription) { 
      this.productDescription = productDescription; 
     } 

     public double getProductPrice() { 
      return productPrice; 
     } 

     public void setProductPrice(double productPrice) { 
      this.productPrice = productPrice; 
     } 

     public String getProductCondition() { 
      return productCondition; 
     } 

     public void setProductCondition(String productCondition) { 
      this.productCondition = productCondition; 
     } 

     public String getProductStatus() { 
      return productStatus; 
     } 

     public void setProductStatus(String productStatus) { 
      this.productStatus = productStatus; 
     } 

     public int getUnitInStock() { 
      return unitInStock; 
     } 

     public void setUnitInStock(int unitInStock) { 
      this.unitInStock = unitInStock; 
     } 

     public String getProductManufacture() { 
      return productManufacture; 
     } 

     public void setProductManufacture(String productManufacture) { 
      this.productManufacture = productManufacture; 
     } 

    } 

jdbc.properties

jdbc.username = **** 
    jdbc.password = **** 
    jdbc.driver = com.mysql.jdbc.Driver 
    jdbc.url = jdbc:mysql://localhost:3306/myeshop 

的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:tx="http://www.springframework.org/schema/tx" 
     xmlns:context="http://www.springframework.org/schema/context" 
     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:property-placeholder 
       location="properties/jdbc.properties" /> 
      <bean id="dataSource" 
       class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
       <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> 

      <bean id="sessionFactory" 
       class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
       <property name="dataSource" ref="dataSource"></property> 
       <property name="hibernateProperties"> 
        <props> 
         <prop key="hibernate.diaect">org.hibernate.dialect.MySQL5Dialect</prop> 
         <!-- <prop key="hibernate.hbm2ddl.auto">update</prop> 
          <prop key="hibernate.show_sql">true</prop> 
          <prop key="hibernate.format_sql">tru</prop> 
         --> 
        </props> 
       </property> 
       <property name="packagesToScan"> 
        <list> 
         <value>dao</value> 
        </list> 
       </property> 
      </bean> 

    <!-- Exei alli klasi sto e-commerce me ref sto SessionFactory 
      <bean id="transactionManager" 
       class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
       <property name="sessionFactory" ref="sessionFactory"></property> 
      </bean> 
    --> 
      <bean id="transactionManager" 
       class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
       <property name="dataSource" ref="dataSource"></property> 
      </bean> 
      <tx:annotation-driven /> 
     </beans> 

    </beans> 

調度-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="controllers" /> 

     <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 /> 
    </beans> 

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
     version="3.1"> 
     <servlet> 
      <servlet-name>dispatcher</servlet-name> 
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
      <load-on-startup>1</load-on-startup> 
     </servlet> 
     <servlet-mapping> 
      <servlet-name>dispatcher</servlet-name> 
      <url-pattern>/</url-pattern> 
     </servlet-mapping> 

     <!-- add listener --> 

     <description>eShop Database</description> 
     <resource-ref> 
      <description>DB Connection</description> 
      <res-ref-name>jdbc/myeshop</res-ref-name> 
      <res-type>javax.sql.DataSource</res-type> 
      <res-auth>Container</res-auth> 
     </resource-ref> 

     <listener> 
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
     </listener> 
     <context-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value> 
      WEB-INF/dispatcher-servlet.xml 
      WEB-INF/applicationContext.xml 
     </param-value> 
     </context-param> 
     <!-- end of adding listener --> 
    </web-app> 

pom.xm l

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 

     <modelVersion>4.0.0</modelVersion> 
     <groupId>com.mywebsite</groupId> 
     <artifactId>emusicstore</artifactId> 
     <version>0.0.1-SNAPSHOT</version> 
     <packaging>war</packaging> 
     <dependencies> 
      <dependency> 
       <groupId>org.springframework</groupId> 
       <artifactId>spring-webmvc</artifactId> 
       <version>4.1.4.RELEASE</version> 
      </dependency> 

      <dependency> 
       <groupId>org.springframework</groupId> 
       <artifactId>spring-core</artifactId> 
       <version>4.1.1.RELEASE</version> 
      </dependency> 
      <dependency> 
       <groupId>org.springframework</groupId> 
       <artifactId>spring-orm</artifactId> 
       <version>4.1.4.RELEASE</version> 
      </dependency> 
      <dependency> 
       <groupId>org.hibernate</groupId> 
       <artifactId>hibernate-core</artifactId> 
       <version>4.0.1.Final</version> 
      </dependency> 
      <dependency> 
       <groupId>org.hibernate.javax.persistence</groupId> 
       <artifactId>hibernate-jpa-2.0-api</artifactId> 
       <version>1.0.1.Final</version> 
      </dependency> 
      <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> 
      <dependency> 
       <groupId>mysql</groupId> 
       <artifactId>mysql-connector-java</artifactId> 
       <version>6.0.5</version> 
      </dependency> 



      <dependency> 
       <groupId>jstl</groupId> 
       <artifactId>jstl</artifactId> 
       <version>1.2</version> 
      </dependency> 

      <dependency> 
       <groupId>taglibs</groupId> 
       <artifactId>standard</artifactId> 
       <version>1.1.2</version> 
      </dependency> 

     </dependencies> 

     <build> 
      <plugins> 
       <plugin> 
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-compiler-plugin</artifactId> 
        <version>3.5.1</version> 
        <configuration> 
         <source>1.8</source> 
         <target>1.8</target> 
        </configuration> 
       </plugin> 
      </plugins> 
     </build>   
    </project> 

在此先感謝!:)

+0

我認爲你需要做一個組件掃描。爲dao或dao.impl包添加組件掃描。 –

+0

我不確定,但您嘗試自動調用調用時爲空的Dao實例。嘗試通過添加setter到'Home Controller'來解決這個問題。 – Reborn

回答

0

的Botas,

你需要把你的道封裝的元件騙局標籤在調度員servlet.xml中。

<context:component-scan base-package="controllers,dao" /> 
+0

我試圖調試的那一天,但沒有任何東西..我嘗試了所有推薦的解決方案,但同樣失敗..任何其他想法? –

0

你的applicationContext.xml改成這樣

<property name="packagesToScan"> 
    <list> 
     <value>dao.**.*</value> 
    </list> 
</property> 

現在每個班級,即使在子包,將彈簧被發現。

+0

整天我試圖調試,但沒有任何..我嘗試了所有推薦的解決方案,但同樣失敗..任何其他想法? –

0

除了包sanning,你似乎錯誤地定義了你的sessionFactory。

當您使用彈簧4和休眠4,你應該把它定義爲:

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

也可以嘗試定義事務管理器爲:

<bean id="transactionManager" 
    class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory" /> 
</bean> 
+0

整日我試圖調試,但沒有任何..我嘗試了所有推薦的解決方案,但同樣失敗..任何其他想法? –

相關問題