Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 弹簧&x2B;冬眠。创建bean时出错。不满意的依赖。无法找出哪个批注不正确_Java_Spring_Hibernate_Spring Mvc - Fatal编程技术网

Java 弹簧&x2B;冬眠。创建bean时出错。不满意的依赖。无法找出哪个批注不正确

Java 弹簧&x2B;冬眠。创建bean时出错。不满意的依赖。无法找出哪个批注不正确,java,spring,hibernate,spring-mvc,Java,Spring,Hibernate,Spring Mvc,我已经浏览了关于这个话题的每一篇文章,但我仍然找不到哪里不对。当我尝试运行服务器时,出现以下异常: org.springframework.beans.factory.unsatifiedDependencyException: 创建名为“administratorDAO”的bean时出错:不满意 通过方法“setDao”参数0表示的依赖项:否 限定类型为[com.project.dataAccessLayer.DataAccessObject]的bean 找到依赖项[com.project.d

我已经浏览了关于这个话题的每一篇文章,但我仍然找不到哪里不对。当我尝试运行服务器时,出现以下异常:

org.springframework.beans.factory.unsatifiedDependencyException: 创建名为“administratorDAO”的bean时出错:不满意 通过方法“setDao”参数0表示的依赖项:否 限定类型为[com.project.dataAccessLayer.DataAccessObject]的bean 找到依赖项[com.project.dataAccessLayer.DataAccessObject]: 至少需要1个符合以下条件的bean:autowire候选 这种依赖性。依赖项注释:{};嵌套异常是 org.springframework.beans.factory.noSuchBean定义异常:否 限定类型为[com.project.dataAccessLayer.DataAccessObject]的bean 找到依赖项[com.project.dataAccessLayer.DataAccessObject]: 至少需要1个符合以下条件的bean:autowire候选 这种依赖性。依赖项批注:{}

它说我没有DataAccessObject的bean,即使它是用@Repository注释的,或者在servlet-context.xml中定义的

我有一个CustomerDAO和一个扩展CustomerDAO的AdministratorDAO。我将只发布客户部分,因为我认为如果我能做到这一点,管理员部分将跟进

DataAccessObject class

package com.project.dataAccessLayer;

import java.util.List;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.project.model.Account;
import com.project.model.ModelEntity;
import com.project.model.Product;


@Repository("dao")
public class DataAccessObject {


    private SessionFactory sessionFactory;
    private Session session;

    @Autowired
    @Qualifier("sessionFactory")
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public ModelEntity save(ModelEntity modelEntity) {
        Integer id = null;
        try {
            session = sessionFactory.openSession();
            session.beginTransaction();
            id = (Integer) session.save(modelEntity);
            if (id != null) {
                modelEntity.setId(id);
            }
            session.getTransaction().commit();
            session.close();

        } catch (Exception e) {
            this.update(modelEntity);
        }

        return modelEntity;

    }


    public void update(ModelEntity modelEntity) {
        try {

            session = sessionFactory.openSession();
            session.beginTransaction();

            session.update(modelEntity);

            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }

    }

    @SuppressWarnings("unchecked")
    public List<Product> getProducts() {
        List<Product> list = null;

        try {
            session = sessionFactory.openSession();
            session.beginTransaction();

            list = session.createCriteria(Product.class).list();

            session.getTransaction().commit();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }

        return list;
    }

    @SuppressWarnings("unchecked")
    public List<Account> getAccounts() {
        List<Account> list = null;

        try {
            session = sessionFactory.openSession();
            session.beginTransaction();

            list = session.createCriteria(Account.class).list();

            session.getTransaction().commit();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }

        return list;
    }

    @SuppressWarnings("unchecked")
    public List<Product> getProductByName(String brand, String model) {
        List<Product> list = null;

        try {
            session = sessionFactory.openSession();
            session.beginTransaction();

            list = session.createCriteria(Product.class).add(Restrictions.eq("brand", brand))
                    .add(Restrictions.eq("model", model)).list();

            session.getTransaction().commit();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }

        return list;
    }

    @SuppressWarnings("unchecked")
    public List<Account> getAccountByUsername(String username) {
        List<Account> list = null;

        try {
            session = sessionFactory.openSession();
            session.beginTransaction();

            list = session.createCriteria(Account.class).add(Restrictions.eq("username", username)).list();

            session.getTransaction().commit();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }

        return list;
    }

    public void delete(ModelEntity modelEntity) {
        try {

            session = sessionFactory.openSession();
            session.beginTransaction();

            session.delete(modelEntity);

            session.getTransaction().commit();
            session.clear();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
    }


    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }


}
DataAccessObject类
包com.project.dataAccessLayer;
导入java.util.List;
导入org.hibernate.Session;
导入org.hibernate.SessionFactory;
导入org.hibernate.criteria.Restrictions;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.beans.factory.annotation.Qualifier;
导入org.springframework.stereotype.Repository;
导入com.project.model.Account;
导入com.project.model.ModelEntity;
导入com.project.model.Product;
@存储库(“dao”)
公共类DataAccessObject{
私人会话工厂会话工厂;
非公开会议;
@自动连线
@限定符(“会话工厂”)
public void setSessionFactory(SessionFactory SessionFactory){
this.sessionFactory=sessionFactory;
}
公共模型实体保存(模型实体模型实体){
整数id=null;
试一试{
session=sessionFactory.openSession();
session.beginTransaction();
id=(整数)session.save(modeleentity);
如果(id!=null){
modeleentity.setId(id);
}
session.getTransaction().commit();
session.close();
}捕获(例外e){
更新(模型实体);
}
返回模型实体;
}
公共无效更新(模型实体模型实体){
试一试{
session=sessionFactory.openSession();
session.beginTransaction();
更新(模型实体);
session.getTransaction().commit();
}捕获(例外e){
e、 printStackTrace();
}最后{
session.close();
}
}
@抑制警告(“未选中”)
公共列表产品(){
List=null;
试一试{
session=sessionFactory.openSession();
session.beginTransaction();
list=session.createCriteria(Product.class.list();
session.getTransaction().commit();
}捕获(例外e){
e、 printStackTrace();
}最后{
session.close();
}
退货清单;
}
@抑制警告(“未选中”)

公共列表

您希望在
CustomerDAO
中有一个带有
@Qualifier(“custDAO”)
的依赖项
DataAccessObject
bean,但是没有
@Qualifier(“custDAO”)

换句话说,问题是因为
@限定符(“custDAO”)
,即
Spring
容器无法将
DataAccessObject
bean注入
CustomerDAO
(因为预期的
@限定符没有可用的bean),因此您需要更改
DataAccessObject
,如下所示:

@Repository
@Qualifier("custDAO")
public class DataAccessObject {
    //current code
}

您可以在
@Qualifier

上查找更多信息。您希望
@Qualifier(“custDAO”)
中有一个依赖项
DataAccessObject
bean和
@Qualifier(“custDAO”)
,但是没有
DataAccessObject
bean可用于
@Qualifier(“custDAO”)

换句话说,问题是因为
@限定符(“custDAO”)
,即
Spring
容器无法将
DataAccessObject
bean注入
CustomerDAO
(因为预期的
@限定符没有可用的bean),因此您需要更改
DataAccessObject
,如下所示:

@Repository
@Qualifier("custDAO")
public class DataAccessObject {
    //current code
}

你可以在
@Qualifier

上查找更多信息。你应该仔细分离文件的内容,设置格式。我刚刚编辑完这篇文章,现在它是可读的,很抱歉,你应该仔细分离文件的内容,设置格式。我刚刚编辑完这篇文章,现在它是可读的,很抱歉,我还有一个带有@Repository(“administratorDAO”)公共类administratorDAO扩展CustomerDAO实现IAdministratorDAO{DataAccessObject dao;@Autowired@Qualifier(“adminDAO”)public void setDao(DataAccessObject dao){this.dao=dao;}所以我添加了那个限定符,因为另一个错误说我有两个“dao”如果我这样做了,我将如何在AdministratorDAO中使用DataAccessObject?确保在任何有setter注入(或注入)限定符的地方,都有一个用限定符注释的具体类(实现)。在DataAccessObject@Qualifier(“dao”)上添加一个限定符在CustomerDAO和AdministratorDAO中使用它(在这里我使用带有@Autowired注释的限定符(“dao”)将生成另一个异常org.spri
package com.project.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.project.model.Account;
import com.project.model.Product;
import com.project.services.CustomerService;
import com.project.services.ICustomerService;

@Controller("customerCOntroller")
@RequestMapping("/")
public class CustomerController {

    ICustomerService customerService;

    @Autowired(required = true)
    @Qualifier(value="customerService")
    public void setCustomerService(CustomerService cs){
        this.customerService = cs;
    }

    @RequestMapping(value = "/account", method = RequestMethod.GET)
    public ModelAndView showForm() {
        return new ModelAndView("account", "account", new Account());
    }

    @RequestMapping(value = "/signUp", method = RequestMethod.POST)
    public String signUp(@ModelAttribute("account") Account acc) throws Exception {

        this.customerService.signUp(acc.getFullName(), acc.getUsername(), acc.getPassword(), acc.geteMail(),
                acc.getAddress(), acc.getPermission());

        return "redirect:/logIn";

    }

    @RequestMapping(value = "/logIn", method = RequestMethod.POST)
    public String logIn(@ModelAttribute("account") Account acc) throws Exception {

        this.customerService.logIn(acc.getUsername(), acc.getPassword());

        return "redirect:/products";
    }

    @RequestMapping(value = "/logOut", method = RequestMethod.POST)
    public String logOut(@ModelAttribute("account") Account acc) throws Exception {

        this.customerService.logOut(acc.getId());

        return "redirect:/logIn";
    }

    @RequestMapping(value="/changePassword/{newPassword}",  method = RequestMethod.POST)
    public String changePassword(@ModelAttribute("account") Account acc,
            @PathVariable("newPassword") String newPassword) throws Exception {
        this.customerService.changePassword(acc.getUsername(), acc.getPassword(), newPassword);
        return "redirect:/logIn";
    }

    @RequestMapping(value = "/products", method = RequestMethod.GET)
    public ModelAndView showProducts() {
        List<Product> productList = this.customerService.showAllProducts();
        return new ModelAndView("products", "productList", productList);
    }
}
 <....>``



    <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/root-context.xml</param-value>
        </context-param>

        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>

        <servlet>
            <servlet-name>appServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/appServlet/servlet-context.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>

        <servlet-mapping>
            <servlet-name>appServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    </web-app>
    <?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"
        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.xsd">

    </beans>
    <?xml ....">

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

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

        <!-- Enables the Spring MVC @Controller programming model -->
        <annotation-driven />

        <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
            up static resources in the ${webappRoot}/resources directory -->
        <resources mapping="/resources/**" location="/resources/" />


        <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close">
            <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <beans:property name="url"
                value="jdbc:mysql://localhost:3306/guitarshop" />
            <beans:property name="username" value="root" />
            <beans:property name="password" value="" />
        </beans:bean>


        <beans:bean id="sessionFactory"
            class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            <beans:property name="annotatedClasses">
                <beans:list>
                    <beans:value>com.project.model.Account</beans:value>
                    <beans:value>com.project.model.Product</beans:value>
                </beans:list>
            </beans:property>
            <beans:property name="hibernateProperties">
                <beans:props>
                    <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
                    </beans:prop>
                    <beans:prop key="hibernate.show_sql">true</beans:prop>
                    <beans:prop key="hibernate.enable_lazy_load_no_trans">true</beans:prop>
                </beans:props>
            </beans:property>
        </beans:bean>

    <!--    <beans:bean id="dao" -->
    <!--        class="com.project.dataAccessLayer.DataAccessObject"> -->
    <!--        <beans:property name="sessionFactory" ref="sessionFactory" /> -->
    <!--    </beans:bean> -->

    <!--    <beans:bean id="customerDAO" class="com.project.dataAccessLayer.CustomerDAO"> -->
    <!--        <beans:property name="dao" ref="dao" /> -->
    <!--    </beans:bean> -->

    <!--    <beans:bean id="administratorDAO" -->
    <!--        class="com.project.dataAccessLayer.AdministratorDAO"> -->
    <!--        <beans:property name="dao" ref="dao" /> -->
    <!--    </beans:bean> -->


    <!--    <beans:bean id="customerService" class="com.project.services.CustomerService"> -->
    <!--        <beans:property name="customerDAO" ref="customerDAO"></beans:property> -->
    <!--    </beans:bean> -->

    <!--    <beans:bean id="administratorService" class="com.project.services.AdministratorService"> -->
    <!--        <beans:property name="administratorDAO" ref="administratorDAO"></beans:property> -->
    <!--    </beans:bean> -->

        <tx:annotation-driven transaction-manager="transactionManager" />

        <beans:bean id="transactionManager"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <beans:property name="sessionFactory"
                ref="sessionFactory" />
        </beans:bean>

    </beans:beans>
@Repository
@Qualifier("custDAO")
public class DataAccessObject {
    //current code
}