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

创建名为“clients daoimpl”的bean时出错:注入autowired依赖项失败;

谭光辉
2023-03-14

这个错误简直要了我的命,我在这里搜索了很多类似的主题,但没有一个对我有帮助!所以,我使用的是spring MVC+Hibernate。请看一下,也许你会发现我错过的错误!这是我的文件:客户端

package app.model;

import javax.persistence.*;

@Entity
@Table(name = "clients")
public class Clients {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int client_id;
    private String gender;
    private String first_name;
    private String last_name;
    private String country;
    private String city;
    private String birthdate;
    private String phone;
    private String email;
    private int orders;
    private double total_income;

    public Clients() {
    }

    public Clients(int client_id, String gender, String first_name,
            String last_name, String country, String city, String birthdate,
            String phone, String email, int orders, double total_income) {
        super();
        this.client_id = client_id;
        this.gender = gender;
        this.first_name = first_name;
        this.last_name = last_name;
        this.country = country;
        this.city = city;
        this.birthdate = birthdate;
        this.phone = phone;
        this.email = email;
        this.orders = orders;
        this.total_income = total_income;
    }

    public String getBirthdate() {
        return birthdate;
    }

    public void setBirthdate(String birthdate) {
        this.birthdate = birthdate;
    }

    public int getClient_id() {
        return client_id;
    }

    public void setClient_id(int client_id) {
        this.client_id = client_id;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getFirst_name() {
        return first_name;
    }

    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }

    public String getLast_name() {
        return last_name;
    }

    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getOrders() {
        return orders;
    }

    public void setOrders(int orders) {
        this.orders = orders;
    }

    public double getTotal_income() {
        return total_income;
    }

    public void setTotal_income(double total_income) {
        this.total_income = total_income;
    }

}
package app.service;

import java.text.ParseException;
import java.util.List;

import app.model.Clients;

public interface ClientsService {
    public void addClient(Clients client);
    public void updateClient(Clients client) throws ParseException;
    public void deleteClient(Clients client) throws ParseException;
    public Clients getClient(int client_id);
    public List<Clients> getAllClients();
}
package app.service.impl;

import java.text.ParseException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import app.dao.ClientsDao;
import app.model.Clients;
import app.service.ClientsService;

@Service
public class ClientsServiceImpl implements ClientsService {

    @Autowired
    private ClientsDao clientsDao;

    public ClientsServiceImpl() {
    }

    public List<Clients> getAllClientsList() {
        return clientsDao.getAllClients();
    }

    @Transactional
    public void addClient(Clients client) {
        clientsDao.addClient(client);
    }

    @Transactional
    public void updateClient(Clients client) throws ParseException{ 
        clientsDao.updateClient(client);
    }

    @Transactional
    public void deleteClient(Clients client) throws ParseException{
        clientsDao.deleteClient(client);
    }

    @Transactional
    public Clients getClient(int client_id) {
        return clientsDao.getClient(client_id);
    }

    @Transactional
    public List<Clients> getAllClients() {
        return clientsDao.getAllClients();
    }
}
package app.dao;

import java.text.ParseException;
import java.util.List;

import app.model.Clients;

public interface ClientsDao {
    public void addClient(Clients client);
    public void updateClient(Clients client) throws ParseException;
    public void deleteClient(Clients client) throws ParseException;
    public Clients getClient(int client_id);
    public List<Clients> getAllClients();
}
package app.dao.impl;

import java.text.ParseException;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import app.model.Clients;
import app.dao.ClientsDao;
import app.logic.Similar;

@Repository
public class ClientsDaoImpl implements ClientsDao {

    @Autowired
    public SessionFactory session;


    public ClientsDaoImpl() {
    }


    @Transactional
    @SuppressWarnings("unchecked")
    public List<Clients> getAllClients() {
        return session.getCurrentSession().createQuery("from Clients").list();
    }

    @Transactional
    public void addClient(Clients client) { // add new client
        session.getCurrentSession().save(client);
    }

    @Transactional
    public void updateClient(Clients client) throws ParseException { // update client                                               
        session.getCurrentSession().update(client);
    }

    @Transactional
    public void deleteClient(Clients client) throws ParseException { // delete client if he\she doesnt have any orders
        session.getCurrentSession().delete(client);
    }

    @Transactional
    public Clients getClient(int client_id) {
        return (Clients)session.getCurrentSession().get(Clients.class, client_id);
    }

}
package app.controller;

import java.text.ParseException;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import app.model.Clients;
import app.service.ClientsService;


@Controller
public class ClientsController {

    @Autowired
    public ClientsService clientsService;


    public ClientsController(){

    }

    @RequestMapping("/index")
    public String setupForm(Map<String, Object> map) {
        Clients client = new Clients();
        map.put("client", client);
        map.put("clientsList", clientsService.getAllClients());
        return "client";
    }

    @RequestMapping(value = "/client.do", method = RequestMethod.POST)
    public String doActions(@ModelAttribute Clients client,
            BindingResult result, @RequestParam String action,
            Map<String, Object> map) throws ParseException {
        Clients clientResult = new Clients();

        switch (action.toLowerCase()) {
        case "add":
            clientsService.addClient(client);
            clientResult = client;
            break;
        case "edit":
            clientsService.updateClient(client);
            clientResult = client;
            break;
        case "delete":
            clientsService.deleteClient(client);
            clientResult = new Clients();
            break;
        case "search":
            Clients searchedClient = clientsService.getClient(client.getClient_id());
            clientResult = searchedClient != null ? searchedClient : new Clients();
            break;
        }
        map.put("client", clientResult);
        map.put("clientsList", clientsService.getAllClients());
        return "client";
    }

}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>ProjectMVC</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.dialect=org.hibernate.dialect.MySQLDialect
jdbc.databaseurl=jdbc:mysql://localhost:3306/projectdb
jdbc.username=root
jdbc.password=root
<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">



    <context:annotation-config />
    <context:component-scan base-package="app.*">
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

    <context:spring-configured />

     <bean
        class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />

    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="/WEB-INF/jdbc.properties" />

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
        p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" />
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <tx:annotation-driven />

    <aop:config proxy-target-class="true"/>
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
</beans>
<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>Project</groupId>
    <artifactId>Project</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <org.springframework.version>3.0.5.RELEASE</org.springframework.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <!-- Hibernate resources -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>3.6.7.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>4.3.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-commons-annotations</artifactId>
            <version>3.3.0.ga</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.3.1.GA</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.3.2.GA</version>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.1.2</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>20030825.184428</version>
        </dependency>
        <dependency>
            <groupId>commons-pool</groupId>
            <artifactId>commons-pool</artifactId>
            <version>20030825.183949</version>
        </dependency>
        <!-- MySQL -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <!-- Log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.14</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.5.0</version>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0</version>
        </dependency>

    </dependencies>


    <build>
        <testSourceDirectory>src/main/test</testSourceDirectory>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/webapp</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 

<hibernate-configuration>
    <session-factory>        
        <mapping class="app.model.Clients" />
    </session-factory>         
</hibernate-configuration>

共有1个答案

轩辕乐邦
2023-03-14

可以从这里尝试一下这个解决方案:在我的第一个Maven Spring项目中获得一个org.SpringFramework.Beans.Factory.BeanCreationException

只要加上:

<dependency> 
   <groupId>org.springframework</groupId> 
   <artifactId>spring-aspects</artifactId> 
   <version>${your-spring-version}</version> 
</dependency>
 类似资料: