当前位置: 首页 > 知识库问答 >
问题:

autowired依赖性注射失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:

陆和泰
2023-03-14

我是新来的,我试图用Spring创建一个电子商店,但我有一个问题,与数据库的连接。我搜索了一下,发现其他用户也问了同样的问题,但我找不到问题所在。我缺乏想法,这里出了什么问题。

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.username = ****
  jdbc.password = ****
  jdbc.driver = com.mysql.jdbc.Driver
  jdbc.url = jdbc:mysql://localhost:3306/myeshop
    <?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>
    <?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>
    <?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>
    <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>

提前感谢!:)

共有1个答案

钱朝明
2023-03-14

博塔斯,

您需要将Dao包放在dispatcher-servlet.xml的组件scam标记中。

<context:component-scan base-package="controllers,dao" />
 类似资料: