2013-06-19 71 views
1

我很累人用spring + hibernate + maven + postgreSQL構建一個小應用程序,但是當我厭倦了在我的控制器和服務類中注入依賴項時,它創建注入依賴項的問題在這裏是代碼和日誌請給你的建議來解決這個佈線問題?錯誤創建bean通過@Resource

mvc-dispature-servlet.xml 

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 


<context:component-scan base-package="com.bms" /> 
<context:annotation-config /> 



<bean id="dataSource" 
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="org.postgresql.Driver" /> 
    <property name="url" value="jdbc:postgresql://localhost:5432/bms" /> 
    <property name="username" value="postgres" /> 
    <property name="password" value="*******" /> 
</bean> 

<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 

    <property name="dataSource"> 
     <ref bean="dataSource" /> 
    </property> 

    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop> 
      <prop key="hibernate.show_sql">true</prop> 
      <prop key="hibernate.hbm2ddl.auto">create</prop> 
     </props> 
    </property> 

    <property name="annotatedClasses"> 
     <list> 
      <value>com.bms.Domain.Book</value> 
     </list> 
    </property> 

</bean> 

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

Controller class BooksEntryController.java 

@Controller 
@RequestMapping(value = "/dashboard/*") 
public class BooksEntryController { 

@Resource 
private BookFormService bookFormServiceImp; 

@RequestMapping(value = "/booksentryform", method = RequestMethod.GET) 
public ModelAndView viewBooksEntryForm(Model model) { 
    model.addAttribute("test", new Book()); 
    return new ModelAndView("bookregistrationform"); 
} 

@RequestMapping(value = "/addbookdetails", method = RequestMethod.POST) 
public String addBook(@ModelAttribute("test") Book book, Model model) { 

    if (book != null) { 
     bookFormServiceImp.addBooks(book); 
     return "bookaddsuccessfully"; 

    } else { 
     throw new NullPointerException(); 
    } 

} 
} 


Service interface BookFormService.java 

    public interface BookFormService { 
void addBooks(Book book); 
     } 

ServiceImp class BookFormServiceImp.java 

    @Service("bookFormServiceImp") 

    public class BookFormServiceImp implements BookFormService { 

@Resource 
private BookFormDao bookFormDaoImp ; 

public void addBooks(Book book) { 
    bookFormDaoImp.addBook(book); 
    // TODO Auto-generated method stub 

} 

    } 


    Dao interface BookFormDao .java 

    public interface BookFormDao { 
public void addBook(Book book); } 

DaoImp class BookFormDaoImp.java 

    @Repository("bookFormDaoImp") 
    public class BookFormDaoImp implements BookFormDao { 
@Resource 
private SessionFactory sessionFactory; 

private Session getCurrentSession() { 
    return sessionFactory.getCurrentSession(); 
} 

public void addBook(Book book) { 
    // TODO Auto-generated method stub 
    getCurrentSession().save(book); 

} 

    } 

這裏是日誌:

2013-06-20 00:35:51.528:INFO:/bms:Initializing Spring root WebApplicationContext 
Jun 20, 2013 12:35:51 AM org.springframework.web.context.ContextLoader initWebApplicationContext 
INFO: Root WebApplicationContext: initialization started 
Jun 20, 2013 12:35:51 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh 
INFO: Refreshing Root WebApplicationContext: startup date [Thu Jun 20 00:35:51 IST 2013]; root of context hierarchy 
Jun 20, 2013 12:35:51 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml] 
Jun 20, 2013 12:35:52 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons 
INFO: Pre-instantiating singletons in org.s[email protected]2df8f8: defining beans [booksEntryController,bookFormDaoImp,bookFormServiceImp,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,dataSource,sessionFactory,org.springframework.web.servlet.view.InternalResourceViewResolver#0,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy 
Jun 20, 2013 12:35:52 AM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons 
INFO: Destroying singletons in org.s[email protected]2df8f8: defining beans [booksEntryController,bookFormDaoImp,bookFormServiceImp,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,dataSource,sessionFactory,org.springframework.web.servlet.view.InternalResourceViewResolver#0,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy 
Jun 20, 2013 12:35:52 AM org.springframework.web.context.ContextLoader initWebApplicationContext 
SEVERE: Context initialization failed 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'booksEntryController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookFormServiceImp': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookFormDaoImp': Injection of resource dependencies failed; nested exception is java.lang.NoClassDefFoundError: Lorg/hibernate/cache/CacheProvider; 
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:306) 
     ................................... 
     .................................... 
+0

您似乎缺少一些罐子。 – smk

+0

您正在使用哪個版本的hibernate和spring? –

回答

0

你可能不使用正確的sessionFactory類。如果您使用的是Hibernate 4,請使用LocalSessionFactoryBean而不是AnnotationSessionFactoryBean

更換

<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 

通過

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

感謝Jean問題解決 –

相關問題