2014-09-29 54 views
0

這個錯誤正在殺死我,我在這裏搜索了很多類似的話題,但是他們都沒有幫助我! 所以,我使用的是Spring MVC + Hibernate。請看一下,也許你會發現我錯過的錯誤! 這裏是我的文件: 客戶創建名爲'clientsDaoImpl'的bean時出錯:注入自動裝配的依賴項失敗;

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

} 

ClientsService

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

ClientsServiceImpl

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

ClientsDao

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

ClientsDaoImpl

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

} 

ClientsController

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

} 

的web.xml

<?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.properties

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 

彈簧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: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> 

的pom.xml

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

UPD:爲得到hibernate.cfg

<?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

請添加更長的堆棧跟蹤 - 它是否失敗,沒有候選人發現異常? – freakman 2014-09-29 19:48:56

+0

你可以嘗試base-package =「app」,而不是「app。*」嗎? – 2014-09-29 20:00:45

+0

是的,我試着只是一個「應用程序」,沒有幫助。我在下面添加了更多信息 – Cooler 2014-09-29 20:11:53

回答

1

可能從這裏開始嘗試這種解決方案:Getting a org.springframework.beans.factory.BeanCreationException with my first Maven, Spring Project

只需添加:

<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-aspects</artifactId> 
    <version>${your-spring-version}</version> 
</dependency> 
+0

nope,沒有幫助:( – Cooler 2014-09-29 21:19:12

+0

嗯,你能找到包含AnnotationBeanConfigurerAspect的jar嗎?如果你添加了spring-aspects依賴項,你能在你的m2 repo中找到那個jar,看看它是否包含類文件? – FriedSaucePots 2014-09-29 21:37:25

+0

我不知道是什麼問題,我剛剛創建了一個新項目,使用Spring in Action和YouTube教程,並且此問題消失了,無論如何感謝您的幫助! – Cooler 2014-10-06 10:08:40

0

我覺得你在你的ClientsDaoImpl缺少倉庫的名稱。

@Repository("clientsDAO") 
public class ClientsDaoImpl implements ClientsDao..... 
相關問題